Springframework (4)

zhaozj2021-02-16  97

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">

. * /. get. *

. * /. set. *

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">

. * /. get. *

. * /. set. *

(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:

tony

51

Personimpl implements the Person interface L Interceptors and Advisors:

Something

l proxy:

Eg.Person

myadvisor

Debuginterceptor

l Use Bean:

• The client should get Person Bean instead of Persontarget

• Access can be accessed through application context or programming

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)

True

myadvisor

Debuginterceptor

(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">

Employee *

Myinterceptor

(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">

. * /. get. *

This Advisor application debuginterceptor to all GET methods for any class

Class = "... aop.framework.autoproxy.advisorautoproxycreator">

True

(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

转载请注明原文地址:https://www.9cbs.com/read-10627.html

New Post(0)