How can I qualify an autwired property with a variable from a configuration file using annotations?

My particular problem is that I configured two beans that implement the same interface, and I have a third bean that has the property of this type of interface. I am adding a property using the config property. Thus, assuming RemoteDataSource and LocalDataSource both implement IDataSource, and dao1 has a property of type IDataSource, my XML configuration might look like this:

<bean id="datasource1" class="com.foo.RemoteDataSource">
  <property name="url">${url}</property>
</bean>
<bean id="datasource2" class="com.foo.LocalDataSource">
  <property name="path">${filepath}</property>
</bean>
<bean id="dao1" class="com.foo.MyDAO">
  <property name="dataSource">${datasource}</property>
</bean>

With url, filepath and datasource defined in the included properties file. We are now pushing for annotation-based customization, and I'm not sure how to comment on my dao to set the data source to the properties file. I want to do something similar, but this is obviously not allowed:

@Autowired
@Qualifier("${datasource}")
public void setDataSource(IDataSource datasource) {...}

NB: this is spring 3

+3
5

:

@Autowired
public void setDataProviders(Map<String,IDataProvider> dataProviders) {
    this.dataProviders = dataProviders;
}

@Autowired
@Value("${cms}")
public void setDataProviderName(String dataProviderName) {
    this.dataProviderName = dataProviderName;
}

public IDataProvider getDataProvider() {
    return dataProviders.get(dataProviderName);
}

NB: DataProvider, DataSource, . REST.

+4

xml-? , , .

, xml, .

-

@Autowired
@Qualifier("designatedDatasource")
public void setDataSource(IDataSource datasource) {...}

xml:

<alias name="${dataSource}" alias="designatedDatasource"/>

, spring , , , . , , $dataSource . , .

+5

, . CDI, , beans xml.

+1

, .

, : ServiceImpl1, ServiceImpl2 ServiceImpl3 my.serviceImpl ,

my.serviceImpl = serviceImpl1

my.serviceImpl = serviceImpl2

my.serviceImpl = serviceImpl3

So, in my controller I have to use @Qualifier ($ {my.my.serviceImpl}) but this did not work, I even tried @value, but it also failed.

So, I defined the bean in my ApplicationConf.java as

@Bean(name = "myServiceImpl")
public Service myService() {

    String beanName = environment.getProperty("my.serviceImpl");

        if (beanName.equals("serviceImpl1")) {
            return new serviceImpl1();
        }
        else if(beanName.equals("serviceImpl2")){
            return new serviceImpl2();
        }
        else if(beanName.equals("serviceImpl3")){
            return new serviceImpl3();
        }


}

And in my controller, I used the classifier as

@Autowired
@Qualifier("myServiceImpl")
Service myService;

Not sure though if this is the best way to do this.

+1
source

For Spring 3.1, your problem is solved by Spring profiles :

<bean id="dao1" class="com.foo.MyDAO">
   <property name="dataSource">${datasource}</property>
</bean>

<beans profile="remote">
   <bean id="datasource1" class="com.foo.RemoteDataSource">
      <property name="url">${url}</property>
   </bean>
<beans>

<beans profile="local">
    <bean id="datasource2" class="com.foo.LocalDataSource">
      <property name="path">${filepath}</property>
    </bean>
<beans>

No need for @Qualifier, just one IDataSource per profile.

@Autowired
public void setDataSource(IDataSource datasource) {...}
0
source

Source: https://habr.com/ru/post/1785554/


All Articles