Spring boot not loading PropertySourceLoader

筅森魡賤 提交于 2019-12-11 02:38:16

问题


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:


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.



来源:https://stackoverflow.com/questions/31824601/spring-boot-not-loading-propertysourceloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!