Rav*_*avi 5 java spring annotations maven spring-boot
我正在使用 maven 依赖项在另一个 spring boot 应用程序中将 spring boot 项目作为 jar 使用。仅当我启用来自微服务的自定义注释时,我才想对 jar 进行组件扫描。
@SpringBootApplication
//@ComponentScan({"com.jwt.security.*"}) To be removed by custom annotation
@MyCustomAnnotation //If I provide this annotation then the security configuration of the jar should be enabled.
public class MicroserviceApplication1 {
public static void main(String[] args) throws Exception {
SpringApplication.run(MicroserviceApplication1.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
请提出一些想法。
您可以使用它来定义配置(请参阅此处@Conditional
描述的示例)。源码中的一些代码
@Configuration
public class MyConfiguration {
@Bean(name="emailerService")
@Conditional(WindowsCondition.class)
public EmailService windowsEmailerService(){
return new WindowsEmailService();
}
@Bean(name="emailerService")
@Conditional(LinuxCondition.class)
public EmailService linuxEmailerService(){
return new LinuxEmailService();
}
}
Run Code Online (Sandbox Code Playgroud)
和有条件的
public class WindowsCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Windows");
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用配置文件。只需将其添加@Profile
到您的配置类并扫描所需的包即可。
这里描述了另一种替代方案。
@AutoconfigureAfter(B.class)
@ConditionalOnBean(B.class)
public class A123AutoConfiguration { ...}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1719 次 |
最近记录: |