Configuration files are very different from the way they are annotated, with a very large number of configuration items.
Beans2.xml
<?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: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-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean id= "Personservice" class= "Test.spring.service.impl.PersonServiceBean" ></bean>
<bean id= "Myinterceptor" class= "Test.spring.aop.MyInterceptor2" ></bean>
<aop:config>
<aop:aspect id= "Myaspect" ref= "Myinterceptor" >
<aop:pointcut id= "Mypointcut"expression= "Execution (* test.spring.service.impl.personservicebean.* (..))"/>
<Aop:beforepointcut-ref= "Mypointcut" method= "Doaccesscheck"/>
<aop:after-returningpointcut-ref= "Mypointcut" method= "doafterreturning"/>
<aop:after-throwingpointcut-ref= "Mypointcut" method= "doafterthrowing"/>
<Aop:aroundpointcut-ref= "Mypointcut" method= "Doaround"/>
<Aop:afterpointcut-ref= "Mypointcut" method= "Doafter"/>
</aop:aspect>
</aop:config>
</beans>
Pointcut syntax definition for facets
- Intercept all the methods under Test.spring.service.impl.PersonServiceBean
expression= "Execution (* test.spring.service.impl.personservicebean.* (..))"
- All methods of intercepting all classes under the Test.spring.service.impl sub-package
expression= "Execution (* Test.spring.service.impl). *.*(..))"
- Methods that intercept all the return values under Test.spring.service.impl.PersonServiceBean as String types
expression= "Execution (java.lang.String test.spring.service.impl.personservicebean.* (..))"
- A method that intercepts the first parameter type string in all methods under Test.spring.service.impl.PersonServiceBean
expression= "Execution (* test.spring.service.impl.personservicebean.* (java.lang.String,..))"
Package Test.spring.service.impl;import test.spring.service.personservice;//proxy object Implementation target object all interfaces public class Personservicebean implements Personservice {public Personservicebean () {} @Overridepublic void Save (String name) { System.out.println ("Save ()->>" + name), throw new RuntimeException (">>----Define exception----<<");} @Overridepublic String GetResult () {return ' GetResult () ==>> returns the result ";}}
Package Test.spring.aop;import Org.aspectj.lang.proceedingjoinpoint;public class MyInterceptor2 {public void Doaccesscheck () {System.out.println ("Pre-notification-->>");} public void doafterreturning () {System.out.println ("Post-notification-->>"); public void Doafter () {System.out.println ("finally notified");} public void doafterthrowing () {SYSTEM.OUT.PRINTLN ("Exception notification--");} Public Object Doaround (Proceedingjoinpoint pjoinpoint) throws Throwable {System.out.println ("around Notifications");// This assumes that pjoinpoint.proceed () does not run. The methods that are intercepted later do not run and are well suited for Rights Management Object result = Pjoinpoint.proceed (); System.out.println ("Exit"); return result;}}
Package Test.spring.junit;import Org.junit.test;import Org.springframework.context.support.abstractapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Test.spring.service.PersonService; public class AOPTest3 {@Testpublic void Test () {Abstractapplicationcontext Acontext =//new Classpathxmlapplicationcontext ("Beans2.xml"); Personservice Pservice = (personservice) acontext.getbean ("Personservice");p service.save ("LinDL"); Pservice.getresult (); Acontext.close ();}}
Spring (12) implementing AOP Using Spring's XML file configuration