Spring can be implemented using annotations or XML configuration to implement AOP functionality, which is based on the demo to simulate the process of implementing AOP in XML configuration.
The code structure is as follows
1, spring configuration is as follows, the corresponding tangent pointcut is configured in <aop:config>, and the corresponding Pointcut is referenced in the tangent aspect.
<?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"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 "> <BeanID= "DAO"class= "Com.dao.UserDaoImpl"/> <BeanID= "UserService"class= "Com.service.UserService"> < Propertyname= "DAO"ref= "DAO"></ Property> </Bean> <BeanID= "Loginter"class= "Com.handler.LogInterceptor"/> <Aop:config> <Aop:pointcutexpression= "Execution (* com.service.userservice.* (..))"ID= "Servicepointcut" /> <Aop:aspectID= "LOGAOP"ref= "Loginter"> <Aop:beforeMethod= "Beforeexcute"Pointcut-ref= "Servicepointcut"/> </Aop:aspect> </Aop:config> </Beans>
2. Interception class Loginterceptor
Package Com.handler; Public class Loginterceptor { publicvoid Beforeexcute () { System.out.println (" Aspect execute. " ); }}
3, the interface call is completed by injecting the DAO layer through spring in UserService.
The test classes are as follows:
Packagecom.test;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.service.UserService; Public classtestaopannotation {@Test Public voidtestaopannotation () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring.xml"); UserService Service= (UserService) context.getbean ("UserService"); Service.save (); }}
Test results:
Spring Learning 2_AOP Simple implementation via XML configuration