0

正確な問題を理解するのに苦労しています。SpringBeanxmlからsessionFactoryを自動配線しようとすると、HibernatesessionFactoryに対してNullポインターエラーがスローされるのはなぜですか。誰かが私がこれを修正するのを手伝ってくれませんか。Springを初めて使用し、JBossサーバーにアプリをデプロイします

これがSpring-Config.xmlです

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    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.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:annotation-config />
    <context:component-scan base-package="org.kp.db.dao.impl" />


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

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="configLocation" value="classpath:/hibernate.cfg.xml" />
    </bean>

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

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

これが私がwebcontentフォルダーに置いたHiberante.cfg.xmlです

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ebm</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping resource="org/kp/db/model/Actioncode.hbm.xml"/>
    <mapping class="org.kp.db.model.TblEBMFieldDetails"/>
    <mapping class="org.kp.db.model.TbleOLI"/>
  </session-factory>
</hibernate-configuration>

そして、私がsessionFactoryにアクセスしようとしているDAOクラスは

package org.kp.db.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.kp.db.dao.ActionCodeDao;
import org.kp.db.model.Actioncode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Administrator
 */
@Repository
public class ActionCodeDaoImpl implements ActionCodeDao{

    @Override
    public String[] getActionCodes() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    @Autowired(required=true)
    SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.REQUIRED)
    public String getActionCode(int id) {

        //ApplicationContext context;

        Session  session = sessionFactory.getCurrentSession();
        Actioncode actionCode=null;
        actionCode = (Actioncode)session.get(Actioncode.class, 1);

        return actionCode.getActionName();
    }

}
4

2 に答える 2

1
@Named ("kpAction") 
public class Actions { 
    public String getActionCode() { 
        ActionCodeDao action =new ActionCodeDaoImpl(); 
        String str = action.getActionCode(1); System.out.println(str); return str; 
    } 
} 

このコード行を実行するActionCodeDao action =new ActionCodeDaoImpl();と、Springによって管理されていないため、自動配線された依存関係を含まないオブジェクトが取得されます。

それがあなたがnullポインタを見た理由だと思います。

しかし、あなたは今それを修正したと言います、あなたがそれをどのように修正したかわからない場合は、これも不注意に変更したかどうかはわかりません:)

于 2013-02-19T17:30:15.063 に答える
1

getterメソッドを使用したオブジェクトにアクセスする代わりに、問題を修正しました。これは機能しましたが、その背後にある本当の概念が何であるかわかりません。

public String getActionCode(int id) {

    //ApplicationContext context;

    Session  session = getSessionFactory().getCurrentSession();
    Actioncode actionCode=null;
    actionCode = (Actioncode)session.get(Actioncode.class, 1);

    return actionCode.getActionName();
}
于 2013-02-19T15:14:52.920 に答える