2

I have a class FichierCommunRetriever that use the @Value annotation of Spring. But I am struggling to make it work.

So in my application.properties I have :

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

My class FichierCommunRetriever is using those entries with the following code :

public class FichierCommunRetriever implements Runnable {

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

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

We are loading application.properties with the following code in the class ApplicationConfig:

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

In ApplicationConfig, I am defining a bean that use FichierCommunRetriever in a new thread like that :

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

I suppose that my problem is, since FichierCommunRetriever is running in a separate thread, the class can't reach the applicationContext and is not able to give a value.

I'm wondering if the annotation would work or I must change the way I'm getting those values?

4

2 回答 2

2

In your applicationConfig you should define your bean this way:

@Configuration
public class AppConfig {

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

}

Then, after Spring loads, you can access you bean via the application context

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

Now you are sure that your bean lives inside Spring context and that it is initialized. In addition, in your Spring XML, you have to load the properties (this example uses the context namespace):

<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 回答
2

You create an instance of FichierCommunRetriever using new, instead of asking Spring to return a bean instance. So Spring isn't controlling the creation and injection of this instance.

You should have the following method in your config class, and call it to get the bean instance:

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

...
     Thread threadToExecuteTask = new Thread(fichierCommunRetriever());
于 2013-04-04T18:37:32.430 回答