的Java春:没有把变量插入文件的属性

问题描述:

在我的仓库有文件属性与此变种:的Java春:没有把变量插入文件的属性

wizard.start.scriptNameAndroid=install-android.bat 

这是我的文件弹簧businss.xml的一部分:

<bean id="wizardService" class="business.services.WizardServiceImpl"> 
    <property name="nameFileAndroid" value="${wizard.start.scriptNameAndroid}"/>   
</bean> 

这是我的级Java

public class WizardServiceImpl implements WizardService { 
    private static String nameFileAndroid=""; 

[...] 

    public String getNameFileAndroid() { 
     return nameFileAndroid; 
    } 
    public void setNameFileAndroid(String nameFileAndroid) { 
     this.nameFileAndroid = nameFileAndroid; 
    } 
} 

当我用变量nameFileAndroid这个程序总是把我设定的类的价值。 我如何做文件文件属性的优先级?

+0

在上下文中是否有'context:property-placeholder'定义? –

+0

这个变量放在.properties fie里面吗? –

+0

@MuhammadFaisalHyder是的! –

为什么你不使用它注入:

@Value("${wizard.start.scriptNameAndroid}") 
private static String nameFileAndroid; 

它会从你的属性文件值。

如果这个变量是的.properties文件,你可以参考它的价值是这样的:

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.core.env.Environment; 

@ComponentScan(basePackages = { "business.services.*" }) 
@PropertySource("classpath:file.properties") 
public class WizardServiceImpl implements WizardService { 

    @Autowired 
    private Environment enviro; 

    private static String nameFileAndroid = enviro.getProperty("wizard.start.scriptNameAndroid"); 
} 

Another way

@ComponentScan(basePackages = { "business.services.*" }) 
@PropertySource("classpath:file.properties") 
public class WizardServiceImpl implements WizardService { 

    @Value("${wizard.start.scriptNameAndroid}") 
    private static String nameFileAndroid; 

    //Register bean to enable ${} value wiring 
    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { 
    return new PropertySourcesPlaceholderConfigurer(); 
    } 
} 

或者,如果你还是喜欢XML方式:

<context:property-placeholder location="resources/file.properties" /> 

希望我帮了忙。 :)