spring boot啟動掃描不到自定義註解

shop000發表於2018-04-12

對於自定義註解這裡就不嘮叨了,百度一大堆,這裡有我一個自定義註解

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface MsgEvent {
    RetailOrderEvent msgEvent();
}

註解實現類

@Component
public class MsgEventProcessor implements BeanPostProcessor {
    /**
     * 事件訊息註解與例項Bean的對映物件
     */
    public static Map<String, ServiceBean> EVENTCODESERVICEBEANMAP = new HashMap<String, ServiceBean>();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
        if (methods != null) {
            for (Method method : methods) {
                MsgEvent myMsgEvent = AnnotationUtils.findAnnotation(method, MsgEvent.class);
                if (myMsgEvent != null) {
                    String eventCode = myMsgEvent.msgEvent().eventCode();
                    ServiceBean servieBean = new ServiceBean();
                    servieBean.setServiceBeanObj(bean);
                    servieBean.setServiceMethod(method);
                    Class<?> argsCls = method.getParameterTypes()[0];
                    servieBean.setArgsCls(argsCls);
                    EVENTCODESERVICEBEANMAP.put(eventCode, servieBean);
                }
            }
        }
        return bean;
    }
}

呼叫者

@MsgEvent(msgEvent = RetailOrderEvent.PLACE_GENERALRETAILORDER)
    public Person getPerson(Person p) {
        return personMapper.getPerson(p.getId());
    }

spring boot debug模式下啟動一直不會再程式碼紅色部分停下,說明沒有獲取到自定義註解

原因是發現bean為jdk代理

解決辦法

@SpringBootApplication
@EnableAspectJAutoProxy(exposeProxy = true)
public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
或者
@ImportResource(locations = { "classpath:spring-basic.xml" })
@SpringBootApplication
//@EnableAspectJAutoProxy(exposeProxy = true)
public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
spring-basic.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-3.1.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd        
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">

    <!-- 配置使Spring採用CGLIB代理 -->
  	<aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true" />
    
</beans>

____________________

上述會讓所有的都採用CGLIB代理,如果只想對使用的類採用,其他的還是原來的話就可以對註解使用類上標註@Configuration代替@Component

相關文章