Find method level custom annotation in a Spring context

前端 未结 3 1656
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 14:03

All I wanted to find out was \"all the class/methods in Spring beans which are annotated as @Versioned\".

I created my custom annotation as,

@Target({Ele         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 14:19

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    
        for (String beanName : applicationContext.getBeanDefinitionNames()) {
            Object obj = applicationContext.getBean(beanName);
            Class objClz = obj.getClass();
            if (org.springframework.aop.support.AopUtils.isAopProxy(obj)) {
                objClz = org.springframework.aop.support.AopUtils.getTargetClass(obj);
            }
    
            for (Method m : objClz.getDeclaredMethods()) {
                if (m.isAnnotationPresent(Transactional.class)) {
                    Transactional transactional = m.getAnnotation(Transactional.class);
                    Class[] value = transactional.rollbackFor();
                    if (value == null){
                       // help !!!
                        // If value is null, I want to set a value for him like Exception.class How can I modify it?
                    }
    
                }
            }
        }
    }
    

提交回复
热议问题