3, AOP
(1) aspect-oriented programming
l Supplement OOP
l Decompose the problem (or relationship)
l Modular relationship
l Usage:
? Persistence
? Transaction management
? Safety
? Log management
? Debugging
(2) AOP concept
l aspect: Modular relationship (Concern)
l JOINPOINT: a point at the time of execution
l Advice: Action made in concrete JoinPoint
l PointCut: An Advice should be activated specified JOINPOINT collection
l INTRODUCTION: Add method or domain to the Advice class
(3) Pointcut
l A specified JoinPoint collection that Advice should be activated
Public interface pointcut {
Classfilter getClassFilter ();
Methodmatcher getMethodmatcher ();
}
Public interface classfilter {
Boolean Matches (Class Clazz);
}
Public interface methodmatcher {
Boolean Matches (Method M, Class TargetClass);
Boolean Matches (Method M, Class TargetClass, Object [] args;
Boolean isRuntime ();
}
Limit PointCut as a set of Target classes; static PointCuts does not need to use parameters
(4) PointCut implementation
l Regular expression
Class = "org.springframework.aop.support.RegexpMethodpointcut"> list> Property> bean> Method Name All Road Matching Perl5 Regular Expression (5) Advice l Action made in concrete JoinPoint Public interface methodinterceptor extends interceptor { Object Invoke (MethodInvocation Invocation) throws throwable; } Spring uses an interceptor chain surrounded by JoinPoint (Interceptor) chain to implement Advice l example: Public class debuginterceptor imports methodinterceptor { Public Object Invoke (MethodInvocation Invocation) Throws throwable { System.out.println (">>" invocation); // BeforeObject rval = invocation.proceed (); System.out.println ("<< Invocation Returned"); //After Return RVAl; } } (6) Advice type l Around Advice (such as the example above) l before advice l throws advice l at Returning Advice l Introduction Advice (7) Spring Advisors l Pointcutadvisor = PointCut Advice l Each built-in Advice has an Advisor l example: Class = "... aop.support.regexpmethodpointcutaroundadadvisor"> Property> list> Property> bean> (8) ProxyFactory l Get an Advised object using ProxyFactory ? Define the application of Pointcuts and Advices • Return to Interceptor with a proxy object • Use Java dynamic proxy or CGLIB2 (can proxy interface and class) l Programming method creation AOP proxy ProxyFactory Factory = New ProxyFactory (MybusinessInterface); Factory.addinterceptor (MyMethodInterceptor); Factory.addadvisor (MyAdvisor); MybusinessInterface b = (mybusinessinterface) factory.getProxy (); (9) ProxyFactoryBean l Used to get a proxy for Bean l Beans to be proxy: bean> Personimpl implements the Person interface L Interceptors and Advisors: bean> bean> l proxy: list> Property> bean> l Use Bean: • The client should get Person Bean instead of Persontarget • Access can be accessed through application context or programming bean> Person Person = (Person) Factory.getBean ("Person"); l If it is a proxy class rather than an interface • Set the proxyTargetClass to True to replace ProxyInterfaces ? The agent wants to expand the Target class (constructed by CGLIB) list> Property> bean> (10) AutoProxy l Auto Agent creation: ? As long as defining targets • Selected bean will be automatically proxy l Don't use ProxyFactoryBean for each Target Bean (11) BeanNameAutoproxycreator l Select Targets using a bean name. Class = "... AOP.Framework.autoproxy.BeanNameAutoproxycreator"> list> Property> bean> (12) AdvisorauToproxycreator l Auto application Advisors to the bean in Context Each Advisor corresponds to a PointCut and Advice • If PointCut is applied to bean, it will be intercepted by Advice. l helps to maintain the consistency of the same Advice to multiple transaction objects l Can't get an object without advised l example: Class = "... aop.support.regexpmethodpointcutadvisor"> constructor-arg> bean> This Advisor application debuginterceptor to all GET methods for any class Class = "... aop.framework.autoproxy.advisorautoproxycreator"> bean> (13) AOP advanced features L metadata driven automatic agent l target source (Targetsources) • Heat exchange target Source: When the caller is allowed to keep his reference, the agent is allowed to switch the agent's bean • Target source pool: Maintain the pool of the same instance, release the object to the pool when the method is activated