SpringBoot调用公共模块的自定义注解失效的解决
时间:2022-07-27 11:16:14|栏目:JAVA代码|点击: 次
调用公共模块的自定义注解失效
项目结构如下

我在 bi-common 公共模块里定义了一个自定义注解,实现AOP记录日志,bi-batch 项目已引用了 bi-common ,当在 bi-batch 使用注解的时候,没有报错,但是切面却失效。
自定义注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JobLog {
}
切面实现:
/**
* 执行任务时记录日志
*/
@Aspect
@Component
@Order(1)
@Slf4j
public class JobLogAspect {
@Pointcut("@annotation(aoshu.bi.platform.common.annotation.JobLog)")
public void pointcut() {
}
@Before("pointcut()")
public void logStart(JoinPoint joinPoint) {
log.info("开始执行" + joinPoint.getSignature().getName() + "任务,参数为:" + Arrays.toString(joinPoint.getArgs()));
}
@After("pointcut()")
public void logEnd(JoinPoint joinPoint){
log.info(""+joinPoint.getSignature().getName()+"方法运行后。。。@After");
}
}
注解使用:
/**
* 这里使用了自定义注解,却失效,但是没报错
*/
@JobLog
public Job createEsJob(String jobName) {
return jobBuilderFactory.get(jobName)
.start(esLogJobStep.step())
.build();
}
解决方法
原因:
其他工程没有扫描公共模块的包,没有扫描到注解的位置。
解决方法1:
在启动类加上公共模块的包路径,注意别忘记把原项目的包路径也加上
@SpringBootApplication(scanBasePackages = {
"aoshu.bi.platform.batch",
"aoshu.bi.platform.common"
})
解决方法2:
在配置类里导入该切面实现
@Import({
aoshu.bi.platform.common.aspect.JobLogAspect.class
})
@Configuration
public class BatchConfigure {
}
SpringBoot注解不生效,踩坑
子模块的项目,注解都不生效,包括@RestController @EnableScheduling @Scheduled等;
解决方法
在子项目右键,clean install,会发现报错了,解决完问题以后就可以了。

上一篇:关于Spring中一级缓存、二级缓存和三级缓存的那些事
栏 目:JAVA代码
下一篇:springBoot使用openfeign来远程调用的实现
本文标题:SpringBoot调用公共模块的自定义注解失效的解决
本文地址:http://www.codeinn.net/misctech/209036.html






