Java 有条件的spring bean创建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29844271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional spring bean creation
提问by user2957954
I have a question about Spring annotation configurations. I have a bean:
我有一个关于 Spring 注释配置的问题。我有一个豆子:
@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. If property value is false I don't want to establich web socket connection at all.
它标志着是否应该创建一个 bean ObservationWebSocketClient。如果属性值为 false,我根本不想建立 Web 套接字连接。
Is there any technical possibility to realize this?
是否有任何技术可能性来实现这一点?
采纳答案by beerbajay
Though I've not used this functionality, it appears that you can do this with spring 4's @Conditional
annotation.
虽然我没有使用过这个功能,但看起来你可以用 spring 4 的@Conditional
annotation来做到这一点。
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();
}
editThe spring-boot
annotation @ConditionalOnProperty
has implemented this generically; the source code for the Condition
used to evaluate it is available on github herefor those interested. If you find yourself often needing this funcitonality, using a similar implementation would be advisable rather than making lots of custom Condition
implementations.
编辑的spring-boot
注释@ConditionalOnProperty
也一般实行这一点; Condition
用于评估它的源代码可在 github 上找到,供感兴趣的人使用。如果您发现自己经常需要这种功能,建议使用类似的实现而不是进行大量自定义Condition
实现。
回答by chrylis -cautiouslyoptimistic-
Annotate your bean method with @ConditionalOnProperty("createWebSocket")
.
用 注释你的 bean 方法@ConditionalOnProperty("createWebSocket")
。
Note that Spring Boot offers a number of useful conditions prepackaged.
请注意,Spring Boot 提供了许多预先打包的有用条件。
回答by Dmytro Melnychuk
As for me, this problem can be solved by using Spring 3.1 @Profiles
, because @Conditional
annotation give you opportunity for define some strategy for conditional bean registration (user-defined strategies for conditional checking), when @Profiles
can based logic only on Environment
variables only.
对我来说,这个问题可以通过使用 Spring 3.1 来解决@Profiles
,因为@Conditional
注解让你有机会为条件 bean 注册定义一些策略(用户定义的条件检查策略),当@Profiles
逻辑只能基于Environment
变量时。
回答by rod.poli.diniz
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"). That variable can be set on O.S. environment variables or using command line, such as -Dspring.profiles.active=prod.
这将允许您过滤所需的配置文件/配置文件,用于生产或暂存以及使用该注释的底层 Bean 只有当您将变量 spring.profiles.active 设置为等于“prod”和(“ prod”或“stg”)。该变量可以在操作系统环境变量上或使用命令行设置,例如 -Dspring.profiles.active=prod。