2

I want to add properties files encryption with Jasypt in my Spring 4 app (without Spring Boot). The configuration is in Java.

I found solution for Spring 3.1 XML config, and for Spring Boot but not Spring4 Java config.

I tried declaring it as suggested in the doc for 3.1 (http://www.jasypt.org/spring31.html) in a Java config way, but it doesn't load the properties.

@Bean
public EncryptablePropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
    EncryptablePropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new EncryptablePropertyPlaceholderConfigurer(new OurCustomEncryptor());
    propertyPlaceholderConfigurer.setLocations(
            new ClassPathResource("config-encrypted.properties")
    );
    return propertyPlaceholderConfigurer;
}

Any idea?

Matt
  • 3,422
  • 1
  • 23
  • 28

1 Answers1

2

Actually the problem was twofold :

First, I had another PropertyPlaceholderConfigurer declared somewhere else in the config which seemed to override this one.

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

So getting rid of it allowed me to go further.

Secondly, it worked for properties injected with @Value annotation but not when trying to retrieve the value through Spring Environment#getProperty method. And this is because as explained in this answer, PropertyPlaceholderConfigurer is only valid during bean creation.

Matt
  • 3,422
  • 1
  • 23
  • 28