2

の注釈FichierCommunRetrieverを使用するクラスがあります。しかし、私はそれを機能させるのに苦労しています。@ValueSpring

だから私application.propertiesは持っています:

application.donneeCommuneDossier=C\:\\test
application.destinationDonneeCommuneDossier=C\:\\dev\\repertoireDonneeCommune\\Cobol

私のクラスFichierCommunRetrieverは、次のコードでこれらのエントリを使用しています:

public class FichierCommunRetriever implements Runnable {

    @Value("${application.donneeCommuneDossier}")
    private String fichierCommunDossierPath;

    @Value("${application.destinationDonneeCommuneDossier}")
    private String destinationFichierCommunDossierPath;
}

application.propertiesクラスに次のコードをロードしていますApplicationConfig

@ImportResource("classpath:/com/folder/folder/folder/folder/folder/applicationContext.xml")

では、そのような新しいスレッドで使用する をApplicationConfig定義しています:beanFichierCommunRetriever

Thread threadToExecuteTask = new Thread(new FichierCommunRetriever());
threadToExecuteTask.start();

私の問題は、FichierCommunRetriever別のスレッドで実行されているため、クラスがに到達できずapplicationContext、値を与えることができないことだと思います。

注釈が機能するかどうか、またはそれらの値を取得する方法を変更する必要があるかどうか疑問に思っていますか?

4

2 に答える 2

2

newSpring に Bean インスタンスを返すように要求する代わりに、 を使用して FichierCommunRetriever のインスタンスを作成します。そのため、Spring はこのインスタンスの作成と注入を制御していません。

構成クラスに次のメソッドがあり、それを呼び出して Bean インスタンスを取得する必要があります。

@Bean
public FichierCommunRetriever fichierCommunRetriever() {
    return new FichierCommunRetriever();
}

...
     Thread threadToExecuteTask = new Thread(fichierCommunRetriever());
于 2013-04-04T18:37:32.430 に答える
2

applicationConfig では、Bean を次のように定義する必要があります。

@Configuration
public class AppConfig {

    @Bean
    public FichierCommunRetriever fichierCommunRetriever() {
        return new FichierCommunRetriever();
    }

}

次に、Spring がロードされた後、アプリケーション コンテキストを介して Bean にアクセスできます。

FichierCommunRetriever f = applicationContext.getBean(FichierCommunRetriever.class);
Thread threadToExecuteTask = new Thread(f);
threadToExecuteTask.start();

これで、Bean が Spring コンテキスト内にあり、初期化されていることを確認できました。さらに、Spring XML では、プロパティをロードする必要があります (この例では context 名前空間を使用しています)。

<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"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

...

<context:property-placeholder location="classpath:application.properties" />

...

</beans>
于 2013-04-04T18:37:25.950 に答える