java - spring propery initialization issue -
java - spring propery initialization issue -
i have create service bean , declare variable . pseudocode : //one class
@service @transactional(propagation=propagation.requires_new) public class customerservice { public map<long,boolean> somemap= new hashmap<>(); }
//caller class
@component @path("/maincontroller") class maincaller{ @autowired customerservice customerservice; @post @path("/process") public response processcustomer() { try{ //some processing }finally{ synchronized (customerservice.somemap) { //liee 64 //do work on map } } }
i getting null pointer exception @liee dont understand this. far know spring creates bean after initializing properties. , declared somemap new variable should not null. why giving null pointer exception. error :
com.sun.jersey.spi.container.containerresponse mapmappablecontainerexception severe: runtimeexception not mapped response, re-throwing http container 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-context
<?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
<!-- bailiwick of jersey --> <dependency> <groupid>com.sun.jersey</groupid> <artifactid>jersey-server</artifactid> <version>1.8</version> </dependency>
just clarify:
reason: 1. using @transactional, need understand works aop, see here spring transaction management. 2. annotated service class, implements no interface, means spring utilize cglib implement transaction management. (if have interface, utilize proxy pattern implement transaction management). 3. cglib modifies byte code of class, causes npe
solution: utilize getter/setter pattern void direct access of public field.
misc: imo not practice utilize public field. static cache using map cause potential memory leak.
java spring spring-mvc jersey
Comments
Post a Comment