2016-12-16 43 views
0

在我的倉庫有文件屬性與此變種:的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是的! –

回答

0

爲什麼你不使用它注入:

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

它會從你的屬性文件值。

0

如果這個變量是的.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" /> 

希望我幫了忙。 :)