First: Annotation configuration AOP
The annotation configuration AOP (implemented using the AspectJ class library) is roughly divided into three steps:
1. Use annotation @aspect to define a slice, define pointcuts in the section (@Pointcut), notification types (@Before, @AfterReturning, @After, @AfterThrowing, @Around).
2. Develop classes that need to be intercepted.
3. Configure the slice into XML, and of course we can use the way to scan the bean automatically. In that case, it is managed by the Spring AOP container.
Additionally, you need to refer to the AspectJ jar package: Aspectjweaver.jar Aspectjrt.jar
Instance:
User.java
package Com.bjsxt.model;
public class User {
private String username;
private String password;
Public String GetUserName () {return
username;
}
public void Setusername (String username) {
this.username = username;
}
Public String GetPassword () {return
password;
}
public void SetPassword (String password) {
this.password = password;
}
}
/**
* Interface class * *
package Com.bjsxt.dao;
Import Com.bjsxt.model.User;
Public interface Userdao {public
void Save (user user);
}
To implement an interface:
Package Com.bjsxt.dao.impl;
Import org.springframework.stereotype.Component;
Import Com.bjsxt.dao.UserDAO;
Import Com.bjsxt.model.User;
@Component ("U") public
class Userdaoimpl implements Userdao {public
void Save (user user) {
System.out.println ("User save11d!");
/*throw New RuntimeException ("exception"); *///Throw exception
}
}
Action class:
Package com.bjsxt.service;
Import Javax.annotation.Resource;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Qualifier;
Import org.springframework.stereotype.Component;
Import Com.bjsxt.dao.UserDAO;
Import Com.bjsxt.model.User;
@Component ("UserService") public
class UserService {
private Userdao Userdao;
public void init () {
System.out.println ("Init");
}
public void Add (user user) {
userdao.save (user);
}
Public Userdao Getuserdao () {return
Userdao;
}
@Resource (name= "U") public
void Setuserdao (Userdao userdao) {
This.userdao = Userdao;
}
public void Destroy () {
System.out.println ("destroy");
}
Join AOP
Package COM.BJSXT.AOP;
Import Org.aspectj.lang.annotation.After;
Import org.aspectj.lang.annotation.AfterReturning;
Import org.aspectj.lang.annotation.AfterThrowing;
Import Org.aspectj.lang.annotation.Aspect;
Import Org.aspectj.lang.annotation.Before;
Import Org.aspectj.lang.annotation.Pointcut;
Import org.springframework.stereotype.Component; @Aspect @Component public class Loginterceptor {@Pointcut (execution) (Public * Com.bjsxt.service ...
*.add (..)) ")
public void MyMethod () {}; /* @Before ("Execution public void Com.bjsxt.dao.impl.UserDAOImpl.save (Com.bjsxt.model.User)") */@Before ("MyMethod
() ") public void before () {System.out.println (" method Staet ");
@After ("MyMethod ()") public void After () {System.out.println (' after '); @AfterReturning (Execution (public * Com.bjsxt.dao ...).
*.*(..))")
public void afterreturning () {System.out.println (' method afterreturning '); @AfterThrowing (Execution (public * Com.bjsxt.dao ...).
*.*(..))") Public void Afterthrowing () {System.out.println ("method afterthrowing");
}
}
Configuration file
<?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-2.5.xsd
Http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/ Spring-context-2.5.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
http://www.springframework.org/ Schema/aop/spring-aop-3.1.xsd
><!--to add the last 2 lines-->
<context:annotation-config/>
< Context:component-scan base-package= "Com.bjsxt"/> <!--automatic scanning-->
<aop:aspectj-autoproxy/> <!- -To add the bank-->
</beans>
Test class:
Package com.bjsxt.service;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.bjsxt.model.User;
Dependency injection//inverse of control public
class Userservicetest {
@Test public
void Testadd () Throws Exception {
Classpathxmlapplicationcontext ctx = new Classpathxmlapplicationcontext (" Applicationcontext.xml ");
UserService service = (userservice) ctx.getbean ("UserService");
System.out.println (Service.getclass ());
Service.add (New User ());
System.out.println ("###");
Ctx.destroy ();
}
Results:
Class com.bjsxt.service.userservice$ $EnhancerByCGLIB $$7b201784 method
staet
user save11d!
Method Afterreturning method after
###
Attention:
@Aspect: It means this class is slice
@Componet: Because spring is needed to be managed as a slice class, it is necessary to initialize this class to join spring's administration when initializing;
@Befoe: The logic of the Pointcut (Advice)
Execution ...: pointcut syntax
The second type: XML configuration AOP
Instances above: Just different configuration files
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-2.5.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/ SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.1.xsd "><!--to add the last 2 lines--> <context:an Notation-config/> <context:component-scan base-package= "com.bjsxt"/> <bean id= "LogInterceptor" class= " Com.bjsxt.aop.LogInterceptor "></bean> <aop:config> <aop:pointcut expression=" Execution (public * Com.bjsxt.service..
*.add (..)) " Id= "Servicepointcut"/> <aop:asPect id= "Logaspect" ref= "Loginterceptor" > <aop:before method= "before" pointcut-ref= "Servicepointcut"/> <
/aop:aspect> </aop:config> </beans>
The following <beans> is the Configuration tab for spring, with several important attributes in beans:
xmlns
is the default XML document parsing format, the beans of spring. The address is Http://www.springframework.org/schema/beans.
By setting this property, all the attributes declared in the beans can be used directly through <>, such as <bean> and so on.
Xmlns:xsi:
Is the specification that XML needs to observe, through the URL can see, is the W3 uniform specification, later through xsi:schemalocation to locate all parsing files.
XMLNS:AOP:
This is the point, and some of the semantic specifications we need to use here are related to facet AOP.
XMLNS:TX:
The configuration content associated with the transaction in spring.
An XML file that declares only a specification of the default semantic parsing.
For example, in the XML above, only the beans one is the default, others need to use a specific tag, such as AOP, it has a lot of properties, if you want to use, the front must add aop:xxx can. Like the aop:config above.
Similarly, if the default xmlns configuration is AOP-related semantic parsing specifications, then you can write the config tag directly in XML.
This is the two configuration of spring AOP, you know? Then there are more articles on spring AOP two ways to configure and share with you, and be sure to keep your eye on it.