Niranga Sandaruwan
Niranga Sandaruwan

Reputation: 711

Spring read property file outside the class path

I have a property file located in tomcat folder.Im trying to read the propert file content as follow.but im getting null value for the property value.

file path has no any issue.

@ComponentScan(basePackages = "org.sakaiproject.log.api")
@Configuration
@PropertySource(name = "props", value = {
        "file:/home/tomcat-sakai-7.0.55/sakai/local.properties",
        "file:/home/tomcat-sakai-7.0.55/sakai/sakai.properties" })
public class SpringCryptoContext {



    @Value("${aes.encryption.cipherString}")
    private static String cyperString;

    public SpringCryptoContext() {

    }



    public static void main(String[] args) throws Exception {

        ApplicationContext context = new AnnotationConfigApplicationContext(
                SpringCryptoContext.class);


        System.out.println(cyperString);


    }
}

Edit:

I created seperate class and load the properties as follow.

@Service
@PropertySource(name = "locations", value = {
        "file:/home/nirmala/projects/iml/tomcat-sakai-7.0.55/sakai/local.properties",
        "file:/home/nirmala/projects/iml/tomcat-sakai-7.0.55/sakai/sakai.properties" })
public class CryptoToolImpl implements CryptoTool {

    private Cipher cipher = null;
    private Key key = null;
    @Value("${pii.encryption.cipherString}")
    private String cipherString = "";

    public static final Logger log = Logger.getLogger(CryptoToolImpl.class);

    public IMLCryptoToolImpl() {
        try {
            fixKeyLength();


            cipher = Cipher.getInstance(cipherString);
            key = KMSKeyStoreSingleton.getInstance().getPrivateKey();

        } catch (Exception e) {
            log.error("Error in initializing CryptoToolImpl : "
                    + e.getMessage());
        }

    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

but i get the following error

Error in initializing CryptoToolImpl : Invalid transformation format:

Upvotes: 1

Views: 1185

Answers (2)

Manoj Patel
Manoj Patel

Reputation: 11

Something below should help.

@Configuration
@PropertySources(value={
    @PropertySource(value="file:${catalina.home}/sakai/local.properties"),
    @PropertySource(value="file:${catalina.home}/sakai/sakai.properties")
})
public class SpringCryptoContext {

    @Value("${aes.encryption.cipherString}")
    private String cyperString;
}

Upvotes: 1

kakabali
kakabali

Reputation: 4033

Please add the following bean in your configuration class:-

//To resolve values using the @Value
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigDev() {
    return new PropertySourcesPlaceholderConfigurer();
}

Update

static won't work in this case, you need to do the following:-

Add the following code:-

@Value("${aes.encryption.cipherString}")
private String cyperString;

public String getCypherString(){
    return this.cyperString;
}

This is just an example, but static you are using because you are accessing it in the main method which is static and for accessing the variable, you marked it static

The issue is that the static variable is a class variable which is having no property when the code reaches at your line of System.out for the variable, At this time - spring is still doing internal initialization etc..

You are accessing/ using the variable as non spring framework while you are instantiating using spring. It has to be single way only

And I would suggest to have a separate class to load these variables into context, may be some class marked with @Component or more specifically @Service

Upvotes: 2

Related Questions