2014-07-11 176 views
-1

我將從我的項目結構開始。Spring @Autowired Bean NullPointerException - Not Getting Wired

enter image description here

SpecialClaimsCaseManager.java - 何處發生錯誤:

package com.redacted.sch.web.mvc.model; 

//Some imports that don't matter 

@Component 
public class SpecialClaimsCaseManager { 

    @Autowired 
    private SpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto> specialClaimsCaseRepositoryService; 

    public Collection<SpecialClaimsCase> findAll() { 
     return convertToSpecialClaimsCase(specialClaimsCaseRepositoryService.findAll()); //Error happens here. specialClaimsCaseRepositoryService is null 
    } 

//Some irrelevant code finishing up the class 

SpecialClaimsCaseRespositoryService - 類的其中需要被注入接口:

package com.redacted.sch.service; 

public interface SpecialClaimsCaseRepositoryService<C extends SpecialClaimsCaseDto> { 
    //Some irrelevant interface code 
} 

SpecialClaimsCaseRepositoryServiceImpl - 的類,它實現界面。這是應該注入的。

package com.redacted.sch.service.impl; 

@Service 
public class SpecialClaimsCaseRepositoryServiceImpl implements SpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto> { 

    @Autowired 
    private SpecialClaimsCaseRepository repository; //This autowire actually works. Not relevant to the problem, though. 

    //Some code... 
} 

ApplicationController中 - 控制器明顯

package com.redacted.web.mvc.controllers; 

//Some imports 

@Controller 
public class ApplicationController { 

     @RequestMapping(value="/test", method=RequestMethod.GET) 
     public @ResponseBody Collection<SpecialClaimsCase> getCaseInJSON(@RequestParam Map<String, String> requestParams, ModelMap model) { 
      SpecialClaimsCaseManager caseManager = new SpecialClaimsCaseManager(); 

      return new ArrayList<SpecialClaimsCase>(caseManager.findAll()); 
     } 
} 

我們獲得的配置,我們將與web.xml中開始:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <display-name>SpecialClaimsHandling</display-name> 

    <!-- Spring Configuration Files --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      WEB-INF/applicationContext.xml 
      WEB-INF/application-security.xml 
      classpath*:sch_model_spring.xml 
     </param-value> 
    </context-param> 

    <!-- Spring Security Filters --> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class> 
      org.springframework.web.filter.DelegatingFilterProxy 
     </filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <!-- Spring Listeners --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- MVC Filter --> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

    <!-- Session Configuration --> 
    <session-config> 
     <session-timeout>5</session-timeout> 
    </session-config> 

</web-app> 

與調度員-servlet.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/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="com.redacted.sch.web.mvc.controllers"/> 

    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <mvc:annotation-driven /> 

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

</beans> 

applicat ionContext.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/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="com.redacted.sch.web.mvc.controllers"/> 

    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <mvc:annotation-driven /> 

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

</beans> 

最後sch_model_spring.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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.xsd"> 

     <context:component-scan base-package="com.redacted.repository.jpa, 
       com.redacted.sch.domain.model, 
       com.redacted.sch.repository.jpa, 
       com.redacted.sch.service, 
       com.redacted.sch.service.impl"/> 

     <tx:annotation-driven /> 

     <tx:jta-transaction-manager /> 

     <!-- Data source used for testing --> 
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
      <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" /> 
      <property name="url" value="redacted.doesntmatter.com" /> 
      <property name="username" value="redacted" /> 
      <property name="password" value="redacted" /> 
     </bean> 

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
       <property name="persistenceUnitName" value="schManager" /> 
       <property name="dataSource" ref="dataSource" /> 
     </bean> 

     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
      <property name="entityManagerFactory" ref="entityManagerFactory" /> 
     </bean> 
</beans> 

OK,這樣的配置文件,類和結構牆後,讓我們的問題。在SpecialClaimsCaseManager我有@AutowiredSpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto>對象,它實際上沒有連線。當談到被使用,被拋出NPE是以下堆棧跟蹤:

(短這裏,通過fpaste提供完整的東西)

[7/10/14 15:20:13:343 CDT] 000000fe webapp  E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[dispatcher]: java.lang.NullPointerException 
    at com.redacted.sch.web.mvc.model.SpecialClaimsCaseManager.findAll(SpecialClaimsCaseManager.java:26) 
    at com.redacted.sch.web.mvc.controllers.ApplicationController.getCaseInJSON(ApplicationController.java:43) 
    ........ 

從我以前幫助雲集,這是上下文問題。 SpecialClaimsCaseManager所在的上下文無權訪問SpecialClaimsCaseRepositoryService。我不知道這是爲什麼,或者如何解決它。我已經完成並將該項目重構爲我認爲可行的工作,但仍然沒有運氣。我真的被困在這裏,不知道如何解決問題。

非常感謝您的任何幫助。如果需要其他東西,請詢問。

+0

顯示爲'ApplicationController'的代碼,包括字段初始化。 – chrylis

+0

嘗試給這個bean一個名字:'@Service(「specialClaimsCaseRepositoryService」)' – jalynn2

+0

這幾乎肯定不是一個方面的問題,或者建設的背景下,當你會得到錯誤。 – chrylis

回答

3

你使用new操作符它不是Spring管理的時刻。

控制器更改爲:

@Controller 
    public class ApplicationController { 
      @Autowired 
      SpecialClaimsCaseManager caseManager; 

      @RequestMapping(value="/test", method=RequestMethod.GET) 
      public @ResponseBody Collection<SpecialClaimsCase> getCaseInJSON(@RequestParam Map<String, String> requestParams, ModelMap model) { 
       .... 
       return new ArrayList<SpecialClaimsCase>(caseManager.findAll()); 
      } 
    } 
+0

這是很好的知道。我應該只是自動裝配的一切嗎? (這樣做釋放了一個新的錯誤,我會盡快標記這是正確的能夠確認) – jready

+0

如果你想讓它在春天的環境中使用,它應該是Spring管理 – Prasad

+0

這樣做產生了一個IllegalStateException:A JTA EntityManager的不能使用getTransaction()這是新的問題值得 – jready

0

難道你的classpath def在web.xml中是錯誤的嗎?

classpath*:sch_model_spring.xml 

我normall會使用類似:

classpath:/spring/beans-datasource.xml 
+0

已經嘗試過。沒有區別 – jready

+0

這不是很好-1。祝你好運\ –

+0

我沒有-1你:( – jready