Start by creating a common Java Project in eclipse named Springaop. In order to use the spring annotation approach for aspect-oriented programming, you need to include AOP-related jar packages in the SPRINGAOP project, and Spring AOP requires additional jar packages: Com.springsource.org.aopalliance-1.0.0.jar Com.springsource.org.aspectj.weaver-1.6.8.release.jar Spring-aop-4.2.5.release.jar Spring-aspects-4.2.5.release.jar
In this way, the SPRINGAOP project requires a jar package as follows:
To do AOP programming, we then introduce the AOP namespace in spring's configuration file--beans.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.xsd
http ://www.springframework.org/schema/context
http://www.springframework.org/schema/context/ Spring-context-4.2.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
Http://www.springframework.org/schema /aop/spring-aop-4.2.xsd ">
</beans>
Spring provides two ways to declare facets, and in practice we can choose one of them: to declare a slice based on the way XML is configured. Declares a slice based on the annotation method.
In this paper, we use the second approach for aspect-oriented programming, that is, to declare slices based on spring annotations.
Next, we create a new It.cast.service package under the SRC directory, and under the package, the Personservice interface is created with the following code:
Public interface Personservice {public
void Save (String name);
public void Update (String name, Integer ID);
Public String getpersonname (Integer ID);
}
A new It.cast.service.impl package is created under the SRC directory, and the implementation class--personservicebean.java of the Personservice interface under the package is built with the following code:
public class Personserviceimpl implements Personservice {
@Override public
void Save (String name) {
System.out.println ("I Am the Save () method");
}
@Override public
void Update (String name, Integer ID) {
System.out.println ("I Am the update () method");
}
@Override public
String getpersonname (Integer id) {
System.out.println ("I am Getpersonname () method");
return "XXX";
}
}
Then we're going to create a slice class--myinterceptor.java under the Cn.itcast.service package, and let's write it out in the following steps.
First, declare the entire class as a slice with @aspect annotations:
@Aspect public
class Myinterceptor {...
}
Then use the @pointcut annotation to declare a pointcut.
@Aspect public
class Myinterceptor {
@Pointcut ("Execution (* cn.itcast.service.impl.personserviceimpl.* (..))")
private void Anymethod () {}//declares a pointcut, Anymethod is the pointcut name ...
}
We can use method signatures to write pointcut expressions. The most typical pointcut expression is to match various methods according to the signature of the method: Execution (* cn.itcast.service.impl.personserviceimpl.* (..)) : Matches all methods declared in the Personserviceimpl class. The first * represents an arbitrary modifier and any return value type, and the second * represents any method. Match any number of arguments of any type, and omit the package name if the target class is in the same package as the slice. Execution Public * cn.itcast.service.impl.personserviceimpl.* (..) : Matches all public methods in the Personserviceimpl class. execution public double cn.itcast.service.impl.personserviceimpl.* (..) : Matches all public methods in the Personserviceimpl class that return a value of type double. execution public double cn.itcast.service.impl.personserviceimpl.* (double,..) : Matches the first argument in the Personserviceimpl class to a double type, followed by all public methods with no arguments, and the method returns a type double. execution public double cn.itcast.service.impl.personserviceimpl.* (double, double) : matches the parameter type in the Personserviceimpl class to type double,double and returns all public methods with a value type of double type.
Then declare the pre-notification method.
The forward notification method executes before the target method starts.
@Aspect public
class Myinterceptor {
@Pointcut ("Execution (* cn.itcast.service.impl.personserviceimpl.* (..))")
private void Anymethod () {}//declares a pointcut, Anymethod is the pointcut name
//declares that the method is a pre-notification: Executes the @Before before the target method starts
("Anymethod () ") Public
void Doaccesscheck () {
System.out.println (" Pre-Notification ");
}
}
NOTE: If you declare a class as a slice, you need to put the class into IOC container management .
Next, we should revise the spring configuration file--beans.xml and change it to:
<?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.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/conte Xt/spring-context-4.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema /aop/spring-aop-4.2.xsd "> <aop:aspectj-autoproxy/> <bean id=" myinterceptor "class=" Cn.itcast.service .
Myinterceptor "/> <bean id=" Personservice "class=" Cn.itcast.service.impl.PersonServiceImpl "></bean> </beans>
As you can see from above, we didn't let spring automatically scan and manage beans.
Finally, create a new junit.test package in the SRC directory and create a new unit test class--springaoptest.java in the package with the following code:
public class Springaoptest {
@Test public
void Interceptortest () {
ApplicationContext cxt = new Classpathxmlapplicationcontext ("Beans.xml");
Personservice Personservice = (personservice) cxt.getbean ("Personservice");
Personservice.save ("xxx");
}
}
Test the Interceptortest () method and you will find the Eclipse console printing:
To view the source code, click the spring annotation method to get started with the AOP download.