2014-06-26 93 views
0

我已經創建了一個服務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> 
+0

MainCaller是否也是Spring Bean?如果是這樣,你如何實例化它? –

+0

不是它的不是春豆 – coreJavare

+0

'MainCaller'似乎是一個澤西控制器。你有沒有正確整合Spring和Jersey?請發佈您的'web.xml' – geoand

回答

1

只是爲了澄清:

原因:您正在使用@Transactional
1,所以你需要了解它適用於AOP,請參閱Spring transaction management
2.您註釋了服務類,它不實現接口,這意味着spring將使用CGLIB來實現事務管理。 (如果你有一個接口,它將使用代理模式來實現事務管理)。
3. CGLIB修改本次課程的字節代碼,這會導致NPE

解決方案:使用的getter/setter模式作廢公共領域的直接訪問。

雜項:國際海事組織它是不是一個好的做法,使用公共領域。使用映射的靜態緩存可能會導致內存泄漏。