通过环境属性实现Spring服务 [英] Spring service implementation by environment property
问题描述
我有一个服务接口
interface ImageSearchService {//方法...}
我有 2 个实现:
@Service类 GoogleImageSearchImpl 实现 ImageSearchService {//方法...}@服务类 AzureImageSearchImpl 实现 ImageSearchService {//方法...}
而且我有一个控制器来使用两者之一:
@Controller图像搜索控制器 {@自动连线ImageSearchService 图像搜索;//谷歌还是 Azure ?!?}
如何使用 Environment API 设置正确的一个实现?
如果环境属性是 my.search.impl=google
spring 需要使用 GoogleImageSearchImpl
,或者如果环境 my.search.impl=google
> spring 需要使用 AzureImageSearchImpl
.
您可以使用 Spring Profiles 来实现这一点.
接口 ImageSearchService {}类 GoogleImageSearchImpl 实现 ImageSearchService {}类 AzureImageSearchImpl 实现 ImageSearchService {}
请注意,@Service
注释已从两个实现类中删除,因为我们将动态实例化它们.
然后,在您的配置中执行以下操作:
<beans profile="azure"><bean class="AzureImageSearchImpl"/></豆类><beans profile="google"><bean class="GoogleImageSearchImpl"/></豆类></beans>
如果您使用 Java 配置:
@Configuration@Profile("天蓝色")公共类 AzureConfiguration {@豆角,扁豆公共 ImageSearchService imageSearchService() {返回新的 AzureImageSearchImpl();}}@配置@Profile("谷歌")公共类 GoogleConfiguration {@豆角,扁豆公共 ImageSearchService imageSearchService() {返回新的 GoogleImageSearchImpl();}}
运行应用程序时,通过设置变量 spring.profiles.active
的值来选择要运行的配置文件.您可以将其作为 -Dspring.profiles.active=azure
传递给 JVM,将其配置为环境变量等.
此处是一个示例应用程序,展示了 Spring Profiles 的运行情况.>
I have a service interface
interface ImageSearchService {
// methods...
}
And I have 2 implementations:
@Service
class GoogleImageSearchImpl implements ImageSearchService {
// methods...
}
@Service
class AzureImageSearchImpl implements ImageSearchService {
// methods...
}
And I have a controller to use one of the both:
@Controller
ImageSearchController {
@Autowired
ImageSearchService imageSearch; // Google or Azure ?!?
}
How can I use a Environment API to set the right one implementation?
If environment property is my.search.impl=google
spring needs to use GoogleImageSearchImpl
, or if environment my.search.impl=google
spring needs to use AzureImageSearchImpl
.
You can achieve this using Spring Profiles.
interface ImageSearchService {}
class GoogleImageSearchImpl implements ImageSearchService {}
class AzureImageSearchImpl implements ImageSearchService {}
Note that the @Service
annotation has been removed from both the implementation classes because we will instantiate these dynamically.
Then, in your configuration do this:
<beans>
<beans profile="azure">
<bean class="AzureImageSearchImpl"/>
</beans>
<beans profile="google">
<bean class="GoogleImageSearchImpl"/>
</beans>
</beans>
If you use Java configuration:
@Configuration
@Profile("azure")
public class AzureConfiguration {
@Bean
public ImageSearchService imageSearchService() {
return new AzureImageSearchImpl();
}
}
@Configuration
@Profile("google")
public class GoogleConfiguration {
@Bean
public ImageSearchService imageSearchService() {
return new GoogleImageSearchImpl();
}
}
When running the application, select the profile you want to run as by setting the value for the variable spring.profiles.active
. You can pass it to the JVM as -Dspring.profiles.active=azure
, configure it as an environment variable, etc.
Here is a sample application that shows Spring Profiles in action.
这篇关于通过环境属性实现Spring服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!