plane-oriented programming
AOP thought: Horizontal duplication of code, vertical extraction
AOP embodies--filter
AOP embodied-interceptors
AOP embodies--dynamic agent
Implementing AOP Ideas in spring
Principle: Spring can generate proxy objects for objects managed in a container
Agents are divided into dynamic agents and CGLIB agents:
- Dynamic Agent (priority)
- The proxy object must implement the interface in order to produce a proxy object, if no interface will be able to use dynamic Proxy technology, in other words, the proxy object and the agent to implement the same interface
- Cglib Agent
- Third-party agent technology, Cglib agent, can be generated for any class agent, the principle of the agent is the target object to inherit the proxy, if the target object is final decorated, then the class cannot be Cglib proxy
- Hibernate entities use the Cglib agent
Ranking explanation
Code implementation:
1) Guide Package
4+2
Spring-aop-4.2.4.release.jar
Spring-aspects-4.2.4.release.jar
Com.springsource.org.aopalliance-1.0.0.jar
Com.springsource.org.aspectj.weaver-1.6.8.release.jar
2) preparing interfaces and implementing classes
UserService interface
public interface UserService {void Save (); void Delete (); void update (); void find ();
Userserviceimpl Implementation Class
public class Userserviceimpl implements UserService {@Overridepublic void Save () {System.out.println ("Save user!"); /int i = 1/0;} @Overridepublic void Delete () {System.out.println ("delete user!");} @Overridepublic void Update () {System.out.println ("Update user!");} @Overridepublic void Find () {System.out.println ("Find user!");}}
3) Prepare notification (code to be woven into it)
Notification class public class Myadvice {//Pre-notification public void before () {System.out.println ("This is a pre-notification!");} Post-notification public void afterreturning () {System.out.println ("This is a post-notification (if an exception is not called)!");} Surround notice public Object around (Proceedingjoinpoint pjp) throws Throwable {System.out.println ("This is the part before wrapping the notification!"); O Bject proceed = Pjp.proceed ();//Call the target method System.out.println ("This is part of the surround notification!"); return proceed;} Exception notification public void Afterexception () {System.out.println ("Something's wrong!!!!!");} Post-notification public void after () {System.out.println ("This is a post-notification (an exception will also be called)!");}}
4) configuration for weaving, weaving notifications into target objects
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "Http://www.springframework.org/schema/context" XM lns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/ SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!--1. Configuring target objects--<bean name=" UserService "class=" Cn.x5456.service.impl.UserServiceImpl "></bean> <!--2. Configuring notification objects--<bean name=" Myadvice "class=" Cn.x5456.advice.MyAdvice "></bean> <!--3. Configure the weaving point--<aop:config> <!--ID Any, set the weave in point mode--<aop:pointcut id= "PC" expression= "exEcution (* cn.x5456.service.impl.*serviceimpl.* (..)) " ></aop:pointcut> <!--configuration notification, ref is the class name of the notification--<aop:aspect ref= "Myadvice" > <!--Me Thod corresponds to the method name in the notification object, Pointcut is the id--> <!--of the weaving point named before method as the predecessor notification--<aop:before method= "Befo Re "pointcut-ref=" PC "/> <!--Rear--<aop:after-returning method=" afterreturning "Pointc ut-ref= "PC"/> <!--surround notification-<aop:around method= "Around" pointcut-ref= "PC"/> <!--exception interception notification-<aop:after-throwing method= "afterexception" pointcut-ref= "PC"/> < !--Rear-<aop:after method= "after" pointcut-ref= "PC"/> </aop:aspect> </aop:config ></beans>
5) Test class
public class Testdemo { @Test public void func () { Classpathxmlapplicationcontext ac = new Classpathxmlapplicationcontext ("Spring-config.xml"); UserService UserService = (userservice) ac.getbean ("UserService"); Userservice.save (); }}
java--programming for facets, AOP programming in spring