Spring Boot未加载PropertySourceLoader [英] Spring boot not loading PropertySourceLoader

查看:404
本文介绍了Spring Boot未加载PropertySourceLoader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Spring Boot版本1.2.3(也在1.2.5中进行了测试),我正在关注春季引导和Jasypt简便:使用自定义的PropertySourceLoader,对敏感属性进行加密以使用jsypt库.我的源代码加载器类在api.jar中,而此jar文件包含在myapplication.war文件中.这场战争部署在tomcat中.

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.

似乎在应用程序启动时spring没有加载EncryptedPropertySourceLoader.谁能帮忙吗?

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

api.jar是使用api.jar!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;
    }
}

spring.factories的内容是

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.

以下是myapplication.war文件中spring.factories的路径

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

据我了解,我应该在启动时看到RuntimeException,但我的应用程序已成功启动.谁能帮忙找到我所缺少的东西吗?

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?

推荐答案

据我所知,工厂的SpringFactoriesLoader机制PropertySourceLoader仅由PropertySourcesLoader使用,而PropertySourcesLoader仅用于加载应用程序属性.这些是application.propertiesapplication.ymlapplication.yaml.因此,对于您的示例,只需将文件application.properties添加到类路径中,您将得到所期望的异常. 我不知道为什么其他属性源导入机制(例如使用注释@PropertySource)没有通过PropertySourcesLoader来利用工厂加载程序和覆盖机制.

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.

这篇关于Spring Boot未加载PropertySourceLoader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆