【问题标题】:jasypt with spring boot带弹簧靴的 jasypt
【发布时间】:2017-11-07 07:27:43
【问题描述】:

我正在使用 jasypt-spring-boot-starter:1.14spring-boot-2.0.0.M2

如果 application.properties 在类路径 ( src/main/resources ) 中可用,它工作得非常好

即使将 application.properties 放在运行 spring boot jar 的文件夹中,Spring boot 也可以工作(默认情况下,它会在当前文件夹中查找 application.properties)

如果我将 application.properties 移出 src/main/resources 并将其放在根文件夹中,应用程序将无法启动并出现以下错误

nested exception is java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:179) ~[spring-context-5.0.0.RC2.jar!/:5.0.0.RC2]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308) ~[spring-context-5.0.0.RC2.jar!/:5.0.0.RC2]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:229) ~[spring-context-5.0.0.RC2.jar!/:5.0.0.RC2]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271) ~[spring-context-5.0.0.RC2.jar!/:5.0.0.RC2]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94) ~[spring-context-5.0.0.RC2.jar!/:5.0.0.RC2]
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686) ~[spring-context-5.0.0.RC2.jar!/:5.0.0.RC2]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524) ~[spring-context-5.0.0.RC2.jar!/:5.0.0.RC2]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) ~[spring-boot-2.0.0.M2.jar!/:2.0.0.M2]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M2.jar!/:2.0.0.M2]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M2.jar!/:2.0.0.M2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M2.jar!/:2.0.0.M2]

我可以通过将 application.properties 放在运行 Spring Boot 的文件夹中来运行其他 Spring Boot jar。 但是与 jasypt 集成后,它不起作用

如果任何机构遇到此类问题,请回复

【问题讨论】:

标签: spring-boot jasypt


【解决方案1】:

您必须手动解析配置路径。下面的示例实现,其中myapp.home 是应用程序参数(或 VM 选项 -> -Dmyapp.home=c:\my_app

@Configuration
public class Config {
    ...

    @Bean
    private static PropertyPlaceholderConfigurer getCustomPropertyPlaceholderConfigurer() {
        String appPropertyFile = "application.properties";
        String myAppHome = System.getProperty("myapp.home") + + File.separatorChar + "conf";;
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        final Resource extResource = new FileSystemResource( new File( myAppHome, appPropertyFile) );
        ppc.setLocations( new Resource[] { extResource } );
        ppc.setIgnoreUnresolvablePlaceholders( true );
        return ppc;
    }

    ...
}

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 2014-06-27
    • 2020-05-18
    • 1970-01-01
    • 2021-07-15
    • 2021-02-16
    • 2018-11-22
    • 1970-01-01
    相关资源
    最近更新 更多