簡體   English   中英

在Spring 4和Hibernate 4中,SessionFactory為null

[英]SessionFactory is null with Spring 4 and Hibernate 4

當我嘗試將數據保存到DB時, SessionFactorynull 查找下面的代碼。

彈簧配置

<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"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Specifying base package of the Components like Controller, Service, DAO -->
<context:component-scan base-package="com.service.jaxws" />

<!-- Getting Database properties -->
<!-- <context:property-placeholder location="classpath:application.properties" /> -->

<mvc:annotation-driven />

<!-- Specifying the Resource location to load JS, CSS, Images etc -->
<mvc:resources mapping="/resources/**" location="/resources/" />

<!-- View Resolver -->
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

<!-- DataSource -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    id="dataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/hrms"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="packagesToScan" value="com.service.jaxws"></property>
</bean>

<!-- Transaction -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

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

倉庫豆

@Repository
public class UserDAOImpl implements UserDAO {

  @Autowired
  private SessionFactory sessionFactory;

  @Override
  public void addUser(UserBO user) {        
    if (sessionFactory != null) {
        sessionFactory.getCurrentSession().save(user);
    } else {
        System.out.println("Null");
    }       
  }
}

每次調試時, SessionFactorynull 誰能告訴我我想念的東西嗎?

在此先感謝Serin

馬特的問題已解決。 主要問題是@Autowired。 它沒有按預期工作。 我在@Webservice類中嘗試過此操作。 使用擴展SpringBeanAutowiringSupport后,它可以正常工作。 無論如何,感謝您的支持。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM