2013-06-19 70 views
0

很抱歉,如果我惹了我的問題,我剛入門配置MVC,春,等....彈簧自動裝配Autowired服務集合上部署,而是叫時的空

,我運行到是問題在我的Spring MVC項目中,當我的應用程序部署時(通過在Spring Tool Suite中設置斷點進行驗證),我的所有Autowired服務都已成功自動裝入,但是當我嘗試調用自動裝入服務的方法時,我遇到了NullPointerException。我在不同的配置文件中查看了所有的東西,看起來像我的servelt環境,根環境和web XML文件應該沒問題(不要引用我的意思,它們包含在下面)。

我想知道如果我不調用在部署時創建的服務實例?看起來很奇怪的是Autowired setter可以被成功調用,但是這些服務最終都是空的。任何幫助,您可以提供非常感謝!

根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" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
     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-3.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 
<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.OracleDialect 
      </prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
    <property name="packagesToScan" value="com.byteslounge.spring.tx.model" /> 
</bean> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close"> 
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDrive" /> 
    <property name="url" 
     value="jdbc:oracle:thin:@**********" /> 
    <property name="username" value="*******" /> 
    <property name="password" value="*******" /> 
</bean> 

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

<tx:annotation-driven /> 

<context:annotation-config /> 

<bean 
    class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 

<context:component-scan base-package="com.clm.billing" /> 

的servelt-context.xml中:

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
     infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <mvc:annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources in the ${webappRoot}/resources directory --> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
     in the /WEB-INF/views directory --> 
    <beans:bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

</beans:beans> 

web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>OrderList</display-name> 
    <servlet-name>OrderList</servlet-name> 
    <servlet-class>com.clm.billing.Oracle.OrderList</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>OrderList</servlet-name> 
    <url-pattern>/OrderList</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>EditBilling</display-name> 
    <servlet-name>EditBilling</servlet-name> 
    <servlet-class>com.clm.billing.EditBilling</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>EditBilling</servlet-name> 
    <url-pattern>/EditBilling</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>UpdateBilling</display-name> 
    <servlet-name>UpdateBilling</servlet-name> 
    <servlet-class>com.clm.billing.UpdateBilling</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>UpdateBilling</servlet-name> 
    <url-pattern>/UpdateBilling</url-pattern> 
    </servlet-mapping> 
</web-app> 

InsuranceNoteDBServiceImpl: (這裏日ËsetiNoteDAO被稱爲與iNoteDAO類的實例,但是當getNoteById被稱爲iNoteDAO爲null)

@Service 
public class InsuranceNoteDBServiceImpl implements InsuranceNoteDBService { 

    private InsuranceNoteDAO iNoteDAO; 

    @Autowired 
    public void setiNoteDAO(InsuranceNoteDAO iNoteDAO) { 
     this.iNoteDAO = iNoteDAO; 
    } 

    @Override 
    @Transactional 
    public void insertNote(BillerNote note) { 
     iNoteDAO.insertNote(note); 

    } 

    @Override 
    @Transactional 
    public BillerNote getNoteById(int noteId) { 
     return iNoteDAO.getNoteById(noteId); 
    } 

} 

InsuranceNoteDBService:

public interface InsuranceNoteDBService { 

    void insertNote(BillerNote note); 

    BillerNote getNoteById(int noteId); 

} 

InsuranceNoteDAOImpl: (同樣的事情發生在這裏使用init()方法被設置會話工廠)

@Service 
public class InsuranceNoteDAOImpl implements InsuranceNoteDAO { 

    public SessionFactory sessionFactory; 

    @Autowired 
    public void init(SessionFactory factory) { 
     setSessionFactory(factory); 
    } 

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

    @Override 
    public void insertNote(BillerNote note) { 
     sessionFactory.getCurrentSession().save(note); 
    } 

    @Override 
    public BillerNote getNoteById(int noteId) { 
     return (BillerNote) sessionFactory. 
        getCurrentSession(). 
        get(BillerNote.class, noteId); 
    } 

} 

InsuranceNoteDAO:

public interface InsuranceNoteDAO { 

    void insertNote(BillerNote note); 

    BillerNote getNoteById(int noteId); 

} 
+0

爲iNodeDao的制定者似乎不正確。它應該是setINodeDao。你是自動生成二傳手還是手工編寫的? –

+0

@VaibhavRaj - 我用手寫了它,而不是讓STS生成它,但名稱應該不重要,它正在使用InsuranceNoteDAO實例調用。 – grahamcr

+0

@TheNewIdiot - 我不應該在spring上下文xml中定義bean,它們應該由Spring的預處理器根據賦予類的註釋來提取。這是由 grahamcr

回答

0

刪除init方法和註釋二傳手:

@Autowired 
public void setSessionFactory(SessionFactory factory) { 
    this.sessionFactory = factory; 
} 
+0

謝謝您的建議!我像你說的那樣刪除了init方法,並用@Autowired註釋了setter,但仍然和以前一樣。 – grahamcr