Specific cases on GitHub, mostly jar packages on top
Https://github.com/guoyansi/spring-aop-example
Knights.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-bea Ns.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring- Context.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc- 3.2.xsd http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/spring-tx. xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <BeanID= "Knight"class= "P1." Braveknight "> <Constructor-argref= "Quest" /> </Bean> <BeanID= "Quest"class= "P1." Slaydragonquest "></Bean> <BeanID= "Minstrel"class= "P1." Minstrel "></Bean> <Aop:config> <Aop:aspectref= "Minstrel"> <Aop:pointcutID= "Embark"expression= "Execution (* *.embarkonquest (..))" /> <Aop:beforePointcut-ref= "Embark"Method= "Singbeforequest"/> <Aop:afterPointcut-ref= "Embark"Method= "Singafterquest"/> </Aop:aspect> </Aop:config> </Beans>
Iquest.java
Package P1; /** */Publicinterface IQuest {void embark ();}
Iknight.java
Package P1; Public Interface iknight { void embarkonquest ();}
Braveknight.java
Package P1; /** */Publicclassimplements iknight{Private IQuest Quest; Public Braveknight (IQuest Quest) { this.quest=Quest; } @Override publicvoid embarkonquest () { quest.embark (); } }
Slaydragonquest.java
Package P1; Public class Implements iquest{ @Override publicvoid embark () { System.out.println (" Slaydragonquest embark ... "); }
Minstrel.java
package P1; public class Minstrel { void Singbeforequest () {System.out.println (" Before adventure ... "); public void Singafterquest () {System.out.println ( "After adventure ..."
package P1; import Org.springframework.context.ApplicationContext; import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Run { public static void Main (string[] args) {applicationcontext context =new classpathxmlapplicationcontext (" Classpath*:knights.xml "); Iknight Knight = (iknight) Context.getbean ("Knight" ); Knight.embarkonquest (); }}
To execute the result of run:
The above is a complete example. If there is no Commons-logging.jar
The red lettering on the console will not output, and there will be exceptions.
If there is no Aopalliance.jar:
nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
If there is no Aspectjweaver.jar
nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
SPRING-AOP Example