0

特に Bean で、注釈の使用方法を理解するのに問題があります。

私は1つのコンポーネントを持っています

@Component
public class CommonJMSProducer

そして、それを別のクラスで使用したいのですが、それを使用して一意のオブジェクトを作成できると思いました

public class ArjelMessageSenderThread extends Thread {
    @Inject
    CommonJMSProducer commonJMSProducer;

ただし、commonJMSProducer は null です。

私の appContext.xml には、次のものがあります。

<context:component-scan base-package="com.carnot.amm" />

ありがとう

4

3 に答える 3

1

この自動配線機能を使用するには、Spring を構成する必要があります。

<context:annotation-config/>

注釈ベースの構成の詳細については、こちらを参照してください

ArjelMessageSenderThreadまた、Spring で管理する必要があります。そうしないと、Spring はそれを認識しないため、メンバーが改ざんされません。

また

マネージド Bean にできない場合は、次のようなことができます。

ApplicationContext ctx = ...
ArjelMessageSenderThread someBeanNotCreatedBySpring = ...
ctx.getAutowireCapableBeanFactory().autowireBeanProperties(
    someBeanNotCreatedBySpring,
    AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, true);

また

他の人が指摘したように、アノテーションを使用して、Spring によって@Configurableアノテーションで作成されていないオブジェクトに依存性注入を使用できます。

于 2013-10-24T16:35:30.350 に答える
0

注釈の代わりに XML を使用しました。これは大したことではないように思えました。現在、私はこれをxmlに追加しています

<bean id="jmsFactoryCoffre" class="org.apache.activemq.pool.PooledConnectionFactory"
    destroy-method="stop">
    <constructor-arg name="brokerURL" type="java.lang.String"
        value="${brokerURL-coffre}" />
</bean>

<bean id="jmsTemplateCoffre" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory">
        <ref local="jmsFactoryCoffre" />
    </property>
</bean>

<bean id="commonJMSProducer"
    class="com.carnot.CommonJMSProducer">
    <property name="jmsTemplate" ref="jmsTemplateCoffre" />
</bean>

そして、Bean を取得する別のクラス

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

とにかくありがとう

于 2013-10-25T09:16:09.687 に答える
0

のインスタンスをどのように作成するかによって異なりますArjelMessageSenderThread

Spring によって作成される必要がある Bean の場合ArjelMessageSenderThreadは、追加するだけです@Component(コンポーネント スキャンによってパッケージが取得されるようにします)。

ただし、拡張するのでThread、これは標準のSpring Beanであるべきではないと思います。ArjelMessageSenderThreadを使用して自分自身のインスタンスを作成する場合は、注釈を にnew追加する必要があります。インスタンスが Spring によって作成されていない場合でも、依存関係が注入されます。詳細については @Configurable のドキュメントを参照し、ロード時間ウィービングが有効になっていることを確認してください。@ConfigurableArjelMessageSenderThread@Configurable

于 2013-10-24T16:43:25.713 に答える