Spring-AOP-XML

Source: Internet
Author: User
1. Establish the previous dependency injection example. 2. Import aopalliance. Jar aspectjrt. Jar aspectjweaver. Jar three packages 3. Create logaspect. java. Note that the method does not have annotation (nonsense ). Therefore, you need. xml configuration package Org. zttc. ITAT. spring. proxy; import Org. aspectj. lang. joinpoint; import Org. aspectj. lang. proceedingjoinpoint; import Org. springframework. stereotype. component; @ component ("logaspect") // set this partition class to public class logaspect {public void logstart (joinpoint JP) managed by spring {// System of the object to be executed. out. println (JP. gettarget (); // obtain the execution method system. out. println (JP. getsignature (). getname (); logger.info ("add log" );} Public void logend (joinpoint JP) {logger.info ("adding logs after method call");} public void logaround (proceedingjoinpoint pjp) throws throwable {logger.info ("start to add logs to und"); pjp. proceed (); // execute the program logger.info ("end around");} 4. bean. XML <? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/conte XT http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <! -- Enable spring's Annotation support --> <context: annotation-config/> <! -- Set which packages spring uses to find annotation --> <context: component-scan base-package = "org. zttc. itat. Spring"/> <AOP: config> <! -- Define a plane --> <AOP: aspect id = "mylogaspect" ref = "logaspect"> <! -- Where to add the corresponding aspect --> <AOP: pointcut id = "logpointcut" expression = "execution (* Org. zttc. ITAT. spring. dao. *. add *(..)) | execution (* Org. zttc. ITAT. spring. dao. *. delete *(..)) | execution (* Org. zttc. ITAT. spring. dao. *. update *(..)) "/> <AOP: Before method =" logstart "pointcut-ref =" logpointcut "/> <AOP: after method = "logend" pointcut-ref = "logpointcut"/> <AOP: Und und method = "logaround" pointcut-ref = "logpointcut"/> </AOP: aspect> </AOP: config> </beans> generally, AOP uses XML instead of annotion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.