我已經創建了一個服務bean並聲明瞭變量。僞代碼:// 一類春季原始初始化問題
@Service
@Transactional(propagation=Propagation.REQUIRES_NEW)
public class CustomerService {
public Map<Long,Boolean> someMap= new HashMap<>();
}
//調用者類
@Component
@Path("/maincontroller")
class MainCaller{
@Autowired
CustomerService customerService;
@POST
@Path("/process")
public Response processCustomer() {
try{
//some processing
}finally{
synchronized (customerService.someMap) { //liee 64
//do some work on map
}
}
}
我得到空指針異常@liee一個 我不明白這一點。據我所知,spring在初始化所有屬性後創建bean。當我用new變量聲明someMap時,它不應該爲null。那麼它爲什麼會給出空指針異常。 ERROR:
com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException重度:所述的RuntimeException不能 被映射到的響應,重新投擲到HTTP容器 顯示java.lang.NullPointerException在 org.hungama.controller.MainCaller.processCustomer(MainCaller.java:64)
//Web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/rest-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>
com.sun.jersey.config.property.packages
</param-name>
<param-value>org.test.controller</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
// SPRING上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<import resource="rest-datasource.xml" />
<context:component-scan base-package="org.test" />
<mvc:annotation-driven />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/retailser_detaults.properties</value>
<value>classpath:/log4j.properties</value>
</list>
</property>
</bean>
</beans>
//pom.xml
<!-- Jersey -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
MainCaller是否也是Spring Bean?如果是這樣,你如何實例化它? –
不是它的不是春豆 – coreJavare
'MainCaller'似乎是一個澤西控制器。你有沒有正確整合Spring和Jersey?請發佈您的'web.xml' – geoand