Spring aop

        Pointcut(切入点):被增强的方法
        Advice(通知/ 增强):封装增强业务逻辑的方法
        Aspect(切面):切点+通知
        Weaving(织入):将切点与通知结合的过程

 

1、 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tuy</groupId>
    <artifactId>spring_aop</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>

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

    </dependencies>

</project>

2、目标类

package com.tuy.aop;

public interface TargetInterface {
    public void save();
}
package com.tuy.aop;

//目标类
public class Target  implements TargetInterface{
    public void save() {
        System.out.println("save running");
    }
}

3、 切面类

package com.tuy.aop;

//切面类,  有增强的方法
public class MyAspect {
    public  void before()
    {
        System.out.println("前置增强......");
    }
    
    public void afterReturning(){
        System.out.println("后置增强..........");
    }

    //Proceeding JoinPoint:  正在执行的连接点===切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强....");
        Object proceed = pjp.proceed();//切点方法
        System.out.println("环绕后增强....");
        return proceed;
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强..........");
    }

    public void after(){
        System.out.println("最终增强..........");
    }

}

4、applicationContex.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:p="http://www.springframework.org/schema/p"
       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="target" class="com.tuy.aop.Target" ></bean>

<!--    切面对象-->
<bean id="myAspect" class="com.tuy.aop.MyAspect" ></bean>


<!--配置织入,告诉spring框架 那些方法(切点)需要进行哪些增强(前置、后置)-->
<aop:config>
    <!--声明切面 ,告诉spring谁是切面-->
    <aop:aspect ref="myAspect" >

 <!--抽取切点表达式-->
            <aop:pointcut id="myPointcut" expression="execution(* com.tuy.aop.*.*(..))"></aop:pointcut>

        <!--切面就是 切点 + 通知(增强)        -->
    <!--pointcut的值是切点表达式      -->
     <!--  pointcut="execution(*  com.tuy.aop.*.*(..))"  表示com.tuy.aop这个包下的任意类下的方法都可以得到增强   -->
        <aop:before method="before" pointcut="execution(public void com.tuy.aop.Target.save())"></aop:before>

<aop:after-returning method="afterReturning" pointcut="execution(* com.tuy.aop.*.*(..))"/>

<!-- 环绕通知  -->
<!-- <aop:around method="around" pointcut-ref="myPointcut"/>  -->
<!-- 异常抛出通知  -->
<aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut"/>

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

5、测试类

package com.tuy.test;

import com.tuy.aop.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

    @Autowired
    private TargetInterface target;

    @Test
    public void test1()
    {
        target.save();
    }

}

 

 //------------------------

使用注解来配置

package com.tuy.anno;

import org.springframework.stereotype.Component;

//目标类
@Component("target")      //括号里面是名字   @Component把对象创建权交给spring容器
public class Target  implements TargetInterface {
    public void save() {
        System.out.println("save running");
    }
}
package com.tuy.anno;

public interface TargetInterface {
    public void save();
}
package com.tuy.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//切面类,  有增强的方法
@Component("myAspect")
@Aspect        //标注当前MyAspect是一个切面类
public class MyAspect {

    //配置前置通知
    @Before("execution(* com.tuy.anno.*.*(..))")
    public  void before()
    {
        System.out.println("前置增强......");
    }

    public void afterReturning(){
        System.out.println("后置增强..........");
    }

    //Proceeding JoinPoint:  正在执行的连接点===切点
   // @Around("execution(* com.tuy.anno.*.*(..))")      //环绕注解
    //@Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强....");
        Object proceed = pjp.proceed();//切点方法
        System.out.println("环绕后增强....");
        return proceed;
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强..........");
    }

   // @After("execution(* com.tuy.anno.*.*(..))")
    @After("MyAspect.pointcut()")
    public void after(){
        System.out.println("最终增强..........");
    }


    //定义一个切点表达式   (把切点表达式抽取出来)
    @Pointcut("execution(* com.tuy.anno.*.*(..))")
    public void pointcut(){}



}

   applicationContext-anno.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">


    <!--    组件扫描-->
    <context:component-scan base-package="com.tuy.anno" />

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

</beans>

测试

package com.tuy.test;

import com.tuy.anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AopAnnoTest {

    @Autowired
    private TargetInterface target;

    @Test
    public void test1()
    {
        target.save();
    }
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值