简体   繁体   中英

Instantiate Spring Bean

I am trying to call two different beans based on my requirement.This two beans are implementing same interface.I have to use two diffrent db's based on data center.How I can do that in springboot application by passing a parameter from application.properties

interface Data
{
   string getData(String query)
}



   @Component("oracle")
   class Oracle implements Data
   {
       //getMethod Code Here.
   }


   @Component("sqlserver")
   class SqlServer implements Data
   {
       //getMethod Code Here.
   }

Currently I am using

  String db = appContext.getBean(propertiesfile.db//getting the value from properties file, Data::class.java)

and calling particular db based on return value.But is there anyway I can only instantiate one bean based on app.properties means not using appContext.getBean in every request

As per suggestions I ended up Using @ConditionalOnProperty

This link helped me lot resolving this issue.

https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-autoconfiguration/src/main/java/com/baeldung/conditionalonproperty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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