一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - 解決spring boot啟動掃描不到自定義注解的問題

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

2020-09-26 21:30shop000 JAVA教程

這篇文章主要介紹了解決spring boot啟動掃描不到自定義注解的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

?
1
2
3
4
5
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface MsgEvent {
  RetailOrderEvent msgEvent();
}

注解實現類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@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;
  }
}

調用者

?
1
2
3
4
@MsgEvent(msgEvent = RetailOrderEvent.PLACE_GENERALRETAILORDER)
  public Person getPerson(Person p) {
    return personMapper.getPerson(p.getId());
  }

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

原因是發現bean為jdk代理

解決辦法

?
1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableAspectJAutoProxy(exposeProxy = true)
public class Application {
  
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

或者

?
1
2
3
4
5
6
7
8
9
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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
2
3
4
5
6
<!--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 也需要配置上邊兩個條件方能生效

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

原文鏈接:https://blog.csdn.net/wDong0613/article/details/79910768

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产麻豆91网在线看 | 办公室大战秘书呻吟 | 精品手机在线1卡二卡3卡四卡 | 日本国产成人精品视频 | 极品妖艳许清赵丽全文免费阅读 | 日韩夫妻性生活 | 99在线免费播放 | 男人天堂新 | 95视频在线观看在线分类h片 | 久久综合老色鬼网站 | 成人亚洲欧美日韩中文字幕 | 51香蕉视频 | 成人软件18免费 | 韩国三级大全 | 国产精品毛片久久久久久久 | 亚洲成年人专区 | 69av免费视频 | 天天狠天天透天干天天怕处 | porno xxxx日本| 国产综合第一页 | 果冻传媒在线播放观看228集 | 超逼网 | 太粗 好紧 使劲舒服 | 国产精品合集一区二区 | 国产小视频在线免费 | 国产精品视频播放 | 色综合色综合 | 精品国产自在现线久久 | 免费看男人使劲躁女人小说 | 好男人在线观看hd中字 | 美女扒开胸罩露出胸大乳 | 国产精品久久久久久久久免费hd | 激情乱文| 日本天堂视频在线观看 | 女人张开腿让男人桶爽 | 欧美一区二区三区成人看不卡 | 国产色拍| 成人久久久 | 日本丰满大乳乳奶 | 亚洲精品高清中文字幕完整版 | 国产福利在线观看第二区 |