Get the target class from the spring container, perform AOP configuration, and let spring Create the proxy class. Fully automated process. The implementation process of traditional spring AOP
Pre-work: Add an AOP namespace
Target classes and their interfaces:
Public interface UserService {public void AddUser ();p ublic void UpdateUser ();
public class Userserviceimpl implements UserService {@Overridepublic void AddUser () {System.out.println ("Spring AOP add u Ser ");} @Overridepublic void UpdateUser () {System.out.println ("Spring AOP Update User");}}
Slice class:
public class Myaspect implements methodinterceptor{@Overridepublic Object invoke (methodinvocation mi) throws Throwable { System.out.println ("front"); Object obj = Mi.proceed (); System.out.println ("after"); return obj;}}
The focus of this blog post is on XML configuration:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xsi:sche malocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-bean S.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop.xs D "><!--1 Create target class--><bean id=" Userserviceid "class=" COM.CANYUGAN.C_AOP. Userserviceimpl "></bean><!--2 Create a slice class (notification)--><bean id=" Myaspectid "class=" COM.CANYUGAN.C_AOP. Myaspect "></bean><!--3 Spring Traditional AOP development, make notification reference to target class Pointcut * Add AOP Namespace * AOP programming <aop:config>* <AOP:POINTC Ut> is used to declare pointcuts and to determine which methods on the target class will be enhanced. ID: pointcut name expression: Used to write pointcut expressions (aspectj pointcut expressions) Execution (* com.itheima.c_aop.*.* (..)) Fixed return value type package class name Method name parameter list * <aop:advisor> special facets, only one pointcut and one notification advice-ref: a notification reference pointcut-reF: A pointcut reference--><aop:config><aop:pointcut expression= "Execution (* com.canyugan.c_aop.*.* (..))" Id= " Mypointcut "/><aop:advisor advice-ref=" Myaspectid "pointcut-ref=" Mypointcut "/></aop:config></ Beans>
Traditional Spring AOP