软件编程
位置:首页>> 软件编程>> java编程>> 解决spring boot启动扫描不到自定义注解的问题

解决spring boot启动扫描不到自定义注解的问题

作者:shop000  发布时间:2023-10-29 14:31:48 

标签:springboot,扫描,注解

对于自定义注解这里就不唠叨了,百度一大堆,这里有我一个自定义注解


@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

补充知识:解决Aspect注解基于注解的增强不生效的问题

Aspect基于注解的增强生效须满足3个条件:


<!--1.代理方式设置为 cglib,默认false,则必须通过实现某个接口才能实现增强 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!--2.配置文件中把须增强注解所在包扫描注入,或者配置 bean-->
<context:component-scan base-package="注解所在包路径"/>
<!--3.配置文件中把@Aspect注解所在类对应包扫描注入 或者配置bean-->
<context:component-scan base-package="aspect注解所在包路径"/>

ps : 若在 controller 层使用,则controller 也需要配置上边两个条件方能生效

来源:https://blog.csdn.net/wDong0613/article/details/79910768

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com