Aspect-Oriented Programming
Custom aspect
Package CN. csdn. service; import Org. aspectj. lang. proceedingjoinpoint; import Org. aspectj. lang. annotation. after; import Org. aspectj. lang. annotation. afterreturning; import Org. aspectj. lang. annotation. afterthrowing; import Org. aspectj. lang. annotation. around; import Org. aspectj. lang. annotation. aspect; import Org. aspectj. lang. annotation. before; import Org. aspectj. lang. annotation. pointcut;/*** cut **/@ aspectpublic class myinterceptor {@ pointcut ("execution (* CN. itcast. service. impl. personservicebean. *(..)) ") Private void anymethod () {}// declare an entry point @ before (" anymethod () & ARGs (name) ") Public void doaccesscheck (string name) {system. out. println ("pre-notification:" + name) ;}@ afterreturning (pointcut = "anymethod ()", returning = "result") Public void doafterreturning (string result) {system. out. println ("post notification:" + result) ;}@ after ("anymethod ()") Public void doafter () {system. out. println ("final notification") ;}@ afterthrowing (pointcut = "anymethod ()", throwing = "e") Public void doafterthrowing (exception e) {system. out. println ("exception notification:" + E) ;}@ around ("anymethod ()") Public object dobasicprofiling (proceedingjoinpoint pjp) throws throwable {// If () {// determine whether the user has the permission system. out. println ("access method"); object result = pjp. proceed (); system. out. println ("Exit method"); //} return result ;}}
DAO class:
Package CN. csdn. Service; public interface personservice {public void save (string name); Public void Update (string name, integer ID); Public String getpersonname (integer ID );}
Daoimpl class:
Package CN. csdn. service. impl; import CN. csdn. service. personservice; public class personservicebean implements personservice {Public String getpersonname (integer ID) {system. out. println ("I'm getpersonname () method"); Return "XXX";} public void save (string name) {Throw new runtimeexception ("I love exceptions"); // system. out. println ("I am the SAVE () method");} public void Update (string name, integer ID) {system. out. println ("I am the update () method ");}}
Bean. xml configuration file:
<? 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/beanshttp://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 = "myinterceptor" class = "CN. csdn. service. myinterceptor "/> <bean id =" personservice "class =" CN. csdn. service. impl. personservicebean "> </bean> </beans>
Test:
Package JUnit. test; import Org. JUnit. beforeclass; import Org. JUnit. test; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import CN. csdn. service. personservice; public class springaoptest {@ beforeclasspublic static void setupbeforeclass () throws exception {}@ test public void interceptortest () {applicationcontext cxt = new classpathxmlapplicationcontext ("beans. XML "); personservice = (personservice) cxt. getbean ("personservice"); personservice. save ("XX ");}}