Package cn.itcast.spring.aop.xml;
public class Person {
private String pname;
Private Integer pid;
Public String Getpname () {return
pname;
}
public void Setpname (String pname) {
this.pname = pname;
}
Public Integer Getpid () {return
pid;
}
public void Setpid (Integer pid) {
this.pid = pid;
}
}
Package cn.itcast.spring.aop.xml;
Import java.util.List;
Public interface Persondao {public
void Saveperson ();
public void Deleteperon ();
public void Updateperson ();
Public list<person> Findperson ();
}
package cn.itcast.spring.aop.xml;
Import java.util.ArrayList;
Import java.util.List; public class Persondaoimpl implements Persondao {@Override public void Deleteperon () {System.out.println ("delete Pe
Rson ");
@Override public void Saveperson () {System.out.println ("save Person");
@Override public void Updateperson () {System.out.println (' update person ');
@Override public list<person> Findperson () {person person1=new person ();
Person1.setpid (1);
Person1.setpname ("Person1");
Person Person2=new person ();
Person2.setpid (2);
Person2.setpname ("Person2");
List<person> personlist=new arraylist<person> ();
Personlist.add (Person1);
Personlist.add (Person2);
return personlist; }
}
Package cn.itcast.spring.aop.xml;
Import java.util.ArrayList;
Import java.util.List;
Import Org.aspectj.lang.JoinPoint;
Import Org.aspectj.lang.ProceedingJoinPoint;
public class Transaction {public void BeginTransaction () {System.out.println ("BeginTransaction"); /** * @param joinpoint * Joinpoint can obtain some information about the target class and the target method * @param val * The return value of the target method * and <aop:after- Returning values in returning returning= "Val"/> Consistent/public void commit (Joinpoint joinpoint,object val) {//Post notification String me
Thodname=joinpoint.getsignature (). GetName ();
System.out.println (methodname);
int i=1/0;
System.out.println (Joinpoint.gettarget (). GetClass (). GetName ());
System.out.println ("commit");
List<person> list= (arraylist<person>) Val;
for (person p:list) {System.out.println (P.getpname ());
} public void Finallymethod () {System.out.println ("Finallymethod"); } public void Aroundmethod (Proceedingjoinpoint joinpoint) {System.out.println ("Aroundmethod"); try {joinpoint.proceed ();//Call target method of destination class * * * surround notification and the difference between a predecessor notification and a post notification * Surround notification requires manual invocation of the target class's target method, equivalent to a forward and a post notification *
The predecessor and post notifications are the target methods that automatically invoke the target class/catch (Throwable e) {//TODO auto-generated catch block E.printstacktrace ();
} public void Throwingmethod (Throwable except) {System.out.println (Except.getmessage ());
}
}
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "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-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop-2.5.xs D "> <!--1. Target Class 2. Section 3. AOP configuration--> <bean id=" Persondaoimpl class= "Cn.itcast.spr Ing.aop.xml.PersonDaoImpl "></bean> <!--section of the declaration--> <bean id=" Transaction "class=" Cn.itcast.
Spring.aop.xml.Transaction "></bean> <!--AOP configuration--> <aop:config> <!-- Configuration pointcut: ID is the identity of the pointcut expression: An expression execution for pointcuts (Modifiers-pattern? Ret-type-pattern Declaring-type-pattern? Name-pattern (Param-pattern) Throws-pattern?) Modifiers-pattern? Modifier once or once also has no optional public private protected Ret-type-pattern return type must be selected * to represent any type Declaring-type -pattern? Once or once there are no declared types of methods Name-patterm method name type set* All method names beginning with set update* to Upd All the method names at the beginning of the ate param-pattern parameter matching (..) Any number of arguments, any number of types (*,string) for each parameter, two arguments the first is any type, the second is String (string,*,integer) three parameters, One is a string type, the second is any type, and the third is an integer type Throws-pattern? Examples of matching patterns for exceptions: Execution (* cn.itcast.spring.aop.xml.aservice.* (
..)); All the methods under Cn.itcast.spring.aop.xml.AService are execution (public * Cn.itcast.oa ...
*.*(..)) The return value is any type, the modifier is public, all the methods of all classes under the Cn.itcast.oa package and the child package exectuion (* Cn.itcast.oa ... *.update* (*,string)) The return value is any type in the CN.ITCAST.OA packet and the childAll of the parameters that start with update are two, and the first one is--> <aop:pointcut expression= for all methods of the second string of all classes of any type.
Execution (* cn.itcast.spring.aop.xml.persondaoimpl.* (..)) "id= perform"/> <!--configuration section ref: class pointing to the declaration slice
--> <aop:aspect ref= "Transaction" > <!--predecessor notification POINTCUT-REF references a pointcut-->
<aop:before method= "BeginTransaction" pointcut-ref= "perform"/> <!--configuration Post notification: Returning the return value of the target method If an exception is possible in the target method, it is true that the post notification will not execute--> <aop:after-returning method= "commit" pointcut-ref= PE Rform "returning=" val/> <!--The final notification does not get the return value of the target method, regardless of the target method, the final notification performs a release of the resource shutdown connection Put it in the tracking notice.--> <aop:after method= "Finallymethod" pointcut-ref= perform "/>" &l t;! --Surround Notification--> <!--<aop:around method= "Aroundmethod" pointcut-ref= "perform"/>--> <!--exception Notify--> <aop:after-throwing method= "Throwingmethod" pointcut-ref= "perform" throwing= "except"/> <!--principle: When you start the spring container, instantiating the bean that is included in the spring container instantiates the spring container to resolve the <aop:config> in the configuration file, parsing the pointcut expression under Aop:config After parsing it, find out if a class conforms to the pointcut expression in the bean of the spring container if it is compliant create a proxy object for this object the client needs to find a bean based on the ID of the bean if the Bena has a proxy object then returns the generation Rationale object if there is no proxy object This returns the Bean's object--> </aop:aspect> </aop:config> &
Lt;/beans>
Package cn.itcast.spring.aop.xml;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Testaop {
@Test public
void Test () {
ApplicationContext context=new Classpathxmlapplicationcontext ("Cn/itcast/spring/aop/xml/applicationcontext.xml");
Persondao persondao= (Persondao) Context.getbean ("Persondaoimpl");
Persondao.updateperson ();
}