Java 根据配置文件中的变量使用不同的bean实现
我有两颗豆子:Java 根据配置文件中的变量使用不同的bean实现,java,spring,spring-boot,Java,Spring,Spring Boot,我有两颗豆子: @Component("FierstClient") public class FierstClient implements CryptoClient { 及 我有以下服务: @Component public class CryptoServiceImpl implements CryptoService { private final Marshaler marshaler; private final CryptoCli
@Component("FierstClient")
public class FierstClient implements CryptoClient {
及
我有以下服务:
@Component
public class CryptoServiceImpl implements CryptoService {
private final Marshaler marshaler;
private final CryptoClient cryptoClient;
public CryptoServiceImpl(Marshaler marshaler,
@Qualifier("FirstClient") CryptoClient cryptoClient) {
this.marshaler = marshaler;
this.cryptoClient = cryptoClient;
}
现在我有一个任务-移动来控制这个bean配置文件。我知道一些解决方案,但它们对我来说似乎很幼稚:
默认服务器:第一个//或第二个
并在CryptoServiceImpl
中插入2个bean:
@限定符(“FirstClient”)CryptoClient cryptoClientFirst
@限定符(“SecondsClient”)CryptoClient cryptoClientSecondif(default-server equals first)...else...
配置文件
。但我会有另一个配置,如DB等。我会有许多组合的亵渎,如:可能存在依赖于配置文件中的变量使用不同bean实现的明确解决方案 你可以用这样的东西
@Configuration
public class ClientConfig {
@Bean(name="criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
havingValue = "first")
public CryptoClient firstClient() {
// return first client
}
@Bean(name="criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
havingValue = "second")
public CryptoClient secondClient() {
// return second client
}
@Bean(name="criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
matchIfMissing = true)
public CryptoClient defaultClient() {
// return default client
}
}
您需要将enable.client
属性设置为first
或second
。如果属性不存在,则会实例化DefaultClient
另一种方法是将@条件属性
移动到@组件
定义之上。在这种情况下,您将不再需要上述配置
@Component("criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
havingValue = "first")
public class FierstClient implements CryptoClient {
}
@Component("criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
havingValue = "second")
public class SecondClient implements CryptoClient {
}
你可以用这样的东西
@Configuration
public class ClientConfig {
@Bean(name="criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
havingValue = "first")
public CryptoClient firstClient() {
// return first client
}
@Bean(name="criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
havingValue = "second")
public CryptoClient secondClient() {
// return second client
}
@Bean(name="criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
matchIfMissing = true)
public CryptoClient defaultClient() {
// return default client
}
}
您需要将enable.client
属性设置为first
或second
。如果属性不存在,则会实例化DefaultClient
另一种方法是将@条件属性
移动到@组件
定义之上。在这种情况下,您将不再需要上述配置
@Component("criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
havingValue = "first")
public class FierstClient implements CryptoClient {
}
@Component("criptoClient")
@ConditionalOnProperty(
name = "enabled.client",
havingValue = "second")
public class SecondClient implements CryptoClient {
}
移动到此beans配置文件的控制-什么?你能说得更具体一点吗?@Eugene我在配置文件中更改参数,我的服务器使用firestbean/I更改参数,我的服务器使用第二个beanmove来控制这个bean配置文件-什么?你能说得更具体一点吗?@Eugene我在配置文件中更改参数,我的服务器使用firestbean/I更改参数,我的服务器使用第二个beanOk,我尝试了这个解决方案,但我如何将CryptoClient注入我的服务?我有一个警告:无法自动连线。“FirstClient”类型的bean不止一个。bean:firstclientmpl(firstclientmpl.java)secondClient(secondClient.java)启用的
客户端的值是多少?另外,您是否删除了带有@Component
的旧定义?我有两个带有@Component
注释的bean。我想我解决了这个问题。我在@Component
注释下添加@ConditionalOnProperty
注释,并为bean编写相同的名称-@Component(“scriptoclient”)
用于第一个组件,而@Component(“scriptoclient)
用于第二个组件是,您可以将@ConditionalOnProperty
注释移动到此处,也可以删除@组件
注释,并完全依赖bean定义的配置。在任何情况下,如果我删除注释@组件
,则所有bean都必须具有相同的名称。您可以填写您的答案,我会接受它是正确的。好的,我尝试了这个解决方案,但我如何才能将CryptoClient注入到我的服务中?我有一个警告:无法自动连线。“FirstClient”类型的bean不止一个。bean:firstclientmpl(firstclientmpl.java)secondClient(secondClient.java)启用的客户端的值是多少?另外,您是否删除了带有@Component
的旧定义?我有两个带有@Component
注释的bean。我想我解决了这个问题。我在@Component
注释下添加@ConditionalOnProperty
注释,并为bean编写相同的名称-@Component(“scriptoclient”)
用于第一个组件,而@Component(“scriptoclient)
用于第二个组件是,您可以将@ConditionalOnProperty
注释移动到此处,也可以删除@组件
注释,并完全依赖bean定义的配置。在任何情况下,如果我删除注释@组件
,则所有bean都必须具有相同的名称。你可以填写你的答案,我会认为它是正确的。