Springboot-public module custom annotations cannot take effect in other microservices

[spring] @ComponentScan Detailed Explanation & @SpringBootApplication's scanBasePackages Attribute - Programmer Sought

Coupon microservice:

reason: 

Solution:


The solution to the failure of custom annotations that SpringBoot calls public modules:

项目结构如下
 
我在 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) {
 
HBbjuucG 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();
 
}
 
解决方法
 
原因:
 
其他工程没有扫描公共模块的包,没有扫描到注解的位置。
 
解决HBbjuucG方法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 @ScHBbjuucGheduled等;
 
解决方法
 
在子项目右键,clean install,会发现报错了,解决完问题以后就可以了。

Solution: Add the package path of the public module to the startup class @ComponentScan("com.nanjing")

The following is also possible:  

It is not possible to write the following:

Because @SpringBootApplication already contains the @ComponentScan annotation to scan the files under the current package by default

The reason why your annotation is redundant here is because the path of your scan package is the same as the default path

There is no problem writing the following: @ComponentScans({@ComponentScan("com.nanjing.common")})

However, using @ComponentScan to write only the package path of the public module below will report an error: 


Now that the custom annotation PringLog of the public module can be used, I don't need to define another PringLog2

 

Guess you like

Origin blog.csdn.net/ZHOU_VIP/article/details/129876428