Spring AOP配置

AOP:面向切面编程

功能:在不改变原有代码的基础之上,通过Spring为原有代码做功能增强。降低业务逻辑耦合度,提高程序可用性。

配置流程:

(1)添加Maven依赖

(2)写需要添加增强功能的业务代码

(3)写增强类

(4)调整配置文件app.xml

(5)使用

具体过程:

(1)依赖(AOP+aspectj)

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>

(2)业务代码

public class UserController {
    public void fun(){
        System.out.println("公司的原古代码……");
    }
}

(3)增强类

//放置所有的增强功能
@Component
@Aspect
public class BDAdvice {

    @Before("execution(public void com.zparkep.controller.UserController.fun())")
    public void before(){
        System.out.println("before advice");
    }

}

(4)调整配置文件spring.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.zparkep"/>

    <aop:aspectj-autoproxy/>

</beans>

(5)使用

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:app.xml")
public class AdviceTest {

    //对UserController进行增强测试
    @Autowired
    private UserController userController;
    @Test
    public void fun(){
        userController.fun();
    }
  
}

结果:

成功

 

注:@Before("execution(public void com.zparkep.controller.UserController.fun())")表示对public类型的返回值为void的 com.zparkep.controller包下的UserController类下的fun方法做前置增强。

设对public类型的返回值为String的 com.zparkep.controller包下的所有类下的所有方法【参数不确定】做前置增强,则注解为@Before("execution(public String com.zparkep.controller.*.*(..))")

 

若多个增强方法对具有同一规则的方法做增强,根据DRY原则,需要提取规则,可使用@Pointcut()注解,举例。

为UserController类下的所有方法添加前置增强和后置增强:

@Aspect
@Component
@Log
public class BDadvice {
    @Pointcut("execution(public void com.zparkep.controller.UserController.*())")
    public void fun(){

    }

    @After("fun())")
    public void after(){
        log.info("after advice");
    }

    @Before("fun()")
    public void before(){
        System.out.println("before advice");
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值