Spring road three ways of learning and realization of 3-AOP AOP's

AOP

Aspect-oriented programming (Aspect-oriented programming, AOP, also translated as aspect-oriented programming, profile-oriented programming) is a program in computer science design, aimed at cross-cutting concerns with the business subject for further separation to improve the program code modularity. (The Advice) mechanism, and capable of unified management of decoration is declared as "tangent point (Pointcut)" code block by adding additional notification on the basis of the existing code, such as "names of all methods to 'set *' beginning method to add the backlog. " The idea allows developers to code for the core business logic relationships with not-so-close function (such as logging) was added to the program, without compromising the readability of the code of business. Aspect-oriented programming idea is the basis for software development section.

Maven relies

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.5</version>
</dependency>

Implementation of a: Native Spring API Interface

public interface UserService {
    void add();
    int delete();
    void query();
    int update();
}

public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("function:add()");
    }

    @Override
    public int delete() {
        System.out.println("function:delete()");
        return 0;
    }

    @Override
    public void query() {
        System.out.println("function:query()");
    }

    @Override
    public int update() {
        System.out.println("function:update()");
        return 0;
    }
}

//方法执行前的log
public class BeforeLog implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {

        System.err.println("method before log" + target.getClass().getName() + method.getName());
    }
}

//方法执行后的log
public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.err.println("method before log" + target.getClass().getName() + "-->" + method.getName() + "---return:" + returnValue);
    }
}
<?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: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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean id="log1" class="com.youzi.log.BeforeLog"/>
    <bean id="log2" class="com.youzi.log.AfterLog"/>
    <bean id="userservice" class="com.youzi.service.UserServiceImpl"/>

    <!--配置AOP-->
    <aop:config>
        <aop:pointcut id="pointcut1" expression="execution(int com.youzi.service.UserServiceImpl.*(..))"/>
        <aop:pointcut id="pointcut2" expression="execution(void com.youzi.service.*.add(..))"/>
        <aop:advisor advice-ref="log1" pointcut-ref="pointcut1"/>
        <aop:advisor advice-ref="log2" pointcut-ref="pointcut2"/>
    </aop:config>
</beans>

execution() Point cut function syntax:

execution(返回类型 包名.类名.方法名(参数类型))
  • It may also represent all types with an asterisk.

  • * Class name may be used instead of all packets represented by the following classes

  • * (..): This asterisk indicates the method name, an asterisk indicates that all methods, method parameters are in parentheses, two points representing any parameters.

Implementation II: Custom Class

Although a first implementation realized before and after the adding method or a method of logging, but each function can achieve a need to implement an interface, you can use these methods in a class written by the following method

public class MethodLog {
    public void beforeMethod() {
        System.out.println("方法执行前");
    }

    public void afterMethod() {
        System.out.println("方法执行后");
    }
}
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="MethodLog" class="com.youzi.log.MethodLog"/>
    <bean id="userservice" class="com.youzi.service.UserServiceImpl"/>

    <aop:config>
        <aop:aspect ref="MethodLog">
            <aop:pointcut id="poindcut1" expression="execution(* com.youzi.service.*.*(..))"/>
            <aop:before method="beforeMethod" pointcut-ref="poindcut1"/>
            <aop:after method="afterMethod" pointcut-ref="poindcut1"/>
        </aop:aspect>
    </aop:config>
</beans>

Although the simplified method of the above information code, but now it seems not the same as in the first way by reflecting the execution of the method taking

Implementation 3: Use annotations

<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="MethodLog" class="com.youzi.log.MethodLog"/>
    <bean id="userservice" class="com.youzi.service.UserServiceImpl"/>
    <!--在xml中声明使用注解的方式-->
    <aop:aspectj-autoproxy/>
</beans>
@Aspect 
public class MethodLog {
    @Before("execution(* com.youzi.service.UserServiceImpl.*(..))")
    public void beforeMethod() {
        System.out.println("方法执行前1");
    }

    @After("execution(* com.youzi.service.UserServiceImpl.*(..))")
    public void afterMethod() {
        System.out.println("方法执行后1");
    }

    @Around("execution(* com.youzi.service.UserServiceImpl.update(..))")
    public int aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        System.err.println("环绕前");
        System.err.println(joinPoint.getSignature());
        Object proceed = joinPoint.proceed();
        System.err.println("环绕后");
        return Integer.valueOf((Integer) proceed);
    }

}

Unified test code

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userservice", UserService.class);
userService.query();
userService.add();
userService.delete();
userService.update();

Guess you like

Origin www.cnblogs.com/wangjr1994/p/12528806.html