简体   繁体   中英

DAO classes have the same @Qualifier

I have the source code of a website.

This website uses spring, and I'am confused about the Injection.

There are some DAO classes, on these DAO with a Qualifier :

@Repository("myDao")
@Qualifier("myTransactionManager")
public class MyCategoryDao {

}

But there is also another bean with the same name of the Qualifier.

And this bean is a JpaTransactionManager.

@Configuration
@EnableTransactionManagement(mode = AdviceMode.PROXY)
@ComponentScan(basePackages = "com.my.repository")
public class DatabaseConfig {
    @Primary
    @Bean(name = "myTransactionManager")
    public JpaTransactionManager jpaTransactionManager(
    @Named("myEntityManagerFactory") EntityManagerFactory entityManagerFactory) {

    }
}

I don't know why the bean and the DAOs have the same name/Qualifier.

Wouldn't it conflict the others?

Spring will check for type first, and if there is any ambiguity, then only it will check for the name specified in @Qualifier. In your case, types of both the beans are different, so that should work fine.

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