First, create the project
Project Name: spring100807
Second, add Jar package
Com.springsource.org.aopalliance-1.0.0.jar
Commons-logging.jar
Junit-4.10.jar
Log4j.jar
Spring-aop-3.2.0.release.jar
Spring-beans-3.2.0.release.jar
Spring-context-3.2.0.release.jar
Spring-core-3.2.0.release.jar
Spring-expression-3.2.0.release.jar
Third, add the configuration file
1. Create the Conf directory in your project
/conf
2. Add the core configuration file under the Conf directory
Configuration file name: Applicationcontext.xml
Configuration file Contents:
<?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"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " >
</beans>
Iv. Creating Beans
1. Create an interface and implementation class under SRC
Package Name: CN.JBIT.SPRING100807.AOP
Interface Name: Customerservice.java
Public interface CustomerService {
public void login ();
}
Implementation class Name: Customerserviceimpl.java
public class Customerserviceimpl implements CustomerService {
@Override
public void Login () {
System.out.println ("Login Method");
}
}
2. Create a notification
Package Name: Cn.jbit.spring100807.advice
Notification Name: Mybeforeadvice.java
public class Mybeforeadvice implements Methodbeforeadvice {
@Override
public void before (method, object[] arg1, Object arg2)
Throws Throwable {
Method.invoke (Arg2, arg1);
System.out.println ("Pre-notification");
}
}
3. Add the configuration in the configuration file
<!--First step: Configure the interception target class--
<bean id= "CustomerService" class= "Cn.jbit.spring100807.aop.CustomerServiceImpl" ></bean>
<!--Step Two: Configure Notifications-
<bean id= "Mybeforeadvice" class= "Cn.jbit.spring100807.advice.MyBeforeAdvice" ></bean>
<!--step three: Generate Agent Bean--
<bean id= "Customerproxybean" class= "Org.springframework.aop.framework.ProxyFactoryBean" >
<property name= "proxyinterfaces" value= "Cn.jbit.spring100807.aop.CustomerService" ></property>
<property name= "Interceptornames" value= "Mybeforeadvice" ></property>
<property name= "target" ref= "CustomerService" ></property>
</bean>
V. Testing
1. Create the test catalog in the project
/test
2. Create a package in the test directory
Package Name: Cn.jbit.spring100807.advice
3. Create a test class under a package
Class Name: Proxytest.java
Class content:
public class Proxytest {
@Test
public void Testbefore () {
ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
CustomerService customerservice = (customerservice) context.getbean ("Customerproxybean");
Calling a business method
Customerservice.login ();
}
}
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1561204
spring-front-facing notification