简体   繁体   English

不同的spring bean实例的相同类

[英]Same class for different spring bean instances

I have a Spring application that connects to two databases at the same time. 我有一个Spring应用程序,它同时连接到两个数据库。 So I have for this two LocalSessionFactoryBean instances for each connection like this: 所以我为每个连接提供了两个LocalSessionFactoryBean实例,如下所示:

@Bean
public LocalSessionFactoryBean firstSessionFactory() {
    final LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
    lsfb.setPackagesToScan("ro.mycompany.myproject.classes");
    lsfb.setDataSource(dataSourceOne);
    lsfb.setEntityInterceptor(auditInterceptor1);
    lsfb.setHibernateProperties(getHibernateProperties1());
    return lsfb;
}

@Bean
public LocalSessionFactoryBean secondSessionFactory() {
    final LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
    lsfb.setPackagesToScan("ro.mycompany.myproject.classes2");
    lsfb.setDataSource(dataSourceTwo);
    lsfb.setEntityInterceptor(auditInterceptor2);
    lsfb.setHibernateProperties(getHibernateProperties2());
    return lsfb;
}

For the DAO layer I have a class that injects the SessionFactory object like this. 对于DAO层,我有一个类,它会像这样注入SessionFactory对象。

public class GenericDAOImpl extends HibernateDAOSupport implements GenericDAO {
     @Autowired 
     private SessionFactory sessionFactory;
     //Other methods goes here
}

I instantiate the beans in my config file like this: 我在我的配置文件中实例化bean,如下所示:

@Bean
public GenericDAO firstGenericDAO() {
    final GenericDAOImpl genericDAO = new GenericDAOImpl();
    return genericDAO;
}

@Bean
public GenericDAO secondGenericDAO() {
    final GenericDAOImpl genericDAO = new GenericDAOImpl();
    return genericDAO;
}

How can I make the firstGenericDAO to use firstSessionFactory and secondGenericDAO to use secondSessionFactory without creating the setters method? 如何在不创建setter方法的情况下使firstGenericDAO使用firstSessionFactory和secondGenericDAO来使用secondSessionFactory? I want to use both connection at the same time so also Spring profiles won't help me. 我想同时使用这两个连接,所以Spring配置文件也无法帮助我。 Thank you 谢谢

Either use @Qualifier("...") in addition to @Autowired or just use @Resource(name = "...") . 除了@Autowired之外,还可以使用@Qualifier("...") ,或者只使用@Resource(name = "...") Personally I prefer using @Resource as it replaces the two Annotations with the single one. 我个人更喜欢使用@Resource,因为它用单个注释取代了两个注释。

In your case, @Resource(name = "firstSessionFactory") and @Resource(name = "secondSessionFactory") respectively. 在您的情况下,分别是@Resource(name = "firstSessionFactory")@Resource(name = "secondSessionFactory")

For you following code, 对于你下面的代码,

public class GenericDAOImpl extends HibernateDAOSupport implements GenericDAO {
     @Autowired 
     private SessionFactory sessionFactory;
     //Other methods goes here
}

Spring should be known clearly which bean would be autowired. 应该清楚地知道Spring将自动装配哪种bean。 That means, there should be setter method or some other variable to distinguish your sessionFactory1 and sessionFactory2. 这意味着,应该有setter方法或其他一些变量来区分你的sessionFactory1和sessionFactory2。

Just as you said, xml-based is still power than annotation. 正如您所说,基于xml仍然是功能而不是注释。 If you don't want to use XML based, and don't want setter method either, I think sessionFactory can be initialized by another variable to identify which bean used. 如果您不想使用基于XML,也不想使用setter方法,我认为sessionFactory可以由另一个变量初始化,以识别使用的bean。

For example, 例如,

public class GenericDAOImpl extends HibernateDAOSupport implements GenericDAO {
    private SessionFactory sessionFactory;

    public GenericDAOImpl(boolean tag) {
        super();
        ApplicationContext apx = new AnnotationConfigApplicationContext(xxxx.class);
        sessionFactory = tag ? (SessionFactory) apx.getBean("sessionFactory1")
                : (SessionFactory) apx.getBean("sessionFactory1");
    }
}

Of course, you need to specify the bean name for LocalSessionFactoryBean with @Bean(name = "sessionFactory1") and @Bean(name = "sessionFactory2") 当然,您需要为@Bean(name = "sessionFactory1")@Bean(name = "sessionFactory2")指定LocalSessionFactoryBean的bean名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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