对于自定义注解这里就不唠叨了,百度一大堆,这里有我一个自定义注解
?
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基于注解的增强生效须满足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启动扫描不到自定义注解的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/wDong0613/article/details/79910768
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 41
-
2025-05-25 41
-
2025-05-25 35
-
2025-05-27 62
-
2025-05-29 53
热门评论