多平台统一管理软件接口,如何实现多平台统一管理软件接口
463
2022-11-20
解决spring boot启动扫描不到自定义注解的问题
对于自定义注解这里就不唠叨了,百度一大堆,这里有我一个自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface MsgEvent {
RetailOrderEvent msgEvent();
}
注解实现类
@Component
public class MsgEventProcessor implemehttp://nts BeanPostProcessor {
/**
* 事件消息注解与实例Bean的映射对象
*/
public static Map
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitializatioJbmsspHZn(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
xmlns:xsi="http://w3.org/2001/XMLSchema-JbmsspHZinstance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd ">
xmlns:xsi="http://w3.org/2001/XMLSchema-JbmsspHZinstance"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.1.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.1.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.1.xsd ">
上述会让所有的都采用CGLIB代理,如果只想对使用的类采用,其他的还是原来的话就可以对注解使用类上标注@Configuration代替@Component
补充知识:解决Aspect注解基于注解的增强不生效的问题
Aspect基于注解的增强生效须满足3个条件:
ps : 若在 controller 层使用,则controller 也需要配置上边两个条件方能生效
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~