2010-11-27 17 views
6

Bu sorunu çözemediğim bir sorun var. benim @Controller itibarenSpring-nullpointerexception erişilemeyen açıklamalı bir hizmet (veya dao) sınıflandırma notu

i kolayca benim autowired @Service sınıf erişebilir ve onunla hiçbir sorun oynayabilir. Ama bunu ek açıklama yapmadan ayrı bir sınıftan yaptığımda, bana NullPointerException veriyor.

Benim Kontrolörü (çalışıyor) -

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

My ayrı Java sınıfı (çalışmıyor) -

public final class UsersManagementUtil { 
    @Autowired 
    UserService userService; 

veya

@Autowired 
UserDao userDao; 

userService veya userDao hep boş vardır! Sadece herhangi biri çalışıyorsa çalışıyordum.

Bileşen tarama ayarım tarama için ayarlanan kök düzey paketine sahip, bu nedenle Tamam olması gerekir.

benim servlet bağlam -

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

Herhangi ipucu?

+0

olası yinelenen kullanarak nesne olarak örneklenmesi sonra elle Autowiring tetikleyebilir [neden Bahar @Autowired alan null?] (Http: // stackoverflow .com/questions/19896870/why-is-my-spring-autowired-alan-null) – chrylis

cevap

6

Varsayılan olarak

Spring Reference 3.0 itibaren, sınıflar @Component, @Repository, @Service, @Controller ile açıklamalı ya kendisi @Component ile açıklamalı olduğunu özel açıklama sadece tespit aday bileşenleridir.

UsersManagementUtil üzerinde gereken göre bunlardan biri ile açıklamalı edilmelidir.

+0

Bu benim için ne anladım ama benim için çalışmadı .. @Component algılandı ama otomatik olarak bekletilen fasulye hala boş .. Hata yok – user286806

+0

UserService hakkında, bu yay yönetiliyor mu? (açıklamalı veya xml'de tanımlanmış) –

4

Yay bağımlılığı enjeksiyonu, yalnızca Spring tarafından yönetilen bileşenlerde çalışır. UsersManagementUtil'unuz Spring tarafından yönetilmiyorsa (yani, bir Spring bean değil), @Autowired bunun içinde çalışmaz. Ya (<bean> veya ek açıklama kullanarak) Bahar fasulye olarak ilan, ya

applicationContext.getAutowireCapableBeanFactory().autowireBean(object); 
İlgili konular