Java @值注释不';不返回值

Java @值注释不';不返回值,java,spring,spring-annotations,Java,Spring,Spring Annotations,我有一个类FichierCommunRetriever,它使用Spring的@Value注释。但我正在努力让它发挥作用 因此,在我的应用程序.properties中,我有: application.donneeCommuneDossier=C\:\\test application.destinationDonneeCommuneDossier=C\:\\dev\\repertoireDonneeCommune\\Cobol 我的类FichierCommunRetriever正在将这些条目与以

我有一个类
FichierCommunRetriever
,它使用
Spring
@Value
注释。但我正在努力让它发挥作用

因此,在我的
应用程序.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;
}
我们正在类
ApplicationConfig
中加载带有以下代码的
application.properties

@ImportResource("classpath:/com/folder/folder/folder/folder/folder/applicationContext.xml")
ApplicationConfig
中,我定义了一个
bean
,它在一个新线程中使用
FichierCommunRetriever
,如下所示:

Thread threadToExecuteTask = new Thread(new FichierCommunRetriever());
threadToExecuteTask.start();
我假设我的问题是,由于
FichierCommunRetriever
在单独的线程中运行,因此该类无法访问
applicationContext
,并且无法给出值


我想知道注释是否有效,或者我必须更改获取这些值的方式?

在应用程序配置中,您应该以以下方式定义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上下文中,并且已经初始化。 此外,在SpringXML中,必须加载属性(本例使用上下文名称空间):


...
...

在应用程序配置中,您应该这样定义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上下文中,并且已经初始化。 此外,在SpringXML中,必须加载属性(本例使用上下文名称空间):


...
...

您可以使用
new
创建FichierCommunRetriever的实例,而不是要求Spring返回bean实例。所以Spring并没有控制这个实例的创建和注入

您的config类中应该有以下方法,并调用它以获取bean实例:

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

...
     Thread threadToExecuteTask = new Thread(fichierCommunRetriever());

您可以使用
new
创建FichierCommunRetriever的实例,而不是要求Spring返回bean实例。所以Spring并没有控制这个实例的创建和注入

您的config类中应该有以下方法,并调用它以获取bean实例:

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

...
     Thread threadToExecuteTask = new Thread(fichierCommunRetriever());