Spring ------ AOP,
I. concept?
AOP is short for Aspect Oriented Programming, which means cross-section Programming. It is used to solve the problem of program function expansion, and does not need to change the original code during the enhancement. The underlying layer is implemented using the dynamic proxy mode.
There are two types of dynamic proxy implementation, and the core is polymorphism. Whether it is a JDK-based Dynamic proxy or a cglib-based Dynamic proxy, it only uses the relationship between interfaces and implementation classes, and the relationship between parent classes and child classes.
This kind of dynamic proxy application features: if any real class has an implemented interface, this method is used to create another implementation class of the interface as the proxy class. It establishes a proxy class that implements a common interface by using the relationship between interfaces and implementation classes.
- Cglib-based Dynamic Proxy: if the real class is a common class without an interface, this method is used to create a subclass of the real class as the proxy class.
Through examples
@ Test // create a real object final UserService userService = new UserServiceImp () based on the JDK proxy public void fun01 () {// obtain the loaders and interfaces of real objects, create a Proxy class // reflection call method UserService proxyInstance = (UserService) Proxy. newProxyInstance (userService. getClass (). getClassLoader (), userService. getClass (). getInterfaces (), new InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {/ ** Proxy: proxy object, almost no * method: method reference save () --- reference of the save method * args: method parameter */if ("saveUser ". equals (method. getName () {Logger. logger () ;}//// reflection call to run the result so that the proxy object can receive the result. Return method. invoke (userService, args); // return null ;}}); // The proxy does not work. The real class proxyInstance. saveUser ();}
@ Test // Based on Cglib dynamic proxy // if the real class is a common class and no interface is implemented, this method is used to create a subclass of the real class as the proxy class. Public void fun02 () {// enable the creation executor Enhancer enhancer = new Enhancer (); // create the real object final UserService2 userService = new UserService2 (); enhancer. setSuperclass (UserService2.class); enhancer. setCallback (new MethodInterceptor () {@ Override public Object intercept (Object o, Method method, Object [] objects, MethodProxy methodProxy) throws Throwable {Logger. logger (); return method. invoke (userService, objects) ;}}); UserService2 proxyInstance = (UserService2) enhancer. create (); proxyInstance. test ();}Ii. AOP terminology
Iii. Introduction to AOP
Public class UserServiceImpl implements UserService {@ Override public void save () {System. out. println ("the save method of UserServiceImpl is called ");}}
- Define enhancement class
Public class Logger {public static void log () {System. out. println ("output log ~~ ");}}
- Import jar package
A. jar B required for importing spring. additional import: spring-aop-xx.jar, spring-aspect-xx.jar face-to-face process, Spring AOP is followed by the AOP Alliance specification implementation, so the need to have the AOP Alliance interface package aopalliance-x.x.jar, interface package depends on aspectjweaver-x.x.x.jar
- Xml configuration
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Aop getting started configuration --> <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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context: component-scan base-package = "gaga"/> <bean id = "us" class = "gaga. service. serviceimp. userServiceImp "/> <bean id =" ul "class =" gaga. utils. logger "/> <aop: config> <aop: pointcut id =" a "expression =" execution (* gaga. service. serviceimp. *. *(..)) "/> <aop: aspect ref =" ul "> <aop: after method =" logger "pointcut-ref =" a "/> </aop: aspect> </aop: config> </beans>