簡體   English   中英

spring-nullpointerexception-無法在無注釋類中訪問自動連接的帶注釋的服務(或dao)

[英]spring-nullpointerexception- cant access autowired annotated service (or dao) in a no-annotations class

我有這個問題,我無法解決。

從我的@Controller ,我可以輕松訪問我的自動裝配的@Service類並使用它沒有問題。 但是當我從沒有注釋的單獨類中執行此操作時,它會給我一個NullPointerException

我的控制器(工程) -

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

我獨立的Java類(不工作) -

public final class UsersManagementUtil {
  @Autowired
  UserService userService;

要么

@Autowired
UserDao userDao;

userService或userDao始終為null! 只是嘗試其中任何一個有效。

我的組件掃描設置具有用於掃描的根級別包,因此應該沒問題。

我的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>

任何線索?

來自Spring Reference 3.0

默認情況下,使用@Component@Repository@Service @Controller@Repository注釋的@Component或自身使用@Component注釋的自定義注釋是唯一檢測到的候選組件。

UsersManagementUtil應根據您的需要使用其中一個進行注釋。

Spring依賴注入僅適用於Spring管理的組件。 如果您的UsersManagementUtil不是由Spring管理的(即不是Spring bean),則@Autowired在其中不起作用。 將它聲明為Spring bean(使用<bean>或annotation),或者在使用對象實例化后手動觸發自動裝配

applicationContext.getAutowireCapableBeanFactory().autowireBean(object);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM