6

解決できないこの問題があります。

私の から、自動配線されたクラス@Controllerに簡単にアクセスして、問題なく遊ぶことができます。@Serviceしかし、注釈なしで別のクラスからそれを行うと、NullPointerException.

私のコントローラー(動作)-

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

私の別のJavaクラス(動作していません)-

public final class UsersManagementUtil {
  @Autowired
  UserService userService;

また

@Autowired
UserDao userDao;

userService または userDao は常に null です。それらのいずれかが機能するかどうかを試してみました。

私のコンポーネント スキャン設定には、スキャン用のルート レベル パッケージが設定されているので、問題ありません。

私のサーブレットコンテキスト -

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

どんな手掛かり?

4

2 に答える 2

6

SpringReference3.0から

デフォルトでは、、、、、、、またはそれ自体にアノテーションが付けられたカスタムアノテーションが付けられたクラス は 、検出 @Componentされた唯一の候補コンポーネントです。@Repository@Service@Controller@Component

UsersManagementUtil必要に応じて、そのうちの1つに注釈を付ける必要があります。

于 2010-11-27T04:34:29.580 に答える
4

Spring 依存性注入は、Spring によって管理されるコンポーネントでのみ機能します。Spring によって管理されてUsersManagementUtilいない (つまり、Spring Bean ではない) 場合、@Autowiredその内部では機能しません。Spring Bean として宣言するか (<bean>または注釈を使用)、オブジェクトのインスタンス化後に手動で自動配線をトリガーします。

applicationContext.getAutowireCapableBeanFactory().autowireBean(object);
于 2010-11-27T09:33:06.807 に答える