注入sessionFactory获取null

时间:2016-03-15 01:24:27

标签: java hibernate

当我尝试调用getSession()时,它返回null。我还在'application-service.xml'中定义了sessionFactory的bean。我对hibernate完全不熟悉,所以我想得到一些帮助。感谢任何答案;)

  

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

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

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                <value>com.gtis.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <!--  <prop key="hibernate.current_session_context_class">thread</prop> -->
            </props>
        </property>
    </bean>

    <!--  class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" -->
    <!--  class="org.springframework.jdbc.datasource.DriverManagerDataSource" -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.pass}"/>
    </bean>

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

    <!-- <bean id="persistenceExceptionTranslationPostProcessor"
           class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />-->

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="get*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <aop:config expose-proxy="true">
        <aop:pointcut id="txPointcut" expression="execution(* com.gtis.service.*.*(..))"/>
        <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
    </aop:config>

</beans>
  

应用context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

       <context:annotation-config/>

       <context:component-scan base-package="com.gtis.*"/>

       <mvc:default-servlet-handler/>

       <mvc:annotation-driven/>

       <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
              <property name="contentType" value="text/html; charset=utf-8"/>
              <property name="cache" value="true" />
              <property name="suffix" value=".ftl" />
              <property name="order" value="2"/>
       </bean>

       <bean id="freemarkerConfig"
             class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
              <property name="templateLoaderPath" value="/WEB-INF/view/" />
              <property name="freemarkerSettings">
                     <props>
                            <prop key="template_update_delay">0</prop>
                            <prop key="default_encoding">UTF-8</prop>
                            <prop key="number_format">0.##########</prop>
                            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                            <prop key="classic_compatible">true</prop>
                            <prop key="template_exception_handler">ignore</prop>
                     </props>
              </property>
       </bean>

       <import resource="app-service.xml"/>
       <import resource="hibernate.cfg.xml"/>

</beans>
  

UserDao.java

public class UserDao{
    @Autowired
    SessionFactory sessionFactory;

    public Session getSession() {
        Session session = sessionFactory.getCurrentSession();
        return session;
    }

    public User getUser(String userId){
        return (User)getSession().get(User.class,userId);
    }

    public List<User> getAllUser(){
        String hql = "from com.gtis.model.User";
        Query query = getSession().createQuery(hql);
        return query.list();
    }

    public void save(User user){
        getSession().saveOrUpdate(user);
    }

    public void delete(User user){
        getSession().delete(user);
    }
}
  

应用service.xml中

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

       <bean id="userDao" class="com.gtis.Dao.UserDao" scope="prototype"/>
</beans>

0 个答案:

没有答案
相关问题