Spring-AOP-Annotation

Source: Internet
Author: User

1. Suppose you have configured the dependency injection component. In this case, Bean. XML is

<? 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: 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/context http://www.springframework.org/schema/context/sprin G-context-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 "/> </beans> 2. add schema xmlns: AOP = "http://www.springframework.org/schema/aop" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 3. open the final bean of annotation-based AOP <AOP: aspectj-autoproxy/>. 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"/> <! -- Open annotation-based AOP --> <AOP: aspectj-autoproxy/> </beans> 4. Create an AOP focus. It is called logaspect. java. package Org. zttc. ITAT. spring. proxy; import Org. aspectj. lang. joinpoint; import Org. aspectj. lang. proceedingjoinpoint; import Org. aspectj. lang. annotation. after; import Org. aspectj. lang. annotation. around; import Org. aspectj. lang. annotation. aspect; import Org. aspectj. lang. annotation. before; import Org. springframework. stereotype. component; @ component ("logaspect") // Let this partition class be managed by spring @ aspect // declare this The class is a face-cutting class public class logaspect {/** execution description I want to use this method * execution (* Org. zttc. ITAT. spring. dao. *. add *(..)) * The first * indicates any return value * The second * Indicates Org. zttc. ITAT. spring. all classes in the DaO package * Third * indicate all methods starting with add *(..) any parameter */@ before ("execution (* Org. zttc. ITAT. spring. dao. *. add *(..)) | "+" execution (* Org. zttc. ITAT. spring. dao. *. delete *(..)) | "+" execution (* Org. zttc. ITAT. spring. dao. *. update *(..)) ") Public void logstart (joindium Oint JP) {// The execution object system. out. println (JP. gettarget (); // obtain the execution method system. out. println (JP. getsignature (). getname (); logger.info ("add log");}/*** run * @ Param JP */@ After ("execution (* Org. zttc. ITAT. spring. dao. *. add *(..)) | "+" execution (* Org. zttc. ITAT. spring. dao. *. delete *(..)) | "+" execution (* Org. zttc. ITAT. spring. dao. *. update *(..)) ") Public void logend (joinpoint JP) {logger.info (" adding logs to method call end ");}/** * Execute * @ Param pjp * @ throws throwable */@ around ("execution (* Org. zttc. ITAT. spring. dao. *. add *(..)) | "+" execution (* Org. zttc. ITAT. spring. dao. *. delete *(..)) | "+" execution (* Org. zttc. ITAT. spring. dao. *. update *(..)) ") Public void logaround (proceedingjoinpoint pjp) throws throwable {logger.info (" start adding logs to around "); pjp. proceed (); // execute the program logger.info ("end around");} 4.1 import aopalliance. jar aspectjrt. jar Pectjweaver. jar three packages, because spring uses aspect to implement AOP Summary: Spring's AOP is still very simple, and the preceding example of dependency injection does not need to be moved at all, I woven the method to Dao here, but I only know it here. From the perspective of the woven object, it doesn't know if you have added the method to yourself.

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.