亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

解決spring boot啟動掃描不到自定義注解的問題

瀏覽:66日期:2023-04-21 08:32:15

對于自定義注解這里就不嘮叨了,百度一大堆,這里有我一個自定義注解

@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://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- /> </beans>

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

補充知識:解決Aspect注解基于注解的增強不生效的問題

Aspect基于注解的增強生效須滿足3個條件:

<!--1.代理方式設置為 cglib,默認false,則必須通過實現某個接口才能實現增強 --><aop:aspectj-autoproxy proxy-target- /> <!--2.配置文件中把須增強注解所在包掃描注入,或者配置 bean--><context:component-scan base-package='注解所在包路徑'/><!--3.配置文件中把@Aspect注解所在類對應包掃描注入 或者配置bean--><context:component-scan base-package='aspect注解所在包路徑'/>

ps : 若在 controller 層使用,則controller 也需要配置上邊兩個條件方能生效

以上這篇解決spring boot啟動掃描不到自定義注解的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 韩国一级黄色 | 亚洲欧洲精品一区二区三区 | 国产精品国产三级在线专区 | 成人免费在线播放视频 | 免费国产成人高清在线观看视频 | 免费黄色国产视频 | 91视频网页| 色婷婷影院 | 成人影院午夜久久影院 | 欧美亚洲尤物久久精品 | 国产大片中文字幕在线观看 | 久久精品成人一区二区三区 | 日韩特级毛片免费观看视频 | 日韩中文字幕在线视频 | 99久久综合狠狠综合久久aⅴ | 欧美日韩国产在线人 | 国内成人精品视频 | 在线sese| 在线观看日本免费视频大片 | 久久综合九九 | 亚洲色图第1页 | 亚洲欧美中日韩中文字幕 | 97起碰 | 国产成人a视频在线观看 | 国产精品久久久久这里只有精品 | 国产一区二区三区在线观看影院 | 操网| 亚洲女精品一区二区三区 | 欧美专区视频 | 国产丝袜视频在线 | 免费爱爱的视频太爽了 | 久久久7777888精品 | 国产在线精品一区二区高清不卡 | 免费 欧美 自拍 在线观看 | 给我一个可以看片的www日本 | 欧美国产免费 | 亚洲午夜视频在线 | 亚洲精品欧美日本中文字幕 | 国产亚洲精品一区二区在线播放 | pans写真福利视频在线 | 超级碰碰碰视频在线观看 |