Day23SSM之Spring AOP***

learning target

  • (1)SpringAOP
  • (2) Affairs
  • (3) Spring transaction management
  • (4) Spring and Mybatis integration

Dynamic proxy JDK proxy

(1) The dynamic agent must have an interface com.wzx.demo01
(2) The three parameters of the dynamic agent The
agent class has the same status as the agent class
(the same ClassLoader, the same interface),
so there are the same Interface, the same class loader
(3) invoke method.
Parameter 2 indicates which method to enhance
Object returnValue = method.invoke(laoZong,args);

AOP terminology

1. The target class target: LaoZong.class that we need to enhance
2. Proxy class proxy: is the custom proxy object $Proxy0.class
3. JoinPoint: a specific location where the program is executed, Spring only supports The connection point of the method
eat(), sleep(), run()
4. The cut-in point pointCut: is the method
eat() that is actually enhanced in the target class
5. Weave into weave: is to put the method that needs to be enhanced in the proxy class The process of executing in the target class.
Call the original method together with the methods of other classes.
6. Introduction: Introduction is a special enhancement. It adds some attributes and methods to the class (not used by the course).
7. Advice: Change The result of applying the method in the proxy object to the target class.
8. Aspect: All the entry points and the methods of the proxy object are combined to form the aspect

Preparation for Spring AOP

pom.xml

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

    </dependencies>

iuseris

//1 必须要有一个接口
public interface IUser {
    
    
    void work();
}

UserImpl

//2 类与接口是实现关系
public class UserImpl implements IUser {
    
    
    //连接点
    //切点
    @Override
    public void work() {
    
    
        System.out.println("我加班我快乐");
    }
}

Advice

public class Advice {
    
    
    public void writeLog(){
    
    
        System.out.println("写一个开发备忘录");
    }
}

Xml configuration of Spring AOP

Insert picture description here

applicationContext.xml

<?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">


<!--    创建UserImpl对象 目标类 -->
    <bean id="userImpl" class="com.wzx.service.impl.UserImpl"/>
<!--    创建Advice对象-->
    <bean id="advice" class="com.wzx.service.Advice"/>
<!--    配置他们的增强关系 生成一个代理类-->
    <aop:config >
        <aop:pointcut id="work" expression="execution(public void com.wzx.service.impl.UserImpl.work())"/>
        <!-- 我想在work方法执行 之后调用一下writeLog-->
        <aop:aspect ref="advice">
             <aop:after method="writeLog"  pointcut-ref="work"/>
        </aop:aspect>
    </aop:config>
</beans>

UserImplTest

public class UserImplTest {
    
    

    @Autowired
    IUser iUser;
    @Test
    public void work() {
    
    
        System.out.println(iUser);
        iUser.work();;
    }
}

AOP aspect programming-aspect expression

  • (1) What is the aspect expression
    execution([修饰符] 返回值类型 包.类.方法(参数列表) );
  • (2) What is the use of aspect expressions?
    The method that conforms to the expression will be enhanced.
    Use * to indicate any content.
    Use... Can indicate the classes under the package and sub-package.
    Use... Can be written in the method (...) to indicate any parameter
   <!--    配置他们的增强关系 生成一个代理类-->
    <aop:config >
        <aop:pointcut id="work" expression="execution(* com.wzx.service..*.*(..))"/>
        <!-- 我想在work方法执行 之后调用一下writeLog-->
        <aop:aspect ref="advice">
            <aop:after method="writeLog"  pointcut-ref="work"/>
        </aop:aspect>
    </aop:config>

AOP aspect programming-enhanced method

(1) What are the ways to enhance?
The essence is enhanced four optional call positions
Insert picture description here

(2) Each location has four corresponding names
(3) arround around
simplify
joinPoint.proceed(); //切到原来的目标方法,进行执行

Advice

public class Advice {
    
    
    public void writeLog(){
    
    
        System.out.println("写一个开发备忘录");
    }
    public void before(){
    
    
        System.out.println("before-------");
    }
    public void after(){
    
    
        System.out.println("after-------");
    }
    public void afterReturn(){
    
    
        System.out.println("afterReturn-------");
    }
    public void afterThrow(){
    
    
        System.out.println("afterThrow-------");
    }
}

applicationContext.xml

 <!--    配置他们的增强关系 生成一个代理类-->
    <aop:config >
        <aop:pointcut id="all" expression="execution( * com.wzx.service..*.*(..))"/>
        <!-- 我想在work方法执行 之后调用一下writeLog-->
        <aop:aspect ref="advice">
            <!-- try-->
            <aop:before method="before"  pointcut-ref="all"/>
            <!-- 有可能程序正常运行-->
            <aop:after-returning method="afterReturn"  pointcut-ref="all"/>
            <!-- catch-->
            <aop:after-throwing method="afterThrow"  pointcut-ref="all"/>
            <!-- finally-->
            <aop:after method="after"  pointcut-ref="all"/>

        </aop:aspect>
    </aop:config>

Surround notification

Advice

public void arround(ProceedingJoinPoint point ){
    
    //参目标类中的任意方法
        try{
    
    
            //执行before
            before();
            System.out.println("arround-------");
            point.proceed();//对应你调用的UserImpl中的任意方法
            //执行正常返回
            afterReturn();
        }catch (Throwable e){
    
    
            //补救方法
            afterThrow();
        }finally {
    
    
            //释放资源的方法
            after();
        }
    }

applicationContext.xml

 <!--    配置他们的增强关系 生成一个代理类-->
    <aop:config >
        <aop:pointcut id="all" expression="execution( * com.wzx.service..*.*(..))"/>
        <!-- 我想在work方法执行 之后调用一下writeLog-->
        <aop:aspect ref="advice">

            <aop:around method="arround"  pointcut-ref="all"/>

        </aop:aspect>
    </aop:config>

AOP aspect programming-annotation***

(1) Spring annotation configuration
One xml opens annotation automatic proxy
Several annotations

  • Aspect @Aspect+four annotations @Before @AfterReturning @AfterThrowing@After
  • Aspect @Aspect+@Arround

Advice

@Component
@Aspect
public class Advice {
    
    
    public void writeLog(){
    
    
        System.out.println("写一个开发备忘录");
    }
    //try{  }
   // @Before("execution( * com.wzx.service..*.*(..))")
    public void before(){
    
    
        System.out.println("before-------");
    }
    //@AfterReturning("execution( * com.wzx.service..*.*(..))")
    public void afterReturn(){
    
    
        System.out.println("afterReturn-------");
    }
   //catch(exception e){ }
   // @AfterThrowing("execution( * com.wzx.service..*.*(..))")
    public void afterThrow(){
    
    
        System.out.println("afterThrow-------");
    }
    //finally
   // @After("execution( * com.wzx.service..*.*(..))")
    public void after(){
    
    
        System.out.println("after-------");
    }

    @Around("execution( * com.wzx.service..*.*(..))")
    public void arround(ProceedingJoinPoint point ){
    
    //参目标类中的任意方法
        try{
    
    
            //执行before
            before();
            System.out.println("arround-------");
            point.proceed();
            //执行正常返回
            afterReturn();
        }catch (Throwable e){
    
    
            //补救方法
            afterThrow();
        }finally {
    
    
            //释放资源的方法
            after();
        }
    }
}

applicationContext.xml

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--包扫描创建对象 扫描包与它下面的子包中的所有类-->
    <context:component-scan base-package="com.wzx.service"/>

    <!--开启自动AOP代理-->
    <aop:aspectj-autoproxy/>

</beans>

Guess you like

Origin blog.csdn.net/u013621398/article/details/109021685