Implement AOP programming using spring annotations and XML
To implement the AOP programming annotation method, follow these steps:
1) Introduce jar files related to aop first (excellent aspectj aop components)
Spring-aop-3.2.5.RELEASE.jar [spring3.2 source code]
Aopalliance. jar [spring2.5 source code/lib/aopalliance]
Aspectjweaver. jar [spring2.5 source code/lib/aspectj] or [aspectj-1.8.2lib]
Aspectjrt. jar [spring2.5 source code/lib/aspectj] or [aspectj-1.8.2lib]
Note:: The jar file of spring2.5 may be used if JDK is used.
You need to upgrade the aspectj component by providing the jar file in the aspectj-1.8.2 version.
2) introduce the aop namespace in bean. xml (search xmln: aop in the document)
3) Enable the aop Annotation
4) use annotations
@ Aspect: specify a class as the partition class.
@ Pointcut (execution (* cn. itcast. e_aop_anno. *. * (..) specifies the entry point expression
@ Before (pointCut _ () pre-notification: executed Before the target method
@ After (pointCut _ () post-Notification: After the target method is executed (always executed)
@ AfterReturning (pointCut _ () notification after return: execution before the execution method ends (exception not executed)
@ AfterThrowing (pointCut _ () exception notification: Execute when an exception occurs
@ Around (pointCut _ () surround notification: Surround target method execution
Case:
1) IUserDao Interface
package cn.itcast.aop_anno;public interface IUserDao {public void save();}
2) UserDao implements the IUserDao Interface
Package cn. itcast. aop_anno; import org. springframework. stereotype. component; @ Component // IOC container management public class UserDao implements IUserDao {public void save () {System. out. println (core business! Save );}}
3) face-cutting Aop
Package cn. itcast. aop_anno; import org. aspectj. lang. annotation. after; import org. aspectj. lang. annotation. aspect; import org. aspectj. lang. annotation. before; import org. aspectj. lang. annotation. pointcut; import org. springframework. stereotype. component; @ Component @ Aspect // specify the current class as the face-cutting class public class Aop {// face-cutting class @ Pointcut (execution (* cn. itcast. aop_anno. *. *(..))) // cut surface expression public void pointCut _ () {}@ Before (pointCut _ () // intercept the method specified by the cut surface expression .. public void before () {System. out. println (before) ;}@ After (pointCut _ () public void after () {System. out. println (after );}}
4) bean. xml configuration --> introduce the namespace (search xmln: aop in the document)
--> Enable annotation Scanning
--> Enable annotation aop Annotation
5) test App ---> the returned value must be the IUserdao interface.
package cn.itcast.aop_anno;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {ApplicationContext ac=new ClassPathXmlApplicationContext(cn/itcast/aop_anno/bean.xml);@Testpublic void test(){IUserDao userdao=(IUserDao) ac.getBean(userDao);userdao.save();}}
Test results:
------------------------------------------ Deep understanding ----------------------------------------
1) if UserDao does not implement the IUserDao Interface
The test class is changed:
package cn.itcast.aop_anno;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {ApplicationContext ac=new ClassPathXmlApplicationContext(cn/itcast/aop_anno/bean.xml);@Testpublic void test(){UserDao userdao=(UserDao) ac.getBean(userDao);System.out.println(userdao.getClass());userdao.save();}}
Test result: --> the returned result must be UserDao, that is, the class is not an interface.
To implement AOP programming in xml mode, follow these steps:
Implement aop programming in Xml:
1) introduce the jar file [aop-related jar, 4]
2) introduce the aop namespace
3) aop Configuration
* Configure face-cutting classes (classes formed by repeated code execution)
* Aop Configuration
Which methods to intercept/apply the notification code after the method is intercepted?
Case:
IUserDao, UserDao, and AOP classes are the same as above, ---------> just remove the annotation ..
1) bean. xml file configuration
Aspect ref = "aop">
2) test apps
package cn.itcast.aop_xml;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {ApplicationContext ac=new ClassPathXmlApplicationContext(cn/itcast/aop_xml/bean.xml);@Testpublic void test(){IUserDao userdao= (IUserDao) ac.getBean(userDao);System.out.println(userdao.getClass());userdao.save();}}
Running result: