First, create the project
Project Name: spring101001
Second, add Jar package
1. Create a Lib directory in your project
/lib
2. Add the relevant spring jar package in the Lib directory
--For slicing programming
Com.springsource.org.aopalliance-1.0.0.jar
Commons-logging.jar
Junit-4.10.jar
Log4j.jar
--For slicing programming
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 a 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 a business bean
1. Create a business bean package in the SRC directory
Package Name: Cn.jbit.spring101001.service
2. Create a business bean under a package
Business Bean Name: Bookstore.java
Business Bean Content:
public class Bookstore {
/**
* Enter the book
*/
public void Addbook () {
System.out.println ("Add book information");
}
/**
* Find Books
*/
public void Findbook () {
System.out.println ("Find book Information");
}
}
Bean Enhanced Name: Timeexecute.java
Bean Enhancement Content:
Public interface Timeexecute {
public void IsActive (Boolean flage);
}
Referral Name: Timeclass.java
Introduction Content:
public class Timeclass extends Delegatingintroductioninterceptor implements
Timeexecute {
Private Boolean flage;
@Override
public void IsActive (Boolean flage) {
This.flage = Flage;
}
@Override
Public Object Invoke (Methodinvocation arg0) throws Throwable {
if (flage) {
Long begin = System.currenttimemillis ();
Object object = Super.invoke (arg0);
Long end = System.currenttimemillis ();
System.out.println ("Method execution time milliseconds:" + (End-begin));
return object;
}else{
Return Super.invoke (arg0);
}
}
}
V. Adding configuration information to the core configuration file
<!--being proxied--
<bean id= "Bookstore" class= "Cn.jbit.spring101001.service.BookStore" ></bean>
<!--configuration Enhancements--
<bean id= "Timezengqiang" class= "Cn.jbit.spring101001.service.TimeClass" ></bean>
<!--generate proxy objects--
<bean id= "Proxyobject" class= "Org.springframework.aop.framework.ProxyFactoryBean" >
<!--interface--
<property name= "proxyinterfaces" value= "Cn.jbit.spring101001.service.TimeExecute" ></property>
<!--proxy for classes--
<property name= "Proxytargetclass" value= "true" ></property>
<!--configuration Enhancements--
<property name= "Interceptornames" value= "Timezengqiang" ></property>
<!--agent Targets--
<property name= "target" ref= "bookstore" ></property>
</bean>
VI. Testing
1. Create the test catalog in the project
/test
2. Create a test package in the test directory
Package Name: Cn.jbit.spring101001.service
3. Create a test class in a test package
Test class Name: Introtest.java
Test content:
public class Introtest {
@Test
public void Testyinjie () {
ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
Bookstore Bookstore = (bookstore) Context.getbean ("Proxyobject");
Timeexecute Timeexecute = (timeexecute) bookstore;
Timeexecute.isactive (TRUE);
Bookstore.addbook ();
}
}
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1562120
spring-Referral Notification