G
G
GlaIZier2015-05-19 13:52:50
Java
GlaIZier, 2015-05-19 13:52:50

How to properly declare complex beans?

Hello dear developers!
I use spring recently, so I have a question.
There is a project written in spring, configuration via Java was used.
Configuration class:

@SpringBootApplication
@ComponentScan
public class Application extends SpringBootServletInitializer {
     // ...
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "datasource.one")
    DataSource one() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource.two")
    DataSource two() {
        return DataSourceBuilder.create().build();
    }

   @Bean
    DAO dao(DAOImpl dao) {
        return dao;
    }

    // ...
}

DAOImpl class:
@Component
public class DAOImpl implements DAO {

    @Autowired
    @Qualifier("one")
    private DataSource grad;

    @Autowired
    @Qualifier("two")
    private DataSource phone;

    //... сложная бизнес-логика для работы с бд
}

Accordingly, if I want to implement DataSource beans from the configuration, then I must declare the DAOImpl class as a component, so at runtime in the container I get two beans (DAO interface from the Application configuration and DAOImpl) that look at the same implementation. How to correctly configure the beans in this case?
Thanks a lot!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2015-05-19
@GlaIZier

Don't declare a DAO bean. Annotate the DAOImpl implementation with the @Repository annotation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question