0

messageSourceコンポーネントクラスの 1 つに Beanを注入しようとしています。

以下は Bean xml の一部です。

<context:annotation-config />

<context:component-scan base-package="com.mattis.test"/>

<bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>

<bean id="messageSource"
      class="org.springframework.context.support.ResourceBundleMessageSource">
      <property name="basename" value="messages" />
</bean>

そして、私のコンポーネントではこれを持っています:

@Component
public class TestClass {

@Autowired
private MessageSource messageSource;

<-- more code goes here -->
}

TestClass をインスタンス化するときは常に、messageSource は null です。Bean xml とクラス構成をさらに試しましたが、どれも機能しませんでした。

4

1 に答える 1

5

を手動で呼び出していnew TestClass()ます。Spring オートワイヤリングはマネージド Bean でのみ機能します。

于 2013-10-21T06:43:00.560 に答える