Spring boot not loading PropertySourceLoader

2019-07-10 07:02发布

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?

1条回答
Explosion°爆炸
2楼-- · 2019-07-10 07:44

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.

查看更多
登录 后发表回答