Java 无法将SessionFactory[null]自动连接到@Transactional bean中
在将Hibernate SessionFactory bean自动连接到我的服务中时,我遇到了一个非常奇怪的问题 我能够在Spring上下文对象中找到SessionFactorybean。因此,创建这样的bean没有问题。 但是当这个bean被标记为Java 无法将SessionFactory[null]自动连接到@Transactional bean中,java,spring,hibernate,autowired,Java,Spring,Hibernate,Autowired,在将Hibernate SessionFactory bean自动连接到我的服务中时,我遇到了一个非常奇怪的问题 我能够在Spring上下文对象中找到SessionFactorybean。因此,创建这样的bean没有问题。 但是当这个bean被标记为@Transactional注释时,我无法将它自动连接到我的服务中。工厂字段为null。 一旦我删除了这个注释,一切都很好 服务类别: @Service @Transactional public class ExampleRunner implem
@Transactional
注释时,我无法将它自动连接到我的服务中。工厂字段为null
。
一旦我删除了这个注释,一切都很好
服务类别:
@Service
@Transactional
public class ExampleRunner implements Runnable{
@Autowired
SessionFactory sessionFactory;
...
}
applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="classpath:application.properties"
ignore-resource-not-found="true" />
<context:component-scan base-package="org.edu" />
<context:annotation-config />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName:org.hsqldb.jdbcDriver}" />
<property name="url" value="${jdbc.url:jdbc:hsqldb:mem:myAppDb}" />
<property name="username" value="${jdbc.username:sa}" />
<property name="password" value="$jdbc.password:}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.edu" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect:org.hibernate.dialect.HSQLDialect}
</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto:create-drop}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql:true}</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager"
proxy-target-class="true" />
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
好像当我将@Transactional
作为类级注释时,Spring创建了CGLib代理bean,其中SessionFactory字段为null
。
但是当我使用@Transational
作为方法级注释时,一切都很好
所以我只想了解这种行为。
我从Spring文档中遗漏了什么
我使用的是Spring 4.1.4.RELEASE。我收到了您的错误,实际上是因为您不允许容器为您自动连接bean,因为您使用java Main方法,它不会自动连接bean,这就是您收到错误的原因。添加您的代码和Spring配置。@Jens,完成,请注意,将其放在类或方法上不应影响代理创建机制,因为您的类实现了接口。你如何/在哪里使用这个bean?请确保您自己没有创建实例。…。@M.Deinum接口实现在这里并不重要,因为我启用了CGLib代理创建,而不是JDK Dynamic。顺便说一句,我不会手动创建这个bean并尝试从上下文中获取。嗯,很有趣。关于您的上下文,请尝试从xsd文件中删除版本,以便
spring-beans.xsd
而不是spring-beans-3.0.xsd
,对于特定名称空间,指定版本会触发一些回退场景。删除
,因为
已经暗示了这一点
public static void main(String[] args) {
Runnable runner = new ClassPathXmlApplicationContext("applicationContext.xml").getBean(ExampleRunner.class);
runner.run();
}