There are two main methods: XML Schema-based setting and annotation-based setting.
Based onXML SchemaPre-notification(Currently, logbeforeadvice does not need to implement methodbeforeadvice)
Logbeforeadvice. Java
Public ClassLogbeforeadvice {Public VoidBefore (joinpoint) {system. Out. println (joinpoint. getsignature (). getname ()+ ", Start... logbeforeadvice");}}
Applicationcontext. xml
< Bean ID = "Logbeforeadvice" Class = "Com.edu. cug. logbeforeadvice" > </ Bean > < AOP: config > < AOP: Aspect ID = "Logbeforeaspect" Ref = "Logbeforeadvice" > < AOP: pointcut ID = "Logbeforepointcut" Expression = "Execution (* HELLO *(..))" /> < AOP: before Pointcut-ref = "Logbeforepointcut" Method = "Before" /> </ AOP: Aspect > </ AOP: config >
TestProgram:
View code
Public ClassAoptest {Public Static VoidMain (string [] ARGs) {beanfactory Factory=NewClasspathxmlapplicationcontext ("Applicationcontext. xml"); Ihello Bean= (Ihello) Factory. getbean ("ihello"); Bean. hello1 ("Aop1"); Bean. hello2 ("Aop2");}}
Test Results
Hello1, start... logbeforeadvicehello1, aop1hello2, start... logbeforeadvicehello2, aop2
- Based onAnnotationPre-notification
- Definition aspect: Logbeforeadvice. Java
@ Aspectpublic class logbeforeadvice {@ pointcut ("execution (* com.edu. cug. ihello. *(..)) ") Public void anymethod () {}@ before (" anymethod () ") Public void before (joinpoint) {system. out. println (joinpoint. getsignature (). getname () + ", start ............. logbeforeadvice ");}}
Applicationcontext. xml
bean id =" ihello " class =" com.edu. cug. ihelloimpl " > bean > bean id =" logbeforeadvice " class =" com.edu. cug. logbeforeadvice " >
bean > AOP: aspectj-autoproxy />
Test procedure
Aoptest. Java
Public ClassAoptest {Public Static VoidMain (string [] ARGs) {beanfactory Factory=NewClasspathxmlapplicationcontext ("Applicationcontext. xml"); Ihello Bean= (Ihello) Factory. getbean ("ihello"); Bean. hello1 ("Aop1"); Bean. hello2 ("Aop2");}}
Test Results
Hello1, start... logbeforeadvicehello1, aop1hello2, start... logbeforeadvicehello2, aop2
To declare pointcut in spring2.x, there are two main parts: pointcut expression (expression) and pointcut signature (signature)