Java framework articles---two configuration methods of spring aop (1)

Reprinted in: http://www.itxuexiwang.com/a/shujukujishu/2016/0206/81.html
The first type: Annotation configuration AOP
Annotation configuration AOP (implemented using AspectJ class library), roughly divided into three steps:
1 . Use the annotation @Aspect to define an aspect, define the pointcut (@Pointcut), and the notification type (@Before, @AfterReturning, @After, @AfterThrowing, @Around) in the aspect.
2. Develop classes that need to be intercepted.
3. Configure the aspect into xml, of course, we can also use the method of automatic bean scanning. In this case, it is managed by the Spring AoP container.

In addition, you need to refer to the jar package of aspectJ: aspectjweaver.jar aspectjrt.jar

instance:

User.java



package com.bjsxt.model;

#p#page title#e#public class User {
    private String username;
    private String password;
    public String getUsername( ) {
        return username;
#p#page title#e# }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return#p#page title#e# password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}
#p#page title#e#
/**
*Interface class
*/
package com.bjsxt.dao;
import com.bjsxt.model.User;
#p#page title#e#

public interface UserDAO {
    public void save(User user);
}


Implement interface:

#p#page title# e#
package com.bjsxt.dao.impl;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import#p#page title#e# com.bjsxt.model.User;

@Component("u")
public class UserDAOImpl implements UserDAO {

    public#p#page title#e# void save(User user) {
        
        System.out.println( "user save11d!");
        /*throw new RuntimeException("exception");*/ //Throw exception
    }

#p#Page title#e#}
Operation class:

package com.bjsxt.service;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
#p#page header#e#import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.bjsxt.dao. UserDAO;
import com.bjsxt.model.User;

#p#page title#e#
@Component("userService")
public class UserService {
    
    private UserDAO userDAO;  
    
#p#page title#e# public void init() {
        System.out.println("init");
    }
    
    public void add(User user) {
        userDAO. save(user); #p#page title#e#
    }
    public UserDAO getUserDAO() {
        return userDAO;
    }
    
    @Resource(name=#p#page title#e#"u")
    public void setUserDAO( UserDAO userDAO) {
        this.userDAO = userDAO;
    }
   
    public void#p#Paging title#e# destroy() {
        System.out.println("destroy");
    }
}


Add aop

package com.bjsxt.aop;
#p#分页标题#e#
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; #p#分页标题#e#
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
#p#分页标题#e#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))")*/#p#分页标题#e#
    @Before("myMethod()")
    public void before() {
        System.out.println("method staet");
    } 
    #p#分页标题#e#@After("myMethod()")
    public void after() {
        System.out.println("method after");
    } 
    @AfterReturning("execution(public * com.bjsxt.dao..*.*(..))")
#p#分页标题#e#    public void AfterReturning() {
        System.out.println("method AfterReturning");
    } 
    @AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))")
    public void AfterThrowing() { #p#分页标题#e#
        System.out.println("method AfterThrowing");
    } 
}
配置文件

<?xml version="1.0"encoding="UTF-8"?>
#p#分页标题#e#<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
#p#分页标题#e#           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 #p#Pagination title#e#
           "><!-- To add the last 2 lines -->
           
    <context:annotation-config />
    <context:component-scan base-package="com.bjsxt"/> <!-- autoscan -->
    <aop :aspectj-autoproxy/> <!-- To add this line -->
#p#Pagination title#e#</beans>
Test class:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework. context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; #p#page header#e#

import com.bjsxt.model.User;

//Dependency Injection
//Inverse of Control
public#p#page header#e# class UserServiceTest {

    @Test
    public void testAdd() throws Exception {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); #p#page title#e#
        
        
        UserService service = (UserService)ctx.getBean("userService");
        System.out.println(service.getClass());
        service. add(new User());
        System.out.println("###"#p#Pagination title#e#);
        
        ctx.destroy();
        
    }

}
Result:

class com.bjsxt.service.UserService$$EnhancerByCGLIB$ $7b201784 method staet user save11d! method AfterReturning method after ###

#p#Pagination title#e# Note:

@Aspect: means that this class is an aspect class @Componet: because it needs to be managed by Spring as an aspect class, it is This class needs to be initialized and added to Spring's management; @Befoe: the logic of the pointcut (Advice) execution...: the pointcut syntax

The second : the xml configuration aop

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"
#p#分页标题#e#       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
#p#分页标题#e#           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  
           "><!-- 要添加最后2行 --> #p#分页标题#e#
           
    <context:annotation-config />
    <context:component-scan base-package="com.bjsxt"/>
    <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
    <aop:config> #p#分页标题#e#
        <aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))" 
        id="servicePointcut"/>
        <aop:aspect id="logAspect" ref="logInterceptor">
            <aop:before method="before"  pointcut-ref="servicePointcut" />
#p#分页标题#e#        </aop:aspect> The <beans> below is the configuration tag of Spring. There are several important properties in beans: </beans>
        
    </aop:config>





xmlns:

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

By setting this property, all properties declared in beans can be used directly through <>, such as <bean> and so on.

xmlns:xsi:

It is the specification that xml needs to abide by. You can see through the URL that it is the unified specification of w3, and then use xsi:schemaLocation to locate all parsed files.

xmlns:aop:

This is the key point. It is some semantic specifications that we need to use here, which is related to aspect-oriented AOP. #p#Pagination title#e#

xmlns:tx:

Transaction-related configuration content in Spring.

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

For example, in the above xml, only one of beans is the default, and the others need to be used through specific tags, such as aop, which has many attributes. If you want to use it, you must add aop:xxx in front. Such as the above aop:config.

Similarly, if the default xmlns is configured with aop-related semantic parsing specifications, then the config tag can be directly written in xml.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327089431&siteId=291194637