AOP and its application in Spring (2)

xiaoxiao2021-03-06  57

The implementation of AOP in Spring is implemented by dynamic agents. The implementation of the dynamic agent has been involved in the upper blog. The most practical AOP app is currently the most practical AOP application in Spring is not used by its transaction management mechanism. It is also this, so that Spring AOP is very different.

Then we continue to surround the examples of the previous section to explore the application and development of AOP mechanisms in Spring.

First, look at the implementation of several basic concepts in AOP:

Ø Pointcut

A collection of connection points, which indicates when the process will be triggered.

For us, the conditions of "When" is triggered "is mostly a way to be a way. PointCut settings for the method names when configuring transaction management in Spring, specifying AOP-based transaction management. Then, for the AOP components we implemented, we can also use the method name as the trigger judgment condition.

We can set trigger conditions for our components by configuring the following nodes in an XML configuration file.

Class = "org.springframework.aop.support.RegexpMethodpointcutadvisor">

. * save. *

. * do. *

Above We set a method name-based trigger condition for MyInterceptor, that is, modeceptor is triggered when the specified method of the target class is running.

Ø Treatment method (Advice)

That is to say to implement an Interceptor for triggering when connecting points. The General AOP interface of the AOP Alliance (AOPALLIANCE.jar) is used in Spring. Here we use MethodInterceptor defined in AopAlliance.ja as our Advice implementation interface. Then, the processing method in our previous example should be implemented as the following interceptor:

Package test.aop.spring;

Import java.util.logging.logger;

Import Org.AopAlliance.Intercept.MethodInterceptor;

Import Org.AopAlliance.Intercept.MethodInvocation;

/ **

* @Author xkf

** /

Public class lockinterceptor {

Private logger logger = logger.getlogger (this.getclass (). getname ());

Public Object Invoke (MethodInvocation Invocation) throws throwable {

// Todo auto-generated method stub

LOCK ();

Object Ret = Invocation.Proceed (); unlock ();

Return Ret;

}

Private void lock () {

Logger.info ("Lock Domain Object ...");

}

Private void unlock () {

Logger.info ("Unlock Domain Object ...");

}

}

After the implementation, the corresponding interceptor implements the configuration in the configuration file as follows:

Finally, we also need to define a Spring AOP ProxyFactory to load execution AOP components and need to be injected into the interface through IOC.

The corresponding configuration file should be configured as follows to implement these contents:

Test.aop.spring.domainobjdao

mypointcutadvisor

Everything! Write a TestCase to see the results:

Package test.aop.spring;

Import junit.framework.testcase;

Import org.springframework.context.ApplicationContext;

Import org.springframework.context.support.filesystemxmlapplicationContext;

/ **

* @Author xkf

* /

Public class SpringTestcase Extends Testcase {

Domainobjdao test = null;

Protected void setup () throws exception {

Super.setup ();

ApplicationContext CTX = New FileSystemXMLApplicationContext ("Test / Bean.xml");

Test = (DomainObjdao) CTX.getBean ("MyAopProxy");

}

protected void teardown () throws exception {

Super.teardown ();

}

Public void testsave () {

Test.save ();

}

}

The results of the operation are as follows:

Information: Creating Shared Instance of Singleton Bean 'Myinterceptor'2004-12-7 23:50:11 Test.Aop.Spring.lockInterceptor LOCK

Information: LOCK DOMAIN Object ...

Saving Domain Object ......

2004-12-7 23:50:11 Test.aop.spring.lockinterceptor unlock

Information: UNLOCK DOMAIN Object ...

OK! At this point, we have explored the implementation of the AOP dynamic agent and its applications in Spring. The next blog I will pay attention to the implementation of transactions in Spring and persistence.

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

New Post(0)