2010-11-27 38 views
6

J'ai ce problème que je ne peux pas résoudre.spring-nullpointerexception- ne peut pas accéder au service annoté autowired (ou dao) dans une classe sans annotations

Depuis mon @Controller, je peux facilement accéder à ma classe autogérée @Service et ne pas jouer avec. Mais quand je le fais à partir d'une classe séparée sans annotations, cela me donne un NullPointerException.

Mon contrôleur (travaux) -

@Controller 
public class UserController { 
@Autowired 
UserService userService;... 

Ma classe Java séparée (ne fonctionne pas) -

public final class UsersManagementUtil { 
    @Autowired 
    UserService userService; 

ou

@Autowired 
UserDao userDao; 

UserService ou userDao sont toujours nuls! Essayait juste si l'un d'entre eux fonctionne.

Mon paramètre de numérisation de composant a le package de niveau racine défini pour la numérisation, ce qui devrait être OK.

mon contexte de servlet -

<?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/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 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 
<!-- the application context definition for the 
     springapp DispatcherServlet --> 
<!-- Enable annotation driven controllers, validation etc... --> 

<context:property-placeholder location="classpath:jdbc.properties" /> 
<context:component-scan base-package="x" /> 
<tx:annotation-driven transaction-manager="hibernateTransactionManager" /> 

    <!-- package shortended --> 
<bean id="messageSource" 
class="o.s.c.s.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="/WEB-INF/messages" /> 
</bean> 

<bean id="dataSource" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="${database.driver}" /> 
    <property name="url" value="${database.url}" /> 
    <property name="username" value="${database.user}" /> 
    <property name="password" value="${database.password}" /> 
</bean> 

    <!-- package shortened --> 
<bean id="viewResolver" 
    class="o.s.w.s.v.InternalResourceViewResolver"> 

    <property name="prefix"> 
     <value>/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
    <property name="order"> 
     <value>0</value> 
    </property> 
</bean> 

     <!-- package shortened --> 
    <bean id="sessionFactory" 
     class="o.s.o.h3.a.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="annotatedClasses"> 
    <list> 
     <value>orion.core.models.Question</value> 
     <value>orion.core.models.User</value> 
     <value>orion.core.models.Space</value> 
     <value>orion.core.models.UserSkill</value> 
     <value>orion.core.models.Question</value> 
     <value>orion.core.models.Rating</value> 
    </list> 
    </property> 
     <property name="hibernateProperties"> 

     <props> 
      <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
     </props> 
     </property> 
</bean> 

<bean id="hibernateTransactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

Un indice?

+0

double possible de [Pourquoi mon null champ Spring @Autowired?] (Http: // stackoverflow .com/questions/19896870/pourquoi-est-mon-ressort-autowired-champ-nul) – chrylis

Répondre

6

De Spring Reference 3.0

Par défaut, les classes annotées avec @Component, @Repository, @Service, @Controller, ou une annotation personnalisée qui est lui-même annotés avec @Component sont les seuls détectés composants candidats.

UsersManagementUtil doit être annoté avec l'un d'entre eux en fonction de vos besoins.

+0

c'est ce que j'ai compris aussi bien mais ne fonctionnait pas pour moi .. @Component est détecté mais le haricot autowired est toujours nul .. pas d'erreur sinon – user286806

+0

Qu'en est-il de UserService, est ce printemps géré? (annoté ou défini dans votre fichier XML) –

4

L'injection de dépendance de ressort fonctionne uniquement dans les composants gérés par Spring. Si votre UsersManagementUtil n'est pas géré par Spring (ce n'est pas un bean Spring), @Autowired ne fonctionne pas à l'intérieur. Soit déclarer comme un grain de printemps (en utilisant <bean> ou annotation), ou déclencher Autowiring manuellement après l'instanciation de l'objet en utilisant

applicationContext.getAutowireCapableBeanFactory().autowireBean(object);