1. Introduction of the JAR package
Sprig Framework foundation Package +JUNTIL Integration Pack + Log Pack +AOP package
spring的传统AOP的开发的包
spring-aop-4.2.4.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
aspectJ的开发包
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
2. Introduction of configuration Files (AOP constraints, annotation constraints)
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"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/HTTP Www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> </Beans>
3. Create an implementation class
Public Interface UserService { publicvoid Save (); Public void update (); Public void Delete ();}
@Service (value= "UserService") Public classUserserviceimplImplementsUserService {@Override Public voidSave () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Business layer: Save Customer ..."); } @Override Public voidUpdate () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Business layer: Update Customer ..."); } @Override Public voidDelete () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Business layer: Delete Customer ..."); }}
4. Hosting the implementation class in spring annotation mode
5. Creating a Slice Class
public class Myaspectann { public void log () {System.out.println (" Pre-notification ... " public void arround (Proceedingjoinpoint joinpoint) {System.out.println ( Surround notification 1 ...) SYSTEM.OUT.PRINTLN ( surround notification 2 ...) public void after () {System.out.println (" post-Notification: " }
6. Hosting a slice class in spring
7. Defining the Pointcut for the Slice class method
8. Turn on automatic proxy in the configuration file and scan annotations
<!--- <base-package = "Com.spring.demo1" /> <!---- <AOP: Aspectj-autoproxy/>
9. Writing test Code
@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationContext.xml") Public classDemo1 {@Resource (name= "UserService") PrivateUserService UserService; @Test Public voidm01 () {System.out.println ("======================="); Userservice.save (); System.out.println ("======================="); Userservice.update (); System.out.println ("======================="); Userservice.delete (); System.out.println ("======================="); }}
Effect:
8.Spring "AOP" annotation method