午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

解決spring boot啟動(dòng)掃描不到自定義注解的問題

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

對(duì)于自定義注解這里就不嘮叨了,百度一大堆,這里有我一個(gè)自定義注解

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

注解實(shí)現(xiàn)類

@Componentpublic class MsgEventProcessor implements BeanPostProcessor { /** * 事件消息注解與實(shí)例Bean的映射對(duì)象 */ 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; }}

調(diào)用者

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

spring boot debug模式下啟動(dòng)一直不會(huì)再代碼紅色部分停下,說明沒有獲取到自定義注解

原因是發(fā)現(xiàn)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>

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

補(bǔ)充知識(shí):解決Aspect注解基于注解的增強(qiáng)不生效的問題

Aspect基于注解的增強(qiáng)生效須滿足3個(gè)條件:

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

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

以上這篇解決spring boot啟動(dòng)掃描不到自定義注解的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产视频一区在线播放 | 久热操| 中午字幕在线观看 | 国产精品视频一二三 | 亚洲免费视频一区二区 | 欧洲精品一区二区 | 毛片网站在线看 | 国产精品一区二区三区四区 | 91精品国产成人观看 | 日本黄色录象 | 午夜国产福利 | 日韩中文字幕在线 | 国产精品1页 | 加勒比久久综合 | 91久久网| 国产精品第3页 | 午夜影院体验区 | 欧美视频在线一区 | 国产免费美女视频 | 久久av一区二区三区 | 欧美日韩乱国产 | 国产999| 欧美专区亚洲专区 | 亚洲精品视频一区二区三区 | 插妹妹av| 黄色成年视频 | 成年人午夜视频 | 超碰在线国产 | 在线看毛片网站 | 日本中文字幕视频 | 中文字幕第一页在线 | 亚洲精品久久久久avwww潮水 | 国产在线日韩 | 日本综合在线观看 | 国产精品久久久久免费 | 中国第一毛片 | 久久青娱乐 | 久久免费福利 | 最新中文字幕在线视频 | 色视频导航 | 中文字幕亚洲一区 |