First, create the project
Project Name: spring101002
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.spring101002.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 {
public void Save () {
System.out.println ("Add User");
}
}
Five. Creating slices
1) Create the package under SRC
Package Name: Cn.jbit.spring101002.aspect
2) Create a custom slice class under the package
Slice Name: Annotationaspect.java
Slice content:
/**
* Custom Facets
* @author Administrator
*
*/
@Aspect
public class Annotationaspect {
/**
* Front-facing notification
*/
@Before ("Execution (* cn.jbit.spring101002.service.userservice.* (..))")
public void befor () {
System.out.println ("Pre-notification");
}
}
Vi. Adding configuration information to the core configuration file
<!--configured by proxy class--
<bean id= "UserService" class= "Cn.jbit.spring101002.service.UserService" ></bean>
<!--configuring Facets--
<bean id= "Annotationaspect" class= "Cn.jbit.spring101002.aspect.AnnotationAspect" ></bean>
<!--Configure Auto-agent--
<aop:aspectj-autoproxy/>
Seven, testing
1. Create the test catalog in the project
/test
2. Create a test package in the test directory
Package Name: Cn.jbit.spring101002.aspect
3. Create a test class in a test package
Test class Name: Annotationaspecttest.java
Test content:
/**
* Test class
* @author Administrator
*
*/
public class Annotationaspecttest {
/**
* Test the Pre-notification
*/
@Test
public void Testbeforeadvice () {
Read and load the configuration file
ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
Get Bean object based on Bean ID
UserService UserService = (userservice) context.getbean ("UserService");
Invoke Save User method
Userservice.save ();
}
}
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1562136
SPRING-ASPECTJ front-facing notification