解決spring boot啟動(dòng)掃描不到自定義注解的問題
對于自定義注解這里就不嘮叨了,百度一大堆,這里有我一個(gè)自定義注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface MsgEvent {
RetailOrderEvent msgEvent();
}
注解實(shí)現(xiàn)類
@Component
public class MsgEventProcessor implements BeanPostProcessor {
/**
* 事件消息注解與實(shí)例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;
}
}
調(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-class="true" /> </beans>
上述會(huì)讓所有的都采用CGLIB代理,如果只想對使用的類采用,其他的還是原來的話就可以對注解使用類上標(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-class="true"/> <!--2.配置文件中把須增強(qiáng)注解所在包掃描注入,或者配置 bean--> <context:component-scan base-package="注解所在包路徑"/> <!--3.配置文件中把@Aspect注解所在類對應(yīng)包掃描注入 或者配置bean--> <context:component-scan base-package="aspect注解所在包路徑"/>
ps : 若在 controller 層使用,則controller 也需要配置上邊兩個(gè)條件方能生效
以上這篇解決spring boot啟動(dòng)掃描不到自定義注解的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SSM框架+Plupload實(shí)現(xiàn)分塊上傳大文件示例
這篇文章主要介紹了SSM框架+Plupload實(shí)現(xiàn)分塊上傳示例(Spring+SpringMVC+MyBatis+Plupload),將用戶選中的文件(可多個(gè))分隔成一個(gè)個(gè)小塊,依次向服務(wù)器上傳,有興趣的可以了解一下。2017-03-03
Java下利用Jackson進(jìn)行JSON解析和序列化示例
本篇文章主要介紹了Java下利用Jackson進(jìn)行JSON解析和序列化示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
SpringBoot整合jasypt實(shí)現(xiàn)數(shù)據(jù)加密的步驟
聽說過jasypt嗎?它可是一個(gè)超級流行的Java庫哦,提供了簡單又高效的加密和解密接口,整合jasypt后,我們的SpringBoot應(yīng)用就能輕松處理敏感數(shù)據(jù)的加密和解密,而不必為復(fù)雜的加密算法頭疼啦,下面給大家介紹SpringBoot整合jasypt實(shí)現(xiàn)數(shù)據(jù)加密的步驟,感興趣的朋友一起看看吧2025-04-04
詳解Spring關(guān)于@Resource注入為null解決辦法
這篇文章主要介紹了詳解Spring關(guān)于@Resource注入為null解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Java代碼實(shí)現(xiàn)哈希表(google 公司的上機(jī)題)
這篇文章主要介紹了Java 哈希表詳解(google 公司的上機(jī)題),本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
SpringBoot實(shí)現(xiàn)文件上傳并返回url鏈接的示例代碼
文件上傳,當(dāng)我們選擇了某一個(gè)圖片文件之后,這個(gè)文件就會(huì)上傳到服務(wù)器,從而完成文件上傳的操作,是指將本地圖片、視頻、音頻等文件上傳到服務(wù)器,供其他用戶瀏覽或下載的過程,本文給大家介紹了SpringBoot實(shí)現(xiàn)文件上傳并返回url鏈接,需要的朋友可以參考下2024-11-11
java?面向?qū)ο蟠a塊及不同位置對屬性賦值的執(zhí)行順序
這篇文章主要介紹了java面向?qū)ο蟠a塊及不同位置對屬性賦值的執(zhí)行順序,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09

