1

I have a problem with creating two beans of the same class but different qualifier name. Basically One bean is created with the annotations @Repository and the other one is creating inside @Configuration class.

This is the class that we wont two instances with different datasources:

@Repository ("someDao")
public class SomeDaoImpl implements SomeDao {

    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    }
}

And then we have a service like this:

@Service
public class SomeServiceImpl implements
        SomeService {

    @Resource(name = "someDao")
    private SomeDao someDao;

    @Resource(name = "someDaoAnotherDataSource")
    private SomeDao someDaoAnotherDataSource;

}

My first bean is created by the annotation @Repository and the other one I declared in a Spring Configuration class:

@Configuration
@ComponentScan(basePackages = "mypackages")
public abstract class ApplicationRootConfiguration {

    @Bean(name = "otherDataSource")
    public DataSource otherDataSource() throws NamingException {
    ...
    }

    @Bean(name = "someDaoAnotherDataSource")
    public SomeDao getSomeDaoAnotherDataSource(@Qualifier("otherDataSource")
     DataSource dataSource) throws NamingException {
        SomeDao dao = new SomeDaoImpl();
        dao.setDataSource(dataSource);
        return dao;
    }
}

If I run my application, the property someDaoAnotherDataSource in SomeServiceImpl doesn't have my bean declared in the configuration class, it have the bean declared with the annotation Repository.

Also, if I running the same example in XML configuration it works:

<bean id="someDaoAnotherDataSource"     class="SomeDaoImpl">
    <property name="dataSource" ref="otherDataSource" />
</bean>

Any idea why is not autowiring the proper bean?

Thanks in advance.

Gerard Ribas
  • 717
  • 1
  • 9
  • 17

1 Answers1

0

Just a thought, instead of injecting the datasource into the method try the following

@Bean(name = "someDaoAnotherDataSource")
public SomeDao getSomeDaoAnotherDataSource() throws NamingException {
    SomeDao dao = new SomeDaoImpl();
    dao.setDataSource(otherDataSource());
    return dao;
}

Although both should work.

Make sure that the name is correct in your @Resource annotation and instead of (or next to) specifing the name of the bean in the @Bean annotation, rename your method to the same.

@Bean(name = "someDaoAnotherDataSource")
public SomeDao someDaoAnotherDataSource() throws NamingException {
    SomeDao dao = new SomeDaoImpl();
    dao.setDataSource(otherDataSource());
    return dao;
}

The name is basically an alias whereas the method name is used as the id of the bean. Could be that resolving isn't looking at aliases correctly. The same in xml would be

<bean id="getSomeDaoAnotherDataSource" name="someDaoAnotherDataSource"     class="SomeDaoImpl">
    <property name="dataSource" ref="otherDataSource" />
</bean>
M. Deinum
  • 115,695
  • 22
  • 220
  • 224