在Spring中列出Aspect-proxied beans

时间:2017-01-18 11:26:00

标签: java spring-aop

有没有办法让Spring的特定Aspect代理的bean列表?

我们在一些停止工作的bean上有一个方面,我们正在试图弄清楚发生了什么,所以我创建了一个类来在加载后扫描ApplicationContext

@Component
public class AspectScanner implements ApplicationListener<ContextRefreshedEvent> {

    public static final Logger LOGGER = LoggerFactory.getLogger(AspectScanner.class);

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        final ApplicationContext applicationContext = event.getApplicationContext();
        applicationContext.[GET PROXIED BEANS CALL];
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

要识别建议的bean,您应该检查它是否实现org.springframework.aop.framework.Advised并使用org.springframework.aop.aspectj.AspectJPrecedenceInformation#getAspectName来获取方面名称。概念证明代码如下所示。

@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class AspectScanner implements ApplicationListener<ContextRefreshedEvent> {

    public static final Logger LOGGER = LoggerFactory.getLogger(AspectScanner.class);

    public void onApplicationEvent(ContextRefreshedEvent event) {
        final ApplicationContext applicationContext = event.getApplicationContext();
        Map<String, Object> beansOfType = applicationContext.getBeansOfType(Object.class);
        for (Map.Entry<String, Object> entry : beansOfType.entrySet()) {
            boolean advisedWith = isAdvisedWith(applicationContext, entry.getValue(), BeanAspect.class);
            LOGGER.info(entry.getKey() + (advisedWith ? " is advised" : " isn't advised"));
        }
    }

    private static boolean isAdvisedWith(ApplicationContext context, Object bean, Class<?> aspectClass) {
        boolean advisedWith = false;
        HashSet<String> names = new HashSet<>(Arrays.asList(context.getBeanNamesForType(aspectClass)));
        if (bean instanceof Advised) {
            Advisor[] advisors = ((Advised) bean).getAdvisors();
            for (Advisor advisor : advisors) {
                if (advisor instanceof AspectJPrecedenceInformation) {
                    if (names.contains(((AspectJPrecedenceInformation) advisor).getAspectName())) {
                        advisedWith = true;
                    }
                }
            }
        }
        return advisedWith;
    }


    @Aspect
    @Component
    public static class BeanAspect {

        @Before("execution(* test.AspectScanner.Bean*.*(..))")
        public void beforeAny(JoinPoint jp) {

        }

    }

    @Component
    public static class Bean1 {

        public void m() {

        }

    }

    public interface Bean2Intf {

        void m();

    }

    @Component
    public static class Bean2 implements Bean2Intf {

        public void m() {

        }

    }

    @Component
    public static class NotAdvised {

        public void n() {

        }

    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AspectScanner.class);
        context.start();
        context.registerShutdownHook();
    }

}