First, create the project
Project Name: spring101003
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 ASPECTJ
Com.springsource.org.aspectj.weaver-1.6.8.release.jar
Spring-aspects-3.2.0.release.jar
--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.spring101003.service
2. Create a business bean under a package
Business Bean Name: Userservice.java
Business Bean Content:
/**
* By proxy class
* @author Administrator
*
*/
public class UserService {
/**
* 1. Save
*/
public void Save () {
System.out.println ("Save Method");
}
}
Five. Creating slices
1) Create the package under SRC
Package Name: Cn.jbit.spring101003.aspect
2) Create a custom slice class under the package
Slice Name: Myaspect.java
Slice content:
/**
* Custom Facets
* @author Administrator
*
*/
@Aspect
public class Myaspect {
/**
* 1. Front-facing notification
*/
public void before () {
SYSTEM.OUT.PRINTLN ("XML pre-notification");
}
}
Vi. Adding configuration information to the core configuration file
<!--XML--
<!--configuration Target class--
<bean id= "UserService" class= "Cn.jbit.spring101003.service.UserService" ></bean>
<!--configuring Facets--
<bean id= "Myaspect" class= "Cn.jbit.spring101003.aspect.MyAspect" ></bean>
<!--using facets--
<aop:config proxy-target-class= "false" >
<!--reference Slices--
<aop:aspect ref= "Myaspect" >
<!--ready for tangency--
<aop:pointcut expression= "Execution (* cn.jbit.spring101003.service.userservice.* (..))" id= "Mypintcut"/>
<!--1. Front-facing notifications-
<aop:before method= "Before" pointcut-ref= "Mypintcut"/>
<!--5. Final Notice--
</aop:aspect>
</aop:config>
Seven, testing
1. Create the test catalog in the project
/test
2. Create a test package in the test directory
Package Name: Cn.jbit.spring101003.aspect
3. Create a test class in a test package
Test class Name: Myaspecttest.java
Test content:
/**
* Test class
* @author Administrator
*
*/
public class myaspecttest{
/**
* 1. Test the pre-notification
*/
@Test
public void Testbeforeadvice () {
Load configuration file
ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
Get an object based on Bean ID
UserService UserService = (userservice) context.getbean ("UserService");
Invoke Save method
Userservice.save ();
}
}
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1562147
Spring-xml version ASPECTJ pre-notification