【问题标题】:Unable autowire SessionFactory[null] into @Transactional bean无法将 SessionFactory[null] 自动装配到 @Transactional bean
【发布时间】:2015-01-21 13:00:41
【问题描述】:

在将 Hibernate SessionFactory bean 自动装配到我的服务中时,我遇到了非常奇怪的问题。

我能够在 Spring Context 对象中找到 SessionFactory bean。所以创建这样的bean没有问题。 但是当它用@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>

用法:

public static void main(String[] args) {
    Runnable runner =  new ClassPathXmlApplicationContext("applicationContext.xml").getBean(ExampleRunner.class);
    runner.run();
}

似乎当我将@Transactional 作为类级别注释时,Spring 创建了 CGLib 代理 bean,其中 SessionFactory 字段为null。 但是当我使用@Transational 作为方法级注解时,一切正常。

所以我只想了解这种行为。 我从 Spring 文档中遗漏了什么?

我使用 Spring 4.1.4.RELEASE。

【问题讨论】:

  • 添加你的代码和你的 spring 配置。
  • @Jens,完成,请看一下
  • 将它放在类或方法中不应影响代理创建机制,因为您的类实现了接口。你如何/在哪里使用这个 bean?确保您没有自己创建实例...
  • @M.Deinum 接口实现在这里无关紧要,因为我启用了 CGLib 代理创建而不是 JDK 动态。顺便说一句,我不会手动创建这个 bean 并尝试从上下文中获取。
  • 嗯,很有趣。关于您的上下文的几件事,尝试从 xsd 文件中删除版本,因此 spring-beans.xsd 而不是 spring-beans-3.0.xsd,对于某些指定版本的命名空间会触发一些回退场景。删除 &lt;context:annotation-config /&gt;,因为 &lt;context:component-scan /&gt; 已经暗示了这一点。

标签: java spring hibernate autowired


【解决方案1】:

我得到了你的错误,实际上你为什么在这里遇到问题,因为你不允许容器为你自动连接 bean,因为你使用 java Main 方法它不会自动连接 bean,这就是你得到错误的原因。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 2018-12-27
  • 2020-10-22
  • 2016-09-27
  • 2015-07-05
  • 2012-12-04
相关资源
最近更新 更多