4

With spring boot version 1.2.3 (also tested it with 1.2.5), I am following creating-a-custom-jasypt-propertysource-in-springboot and Spring Boot & Jasypt easy: Keep your sensitive properties encrypted to use jsypt library using custom PropertySourceLoader. My source loader class is in api.jar and this jar file is included in myapplication.war file. This war is deployed in tomcat.

It seems that spring is not loading EncryptedPropertySourceLoader on application startup. Can anyone please help?

Following is my project structure and related code.

Project - api.jar
    | src
    | | main
    |   | java
    |     | EncryptedPropertySourceLoader.java
    |   | resources
    |     | META-INF
    |       | spring.factories

The api.jar is build with api.jar!META-INF/spring.factories.

package com.test.boot.env;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;

public class EncryptedPropertySourceLoader implements PropertySourceLoader, PriorityOrdered {

    private static final Logger logger = LoggerFactory.getLogger(EncryptedPropertySourceLoader.class);


    public EncryptedPropertySourceLoader() {
        logger.error("\n\n\n***CREATING properties loader.\n\n\n");
    }

    @Override
    public String[] getFileExtensions() {
        return new String[] { "properties" };
    }

    @Override
    public PropertySource<?> load(final String name, final Resource resource, final String profile) throws IOException {
        logger.error("\n\n\n***Loading properties files.\n\n\n");

        if (true) {
            throw new RuntimeException("calling load"); \\intentional to identify the call
        }
        return null;
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
}

The spring.factories contents are

org.springframework.boot.env.PropertySourceLoader=\
com.test.boot.env.EncryptedPropertySourceLoader

I have also tried without '\' but it does not make any difference.

Following is the path of spring.factories in myapplication.war file

myapplication.war!WEB-INF/lib/api.jar!META-INF/spring.factories

As per my understanding, I should see the RuntimeException on startup but my application is starting successfully. Can anyone please help to find what I am be missing?

Community
  • 1
  • 1
amique
  • 2,176
  • 7
  • 34
  • 53
  • You can use this library: [jasypt-spring-boot][1] for which the only thing you'll have to do is to add a maven dependency (jasypt-spring-boot-starter) and provide the decryption key through a System Property when you run the app. [1]: https://github.com/ulisesbocchio/jasypt-spring-boot – Ulises Aug 07 '15 at 02:22
  • thanks @Ulises can you plz guide me what error do I have in my setup as mentioned above – amique Aug 07 '15 at 06:37

1 Answers1

2

For what I can tell the SpringFactoriesLoader mechanism for factory PropertySourceLoader is only used by PropertySourcesLoader which in turn is ONLY used to load the application properties. Those are either application.properties, application.yml, or application.yaml. So for your example just add a file application.properties to your classpath and you'll get the exception you are expecting. What I don't know is why other property source import mechanisms such as using annotation @PropertySource are not going through PropertySourcesLoader to take advantage of the factories loaders and override mechanism.

Ulises
  • 9,115
  • 2
  • 30
  • 27