我將從我的項目結構開始。Spring @Autowired Bean NullPointerException - Not Getting Wired
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
我有@Autowired
SpecialClaimsCaseRepositoryService<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
。我不知道這是爲什麼,或者如何解決它。我已經完成並將該項目重構爲我認爲可行的工作,但仍然沒有運氣。我真的被困在這裏,不知道如何解決問題。
非常感謝您的任何幫助。如果需要其他東西,請詢問。
顯示爲'ApplicationController'的代碼,包括字段初始化。 – chrylis
嘗試給這個bean一個名字:'@Service(「specialClaimsCaseRepositoryService」)' – jalynn2
這幾乎肯定不是一個方面的問題,或者建設的背景下,當你會得到錯誤。 – chrylis