Spring service implementation by environment property

不羁岁月 提交于 2021-01-22 21:14:26

问题


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.


回答1:


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.




回答2:


You can use @Conditional also

@Configuration
public class MyConfiguration {

  @Bean
  @Conditional(LinuxCondition.class)
  public MyService getMyLinuxService() {
    return new LinuxService();
  }
}

Example given : Spring choose bean implementation at runtime



来源:https://stackoverflow.com/questions/28697296/spring-service-implementation-by-environment-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!