first, Code Practice
First on the code, Spring AOP demo, see below for a small example.
1 packagedemo.spring;2 3 Importorg.aspectj.lang.annotation.After;4 Importorg.aspectj.lang.annotation.Aspect;5 Importorg.aspectj.lang.annotation.Before;6 Importorg.aspectj.lang.annotation.Pointcut;7 Importorg.junit.Test;8 Importorg.junit.runner.RunWith;9 Importorg.springframework.beans.factory.annotation.Autowired;Ten Importorg.springframework.stereotype.Component; one Importorg.springframework.test.context.ContextConfiguration; a Importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@RunWith (springjunit4classrunner.class) the@ContextConfiguration ("classpath:spring-config.xml") - public classAopdemo { - @Autowired - PrivateTarget target; + - @Test + public voidTestsayhello () { aTarget.dosomething ("hello world"); at } - } - - @Component - classTarget { - public voiddosomething (String Params) { in System.out.println (params); - } to } + - @Aspect the @Component * classInterceptor { $ Panax Notoginseng@Pointcut ("execution (* demo.spring.Target.doSomething (String)) && args (params)") - public voiddosomething (String Params) {} the +@Before ("dosomething (params)") a public voidbefore (String Params) { theSystem.out.println ("before params:" +params); + } - $@After ("dosomething (params)") $ public voidafter (String Params) { -System.out.println ("after:" +params); - } the}
The Spring-config.xml file is configured as Follows:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "http://www.springframework.org/schema/beans"3 Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"4 Xmlns:context= "http://www.springframework.org/schema/context"5 XMLNS:AOP= "http://www.springframework.org/schema/aop"6 xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd ">78 <Context:component-scanBase-package= "demo.spring"/>9 <Aop:aspectj-autoproxy/>Ten one </Beans>
Points to Note:
* context:component-scan, using @autowired auto-injection, @Component automatically release beans, two annotations need to configure this element, otherwise cannot use annotations;
* aop:aspectj-autoproxy, use @aspectj and other AOP annotations need to be configured, otherwise cannot use annotations, @AspectJ annotations, will @component automatically publish "interceptor" The bean is converted to a aspectj slice, and annotations such as @pointcut, @Before, @After, @Around, and so on, are identical to those configured in the XML file; @Pointcut Note The following method is meaningless, Just require a corresponding method to provide annotation Attachment.
* Annotations can only be used to obtain the source of the scene, if not to obtain the source code, you can only through the form of XML configuration, the specified object is configured as an interceptor, to intercept the specified target, therefore, through the XML file configuration, rather than annotations, is a more general way.
* In addition to the underlying springframework framework jar package, You also need to rely on the cglib, aspectj jar package, maven Configuration:
1 <Dependency>2 <groupId>Cglib</groupId>3 <Artifactid>Cglib</Artifactid>4 <version>2.2</version>5 </Dependency>6 <Dependency>7 <groupId>Org.aspectj</groupId>8 <Artifactid>Aspectjweaver</Artifactid>9 <version>1.6.11</version>Ten </Dependency>
second, the realization principle
The AOP interception technique in the spring framework is a method-level interception of Pojo. The principle of low-level implementation is actually dynamic agent Technology. For the Interface-oriented method interception, relying on the JDK dynamic agent technology, namely java.lang.reflect.proxy#newproxyinstance, will be the target object of the proxy call, delegate to the proxy object, triggering interception notification, and when the agent is a class object, When it is not an interface, it uses cglib, which dynamically enhances the bytecode and proxies it.
More framework underlying source code and implementation details, to be Continued.
Spring AOP Basic Practice