1. Introduction of relevant JAR files: Aspectjrt.jar, Aspectweaver.jar, Cglib-nodep-2.1_3.jar, Commons-logging.jar, Spring.jar
2. Spring Core configuration file *.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 ">
<bean id= "Usermanager" class= "Com.jzg.spring.UserManagerImpl"/>
<!--Facets--
<bean id= "Loginterceptor" class= "Com.jzg.spring.LogInterceptor"/>
<aop:config>
<!--first declare a tangent object, referencing <bean id= "Securityhandler" > The Java class pointed to--
<aop:aspect id = "Securityaspect" ref= "Loginterceptor" >
<!--claim stealing rules here is a way to match the parameterless add prefix of any return value--
<!--<aop:pointcut id= "Addallmethod" expression= "Execution (* add* (..))" />--
<!--declaration Cut-in method
<!--in accordance with the Allmethod entry point, the CheckSecurity () method will be executed once all the methods are executed under the spring package.
<!--Execution (* com.jzg.spring.*.* (..)), here are all the ways to match the spring package--
<aop:pointcut id= "Allmethod" expression= "Execution (* com.jzg.spring.*.* (..))" />
<aop:before method= "Mybeforemethod" pointcut-ref= "Allmethod"/>
<aop:after method= "Myaftermethod" pointcut-ref= "Allmethod"/>
</aop:aspect>
</aop:config>
</beans>
3. Related Java Classes
Package com.jzg.spring;
public class Loginterceptor {
public void Mybeforemethod () {
System.out.println (" Mybeforemethod execution of pre-cut log-----------------");
}
@SuppressWarnings ("unused")
private void Myaftermethod () {
& nbsp System.out.println ("Myaftermethod () after the completion of the execution of the log------------");
}
}
Package com.jzg.spring;
public class usermanagerimpl{
public void Deluser (int userId) {
SYSTEM.OUT.PRINTLN ("----usermanagerimpl.deluser ()---");
}
Public String Finduserbyid (int userId) {
SYSTEM.OUT.PRINTLN ("----usermanagerimpl.finduserbyid ()---");
return "Zhangsan";
}
public void ModifyUser (int userId, string userName, string password) {
SYSTEM.OUT.PRINTLN ("----usermanagerimpl.modifyuser ()---");
}
public void AddUser (string userName, string password) {
System.out.println ("Usermanagerimpl.adduser () Add User execution-----");
}
}
Package test;
Import Org.springframework.beans.factory.BeanFactory;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.jzg.spring.UserManagerImpl;
public class Test {
private static Usermanagerimpl Usermanager;
private static Beanfactory factory;
static{
Factory = new Classpathxmlapplicationcontext ("Beans.xml");
Usermanager = (Usermanagerimpl) factory.getbean ("Usermanager");
}
@org. junit.test
public void Testadd () {
Usermanager.adduser ("Zhang San", "123");
}
@org. junit.test
public void Testdel () {
Usermanager.deluser (0);
}
@org. junit.test
public void Testfind () {
Usermanager.finduserbyid (1);
}
@org. junit.test
public void Testmodify () {
Usermanager.modifyuser (1, "UserName", "password");
}
public static Usermanagerimpl Getusermanager () {
return usermanager;
}
public static void Setusermanager (Usermanagerimpl usermanager) {
Test.usermanager = Usermanager;
}
public static Beanfactory GetFactory () {
return factory;
}
public static void Setfactory (Beanfactory factory) {
Test.factory = Factory;
}
}