简体   繁体   English

有条件的spring bean创建

[英]Conditional spring bean creation

I have a question about Spring annotation configurations.我有一个关于 Spring 注释配置的问题。 I have a bean:我有一个豆子:

@Bean 
public ObservationWebSocketClient observationWebSocketClient(){
    log.info("creating web socket connection...");
    return new ObservationWebSocketClient();
}

and I have a property file:我有一个属性文件:

@Autowired
Environment env;

In the property file I want to have a special boolean property在属性文件中,我想要一个特殊的布尔属性

createWebsocket=true/false

which signs whether a bean ObservationWebSocketClient should be created.它标志着是否应该创建一个 bean ObservationWebSocketClient。 If property value is false I don't want to establish web socket connection at all.如果属性值为 false 我根本不想建立网络套接字连接。

Is there any technical possibility to realize this?是否有任何技术可能性来实现这一点?

Though I've not used this functionality, it appears that you can do this with spring 4's @Conditional annotation .虽然我没有使用过这个功能,但似乎你可以使用 spring 4 的@Conditional注释来做到这一点。

First, create a Condition class, in which the ConditionContext has access to the Environment :首先,创建一个Condition类,其中ConditionContext可以访问Environment

public class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, 
                           AnnotatedTypeMetadata metadata) {
        Environment env = context.getEnvironment();
        return null != env 
               && "true".equals(env.getProperty("createWebSocket"));
    }
}

Then annotate your bean:然后注释你的bean:

@Bean
@Conditional(MyCondition.class)
public ObservationWebSocketClient observationWebSocketClient(){
    log.info("creating web socket connection...");
    return new ObservationWebSocketClient();
}

edit The spring-boot annotation @ConditionalOnProperty has implemented this generically;编辑spring-boot注释@ConditionalOnProperty已经实现了这一点; the source code for the Condition used to evaluate it is available on github here for those interested.用于评估它的Condition的源代码可在 github 上找到,供感兴趣的人使用。 If you find yourself often needing this funcitonality, using a similar implementation would be advisable rather than making lots of custom Condition implementations.如果您发现自己经常需要这种功能,建议使用类似的实现,而不是制作大量自定义Condition实现。

Annotate your bean method with @ConditionalOnProperty("createWebSocket") .使用@ConditionalOnProperty("createWebSocket")注释您的 bean 方法。

Note that Spring Boot offers a number of useful conditions prepackaged.请注意,Spring Boot 提供了许多预先打包的有用条件。

For Spring Boot 2+ you can simply use:对于 Spring Boot 2+,您可以简单地使用:

@Profile("prod")
or
@Profile({"prod","stg"})

That will allow you to filter the desired profile/profiles, for production or staging and for the underlying Bean using that annotation it only will be loaded by Springboot when you set the variable spring.profiles.active is equals to "prod" and ("prod" or "stg").这将允许您过滤所需的配置文件/配置文件,用于生产或暂存以及使用该注释的底层 Bean,只有当您将变量 spring.profiles.active 设置为等于“prod”和(“ prod”或“stg”)。 That variable can be set on OS environment variables or using command line, such as -Dspring.profiles.active=prod.该变量可以在操作系统环境变量上或使用命令行设置,例如 -Dspring.profiles.active=prod。

对我来说,这个问题可以通过使用 Spring 3.1 @Profiles来解决,因为@Conditional注释让你有机会定义一些条件 bean 注册的策略(用户定义的条件检查策略),而@Profiles只能基于Environment的逻辑变量而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM