会话工厂配置通过应用程序上下文,连接已初始化但会话工厂始终指向null

时间:2016-08-02 11:24:02

标签: java mysql spring hibernate sessionfactory

抱歉,我正在重复我的问题,因为错误未得到纠正。 我将servicedao图层作为接口。我没有制作任何hibernate cfg xml文件,但我在应用程序上下文中编写了所有hibernate配置。

这只是一个测试程序

当我尝试从实体类中获取用户名和密码时,会话工厂始终指向null。如果您需要更具体的代码,请提及它。

应用程序上下文:

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <context:component-scan base-package="com.company"/>
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>/WEB-INF/*.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" 
        p:username="${jdbc.username}" p:password="${jdbc.password}">

    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.company.entity"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.Dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</prop>

            </props>
        </property>
        <property name="entityInterceptor">
            <bean class="com.company.interceptor.AuditInterceptor"/>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>  
    </bean>
   <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributes">
        <props>
            <prop key="create*">PROPAGATION_REQUIRED</prop>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>            
    </bean>


    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">

            <value>*DaoImpl</value>
        </property>
       <property name="interceptorNames">
           <value>transactionInterceptor</value>
       </property>
    </bean>

    <import resource="spring-security.xml"/>

</beans>

用户Dao Impl:

 @Repository
 public class UserDaoImpl extends AbstractDao  implements UserDao {

 public boolean test(){
    int id=1;
    boolean result;
    try{

        User user=null;
        user=(User) this.sessionFactory.getCurrentSession().get(User.class,id);
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        result=true;
        return result;
     }catch(NullPointerException n){
         System.out.println("inside null pointer exception handler");
         n.printStackTrace();
         result=false;
         return result;
     }

UserServive:

@Service
public class UserServiceImpl implements UserService {

    public boolean test(){
        UserDao userDao=new UserDaoImpl();
        return userDao.test();
    }
}

UserController中:

 @Controller
 public class LoginController {

 private Logger logger;

 @Autowired
 UserService userService;

 @RequestMapping(value="/login.do",method=RequestMethod.POST)
 public String login(HttpServletRequest request,HttpServletResponse response){

     boolean result=userService.test();
     if(result=true){
          return "profile";
     }else{
          return "home";
     }
}

摘要道:

public class AbstractDao{
    @Autowired
    protected SessionFactory sessionFactory;
}

1 个答案:

答案 0 :(得分:0)

exchange:routingKey更改为

AbstractDao

然后,从test()方法,您可以为public class AbstractDao extends HibernateDaoSupport { @Autowired protected SessionFactory sessionFactory; }

致电getSession()getHibernateTemplate()

Read this了解更多信息。