我将从我的项目结构开始。
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 - Controller显然
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>
和dispatcher-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>
的applicationContext.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>
好的,所以在配置文件,类和结构的墙之后,让我们解决问题。在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
。我不知道为什么会这样,或者如何解决它。我已经完成了项目并将其重构为我认为可以工作的项目,但仍然没有运气。我真的被困在这里,不知道如何解决这个问题。
非常感谢您的帮助。如果还有其他需要,请询问。
答案 0 :(得分: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());
}
}
答案 1 :(得分:0)
可能是你的classpath def在web.xml中出错吗?
classpath*:sch_model_spring.xml
我也会使用类似的东西:
classpath:/spring/beans-datasource.xml