Spring AOP framework program, the most popular language to explain, easy to learn

Chapter VII: AOP technology

Preface:

AOP technology is technology based on dynamic proxy design pattern. Dynamic agent technology division jdk dynamic proxy and proxy dynamic cglib

jdk dynamic proxy features:

(1) class inherited java.lang.reflect.proxy

(2) implements an interface to achieve true class

(3) the relationship between the true class and the proxy class: they have achieved the same interface

cglib dynamic proxy features:

Features:

(1) proxy class is a subclass of true class

Note: The film Main article Understanding AOP programming, as the dynamic agent technology do not do too much introduction, there is a need, you can leave a message, the next article can explain.

7.1: AOP principles outlined

AOP: Aspect Oriented Programming, Aspect Oriented Programming

Traditional forms, web project implementation process:

Disadvantages:

(1) Bad Smell phenomenon , refers to some code logic appeared in an inappropriate level.

For example: There Transaction Code database in the service layer, not normative

Improve:

AOP programming:

In essence, it is the dynamic proxy design pattern

Programming section: In analogy to the procedure specified in the program chain, truncation at the service layer, the tomographic section become here, the service running herein as vertical, operation service can be seen as reinforcing layer.

concept:

(1) Point (cut-off point): said to be enhanced to which method

(2) Advice (notification): represents the additional functionality

(3) Aspect (section): is the proxy object, (+ cut cut point)

(4) Weaving (weaving): Add the operation notified to the tangent point of the process of generating dynamic proxies

 

7.2: AOP implementation process

7.2.1: Lead Pack

1. Date Kokorozashitsutsumi:

   commons-logging.jar

2. Spring core package:

       spring-core.jar

       spring-beans.jar

       spring-context.jar

       spring-expression.jar

3. aop dependent jar package:

       spring-aop.jar

       Aop specific implementation of the Union's aopalliance.jar -aop

7.2.2: service code is used for testing

implement two spring supports dynamic proxies:

(1) If you provide the interface, use jdk dynamic proxy

(2) if no interface to use dynamic proxies cglib

7.2.3: Provide notification

spring in the types of notifications:

(1) Pre-notification: before the tangent point method call operation, MethodBeforeAdvice

(2) post-operation: After cutting out a method of operating point, AfterReturningAdvice

(3) abnormality notification: performed at the tangent point method execution exception occurs, ThrowsAdvice

(4) around advice: contains pre-notification, abnormality notification and the notification rear, MethodInterceptor

public class DemoBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("我是前置通知....");
    }
}

  

7.2.4: Configure the AOP (weaving / dynamic proxies)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置service对象-->
    <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl" />
    <bean id="testService" class="com.bjsxt.service.TestService" />
    <!--配置通知对象-->
    <bean id="demoBefore" class="com.bjsxt.advice.DemoBeforeAdvice" />
    <!--配置织入, 生成动态代理-->
        <-! Audience configuration ->
    <bean the above mentioned id = "Proxy" class = "org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetName" value="userService" />
        <!--配置通知-->
        <property name="interceptorNames">
            <array>
                <value>demoBefore</value>
            </array>
        </property>
    </bean>
    <bean id="testProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetName" value="testService" />
        <property name="interceptorNames">
            <array>
                <value>demoBefore</value>
            </array>
        </property>
    </bean>
</beans>

  

7.2.5: test code

public class TestAOP {
    @Test
    public void testAop2() {
        //cglib动态代理
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        TestService testProxy = context.getBean("testProxy", TestService.class);
        System.out.println(testProxy.getClass().getName());
        System.out.println(testProxy.getClass().getSuperclass().getName());
        testProxy.test();
    }

    @Test
    public void testAop() {
        //jdk动态代理
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("proxy", UserService.class);
        System.out.println(userService.getClass().getName());
        userService.demo();
    }
}

  

 

Guess you like

Origin www.cnblogs.com/ncl-960301-success/p/10993192.html