Java 使用自定义注释的组件扫描
我在另一个使用maven依赖项的springboot应用程序中使用一个springboot项目作为jar。我只想在启用microservice的自定义注释的情况下对jar进行组件扫描Java 使用自定义注释的组件扫描,java,spring,maven,spring-boot,annotations,Java,Spring,Maven,Spring Boot,Annotations,我在另一个使用maven依赖项的springboot应用程序中使用一个springboot项目作为jar。我只想在启用microservice的自定义注释的情况下对jar进行组件扫描 @SpringBootApplication //@ComponentScan({"com.jwt.security.*"}) To be removed by custom annotation @MyCustomAnnotation //If I provide this annotation then t
@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);
}
}
请提出一些想法。您可以使用
@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();
}
}
和有条件的
public class WindowsCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Windows");
}
}
您可以使用配置文件。只需将@Profile
添加到您的配置类中,并扫描所需的包
描述了另一个备选方案
在您的库中:
@Configuration
@ComponentScan(basePackages = { "com.jwt.security" })
public class MyCustomLibConfig{
}
@Retention(RUNTIME)
@Target(TYPE)
@Import(MyCustomLibConfig.class)
public @interface MyCustomAnnotation{
@AliasFor(annotation = Import.class, attribute = "value")
Class<?>[] value() default { MyCustomLibConfig.class };
}
我建议你重构你想要共享的代码,并将其作为一个单独的项目……除了在微服务世界中共享代码之外,这是一个坏主意……或者如果你需要一个mono repo设置……你是如何实现这一点的?你将如何拥有
basePackages
变量例如,指定自定义值@MyCustomAnnotation(basePackages=“com.example”)
,或者更好,默认为注释类的包。我尝试了这个方法,看起来没有任何进一步的配置,基本包是从Annotation+Config获取的,而不是从Annotation类获取的。您知道这是否可能吗?@knittl在您的例子中,MyCustomAnnotation应该具有属性basePackages
,并且不应该导入MyCystomLibConfig
,而是一个实现importBeanDefinitionRegistrator
的configBean。register
可以访问CustomAnnotation(作为基本包)的属性,并且可以通过编程方式构建MyCustomLibConfig
。要以编程方式设置扫描包,我假设在您的情况下MyCustomLibConfig
必须实现WebApplicationInitializer
。您可以找到一个使用importBeanDefinitionRegistrator
的示例,谢谢,但这是正确的链接吗?我收到一份中文PDF?也许你可以提供一个完整的答案?对不起,我粘贴了一个错误的链接。不管怎样,我刚刚回答了你的问题
@Configuration
@ComponentScan(basePackages = { "com.jwt.security" })
public class MyCustomLibConfig{
}
@Retention(RUNTIME)
@Target(TYPE)
@Import(MyCustomLibConfig.class)
public @interface MyCustomAnnotation{
@AliasFor(annotation = Import.class, attribute = "value")
Class<?>[] value() default { MyCustomLibConfig.class };
}
@SpringBootApplication
@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);
}
}