Spring aop is configured in two ways

  • 2020-04-01 04:11:34
  • OfStack

First: annotations configure AOP
Annotations configure AOP (implemented using the AspectJ class library) in roughly three steps:
1. Use the annotation @aspect to define a slice, where the Pointcut (@pointcut) is defined and the notification type (@before, @afterreturning, @after, @afterthrowing, @around) is defined.
2. Develop classes that need to be intercepted.
3. Configure the facets into the XML, of course, we can also use the method of automatically scanning the beans. In this case, it is left to the Spring AoP container to manage.

You also need to reference aspectJ's jar package: aspectjweaver. Jar aspectjrt.jar

Example:


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; 
 } 
} 
 
package com.bjsxt.dao; 
import com.bjsxt.model.User; 
 
 
public interface UserDAO { 
 public void save(User user); 
} 

Implementation 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 exceptions
 } 
 
}

Operation:


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 the 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("myMethod()") 
 public void before() { 
 System.out.println("method staet"); 
 } 
 @After("myMethod()") 
 public void after() { 
 System.out.println("method 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"); 
 } 
} 

The 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 line  --> 
 
 <context:annotation-config /> 
 <context:component-scan base-package="com.bjsxt"/> <!--  Automatic scanning  --> 
 <aop:aspectj-autoproxy/> <!--  To add line  --> 
</beans> 

The 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
###

Note:

@aspect: means that this class is a section class
@componet: because Spring needs to be managed as a section class, this class needs to be initialized into Spring's management at initialization time.
@befoe: logic for pointcuts
Execution... : pointcut syntax

The second: XML configuration aop

The instance is the same as above: only the configuration file is different


<?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 line  --> 
 
 <context:annotation-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> Spring's configuration TAB, beans has several important properties:

XMLNS:

Is the default XML document parsing format, which is spring's beans. The address is http://www.springframework.org/schema/beans.

By setting this property, all the properties declared in the beans can be directly passed through < > To use, for example, < Bean> And so on.

XMLNS: xsi:

Is the specification that the XML needs to follow, as you can see from the URL, which is the uniform specification for w3, followed by xsi:schemaLocation to locate all the parsing files.

XMLNS: aop:

This is the point, some of the semantic specifications we need to use here, related to aspect-oriented AOP.

XMLNS: tx:

Transaction-related configuration content in Spring.

An XML file can only declare a default specification for semantic parsing.

For example, in the XML above, only beans are the default, and the others need to be used by a specific tag, such as aop, which has many attributes of its own. To use it, you must add aop: XXX. Take aop:config above.

Similarly, if the default XMLNS is configured with aop-related semantic parsing specifications, you can write config tags directly in XML.

Those are the two configurations of spring aop. There will be more articles to share about the two configurations of spring aop, so be sure to stay tuned


Related articles: