gpt4 book ai didi

java - 将 SessionFactory 从 Spring XML-Config 注入(inject) Java-Config

转载 作者:太空宇宙 更新时间:2023-11-04 07:13:02 28 4
gpt4 key购买 nike

出于某种原因,我将 spring bean 配置为 java 配置,并且还有一些其他 bean 在 xml 中定义。现在我想将xml配置中定义的sessionfactory bean注入(inject)到java配置中的bean中。不幸的是,sessionfactory 为空,我得到一个 NullPointerException。怎么了?我正在使用 Spring 3.2.5。另外,@Inject 或@Autowired 而不是@Resource 不起作用。

这是相关部分...

Java 配置:

@Configuration
@ComponentScan
@ImportResource({ "classpath:beans-sessionfactory.xml" })
public class MyJavaConfig {

// defined in 'beans-sessionfactory.xml'
@Resource
//@Inject => also NPE
//@Autowired => also NPE
private AnnotationSessionFactoryBean sessionFactory;

@Bean
public MyDao getMyDao() {
final MyDao dao = new MyDao();
// Why is sessionFactory null?
dao.setSessionFactory(this.sessionFactory.getObject());
return dao;
}

// MyAppConfig extends PropertyPlaceholderConfigurer
@Bean
public MyAppConfig myAppConfig() {...}
...

XML 配置:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
</bean

单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyJavaConfig.class })
public class MyBeanWiringTest {
// test fails with NPE
}

几乎完整的 beans-sessionfactory.xml:

<?xml version="1.0" encoding="ISO-8859-15"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="true">...</bean>

<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">...</bean>

<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">...</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="hibernateProperties" ref="hibernateProperties"/>
<property name="annotatedClasses">
<list>
<value>MyEntity</value>
</list>
</property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="txManager"/>

</beans>

最佳答案

AnnotationSessionFactoryBeanFactoryBean<SessionFactory>这意味着它创建了一个 SessionFactory 。您可以注入(inject)结果而不是工厂。

@Configuration
@ComponentScan
@ImportResource({ "classpath:beans-sessionfactory.xml" })
public class MyJavaConfig {

@Autowired
private SessionFactory sessionFactory;

@Bean
public MyDao getMyDao() {
final MyDao dao = new MyDao();
// Why is sessionFactory null?
dao.setSessionFactory(this.sessionFactory);
return dao;
}

如果你真的想要AnnotationSessionFactoryBean你将不得不诉诸一些命名技巧。您必须注入(inject)一个名为“&sessionFactory”的 bean,注意 &这是向 spring 表明您想要的标记 FactoryBean而不是 FactoryBean 的结果.

@Autowired
@Qualifier("&sessionFactory")
private AnnotationSessionFactoryBean sessionFactory;

关于java - 将 SessionFactory 从 Spring XML-Config 注入(inject) Java-Config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20363365/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com