对于自定义注解这里就不唠叨了,百度一大堆,这里有我一个自定义注解
@Retention(RetentionPolicy.RUNTIME)@Target({ ElementType.METHOD })public @interface MsgEvent { RetailOrderEvent msgEvent();}注解实现类
@Componentpublic 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://ponent-scan base-package="aspect注解所在包路径"/>ps : 若在 controller 层使用,则controller 也需要配置上边两个条件方能生效
以上这篇解决spring boot启动扫描不到自定义注解的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。