Spring - 注解方式配置AOP解析(15)

环境搭建

和xml方式需要导入的包一致,xml方式中的约束是全部约束了,所以这次导入那个就行。

配置

和注解IOC一样,也需要在配置文件中配置信息,让Spring知道AOP使用注解方式进行。

<!-- 打开注解方式的AOP -->
<aop:aspectj-autoproxy />

入门

使用@Aspect@Before@AfterReturning@Around注解

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

<!-- 打开注解方式的AOP -->
<aop:aspectj-autoproxy />

<bean id="userDao" class="com.spring.annotation.UserDaoImpl"></bean>
<bean id="myAspectJ" class="com.spring.annotation.MyAspectJ"></bean>

</beans>
public class UserDaoImpl implements UserDao {
    public void delete() {
        System.out.println("简简单单的删除");
    }

    public Integer delete(String userId, Double type) {
        System.out.println("delete : " + userId);
        System.out.println("delete : " + type);
        return 100;
    }

    public Boolean delete(Double type, String userId) {
        System.out.println("delete : " + userId + " " + type);
        return true;
    }
}
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/*      标识这个类是切面类       */
@Aspect
public class MyAspectJ {

    /**
     * 前置增强,value表达式和xml方式一致
     */
    @Before(value="execution(void com.spring.annotation.UserDaoImpl.delete())")
    public void before() {
        System.out.println("before");
    }

    /**
     * 后置增强,value表达式和xml方式一致
     */
    @AfterReturning(value="execution(Boolean com.spring.annotation.UserDaoImpl.delete(java.lang.Double, java.lang.String))",
            returning="ret")
    public void after(Boolean ret) {
        System.out.println(ret);
        System.out.println("after");
    }

    @Around(value="execution(Integer com.spring.annotation.UserDaoImpl.delete(java.lang.String, java.lang.Double))")
    public Integer around(ProceedingJoinPoint pjp) throws Throwable {

        Object[] args = pjp.getArgs();
        for(Object o : args)
            System.out.println(o);
        //执行切点
        Integer proceed = (Integer)pjp.proceed();
        return proceed;
    }
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao ud = (UserDao)ac.getBean("userDao");
        Integer delete = ud.delete("HelloWorld", 11.0);
        System.out.println(delete);
    }
}

切入点注解

在上面的代码中,把注解加在通知的上面,类似于xml方式的

<aop:aspect ref="myAspectJ">
    <aop:around method="around" pointcut-ref="cut1" />
</aop:aspect>

但是这样的话后期修改很麻烦,所以使用注解完成类似于下面,可以通过id添加通知的操作。

<aop:pointcut expression="execution(Integer com.spring.aopxml.UserDaoImpl.delete(..))" id="cut1"/>
    @Around(value="MyAspectJ.cut1")
    public Integer around(ProceedingJoinPoint pjp) throws Throwable {

        Object[] args = pjp.getArgs();
        for(Object o : args)
            System.out.println(o);
        //执行切点
        Integer proceed = (Integer)pjp.proceed();
        return proceed;
    }

    /*
     * 不能使用属性
     */
    @Pointcut(value="execution(Integer com.spring.annotation.UserDaoImpl.delete(java.lang.String, java.lang.Double))")
    private void cut1() {}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
dynamic-datasource-spring-boot-starter 是一个基于 Spring Boot 的动态数据源库。它提供了在运行时动态切换数据源、动态创建数据源以及多租户的支持。 对于 dynamic-datasource-spring-boot-starter 的源码分析,可以从以下几个方面进行思考: 1. 数据源的自动装配:源码中可能会使用 Spring Boot 的自动配置功能,通过扫描配置类或者注解方式,自动将数据源相关的类和 bean 注入到应用程序中。 2. 动态切换数据源的实现:动态切换数据源是 dynamic-datasource-spring-boot-starter 的核心功能之一。源码中可能会涉及到 AOP、动态代理等技术,通过拦截器或者代理的方式,在运行时根据特定条件切换数据源。 3. 动态创建数据源的实现:动态-datasource-spring-boot-starter 提供了在运行时动态创建数据源的功能。源码中可能会包含一些工厂类或者构建者模式的实现,根据配置信息动态创建数据源实例。 4. 多租户支持的实现:多租户是指一个系统可以同时服务于多个不同的客户或租户。dynamic-datasource-spring-boot-starter 也提供了对多租户的支持。源码中可能会包含一些多租户相关的类和逻辑,如解析请求或者从配置文件中获取租户信息等。 总结来说,dynamic-datasource-spring-boot-starter 源码分析涉及到数据源的自动装配、动态切换数据源的实现、动态创建数据源和多租户支持等方面的内容。可以通过阅读源码来深入了解其实现原理和技术细节,从而更好地使用和定制该库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值