解决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
投稿
猜你喜欢
- 请求SpringBoot接受前台参数的六种方式,首先因为从前台发送的请求没有界面的话只能是从地址栏发送并且只能是Get请求,为了测试其他的请
- 本文实例总结了MFC程序设计常用技巧。分享给大家供大家参考。具体如下:1.属性页的添加:创建对话框的类,该类要从CpropertyPage继
- 1.最常用的方法是创建一个计数器,判断是否遇到‘\0',不是'\0'指针就往后加一。int my_strlen(co
- 简要:EigenFace是基于PCA降维的人脸识别算法,PCA是使整体数据降维后的方差最大,没有考虑降维后类间的变化。 它是将图像
- 第 1 步:将这个 Spring Boot 项目的打包方式设置为 war。<packaging>war</packagin
- 半路开始看的朋友可以回顾一下前几篇java并发编程专题(一)----线程基础知识java并发编程专题(二)----如何创建并运行java线程
- 最近在开发的过程中,一个列表的查询,涉及到了多表的关联查询,由于持久层使用的是mongodb,对这个非关系型数据使用的不是很多,所以在实现此
- 首先给出一段代码:public class AslistMethod { public static void main(String[]
- 详解java.lang.reflect.Modifier.isInterface()方法java.lang.reflect.Modifier
- 在android开发中,有时候我们想获取手机的一些硬件信息,比如android手机的总内存和可用内存大小。这个该如何实现呢?通过读取文件&q
- 如何高效地进行数组复制?如果把一个变量值复制给另外一个数组变量,那么2个变量指向托管堆上同一个引用。如果想在托管堆上创建另外的一份数组实例,
- 最近碰到这么个恶心的问题问题:有个arr文件被放到Module A中引用,现在Module B又依赖了Module A,则在编译过程中会发生
- 前几篇主要集中在注册中心eureka的使用上,接下来可以创建服务提供者provider来注册到eureka。demo源码见: https:/
- 我们都知道单精度浮点数(Single,float,Real)由32位0或1组成,它具体是如何来的。浮点数的32位N=1符号位(Sign)+8
- 一,FileWritter写入文件FileWritter, 字符流写入字符到文件。默认情况下,它会使用新的内容取代所有现有的内容,然而,当指
- package TOOLS;import java.io.BufferedReader;import java.io.File;import
- summarydetail传统的Spring项目会有很多的配置文件,比如我们要使用Redis,一般除了对应的依赖的jar包我们还需要在app
- 一、synchronized 有不足新事物的出现要不是替代老事物,要么就是对老事物的补充JUC 的 locks 就是对 synchroniz
- Java 2D API通过扩展抽象窗口工具箱(AWT),为Java程序提供了二维图像,文本和图形的功能。这个复杂的渲染包支持线形图像,文本和
- 一、Monkey 是什么?Monkey 就是SDK中附带的一个工具。二、Monkey 测试的目的?:该工具用于进行压力测试。 然后开发人员结