2014-06-26 5 views
0

J'ai créé un bean de service et déclaré une variable. Pseudocode: // une classeproblème d'initialisation de la propriété de printemps

@Service 
@Transactional(propagation=Propagation.REQUIRES_NEW) 
public class CustomerService { 
    public Map<Long,Boolean> someMap= new HashMap<>(); 
} 

// classe appelant

@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 
     } 

    } 
} 

Je reçois NullPointerException @liee un je ne comprends pas cela. Autant que je sache, le printemps crée du haricot après avoir initialisé toutes ses propriétés. et comme j'ai déclaré someMap avec une nouvelle variable, elle ne devrait pas être nulle. alors pourquoi il donne une exception de pointeur nul. ERREUR:

com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException GRAVES: Le RuntimeException ne pouvait pas être mis en correspondance avec une réponse, re-jeter au conteneur 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> 

// PRINTEMPS-CONTEXTE

<?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

Le MainCaller est-il également un haricot de printemps? Si oui, comment l'instanciez-vous? –

+0

pas son haricot de printemps – coreJavare

+0

'MainCaller' semble être un contrôleur Jersey. As-tu correctement intégré Spring et Jersey? S'il vous plaît envoyez votre 'web.xml' – geoand

Répondre

1

juste pour préciser:

raison:
1. vous utilisez @Transactional, vous besoin de comprendre cela fonctionne avec AOP, voir ici Spring transaction management.
2. Vous avez annoté votre classe de service, qui n'implémente aucune interface, ce qui signifie que Spring utilisera CGLIB pour implémenter la gestion des transactions. (Si vous avez une interface, il utilisera un modèle de proxy pour implémenter la gestion des transactions).
3. CGLIB modifie votre code d'octet de cette classe, ce qui provoque la NPE

Solution: utiliser modèle getter/setter d'annuler un accès direct du domaine public.

Divers: OMI Il n'est pas recommandé d'utiliser un champ public. Le cache statique utilisant la carte peut entraîner une fuite de mémoire potentielle.

Questions connexes