使用 Spring\SpringBoot 中的组件进行代理(Aop)

package com.jay.prj.mybaits;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Role;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.SQLException;

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
        DataSource bean = (DataSource) run.getBean("dataSource2");
        try {
            System.out.println(bean.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Bean(name = "dataSource2")
    @Role(BeanDefinition.ROLE_SUPPORT)
    public DataSource dataSource() {
        return new PooledDataSource();
    }

    
    /**
     * 代理条件
     */
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public DefaultBeanFactoryPointcutAdvisor transactionAdvisor() {
        DefaultBeanFactoryPointcutAdvisor advisor = new DefaultBeanFactoryPointcutAdvisor();
        advisor.setPointcut(staticMethodMatcherPointcut());
        advisor.setAdvice(transactionInterceptor());
        return advisor;
    }

    /**
     * 代理条件判断
     * @return
     */
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public StaticMethodMatcherPointcut staticMethodMatcherPointcut() {
        return new StaticMethodMatcherPointcut() {
            public boolean matches(Method method, Class<?> aClass) {
                return DataSource.class.isAssignableFrom(aClass) && method.getName().equals("getConnection");
            }
        };
    }

    /**
     * 函数代理逻辑
     */
    public MethodInterceptor transactionInterceptor() {
        return new MethodInterceptor() {
            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
                System.out.println("方法运行前");
                return null;
            }
        };
    }


}

执行结果:

2021-06-11 11:11:14.820  INFO 10776 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 2998 (http)
2021-06-11 11:11:14.826  INFO 10776 --- [           main] com.jay.prj.mybaits.Application          : Started Application in 4.8 seconds (JVM running for 5.775)
org.apache.ibatis.datasource.pooled.PooledDataSource@1690e14
方法运行前
null
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值