使用Spring Boot讀取Jar包外的配置文件

一、指定外部配置文件

在Spring Boot應用程序中,可以通過配置文件指定外部配置文件的位置。具體配置如下:

java -jar myproject.jar --spring.config.location=file:/path/to/myconfig.properties

這樣使用指定的路徑加載外部配置文件,但會覆蓋應用程序打包的默認配置文件。

另一種方法是在應用程序配置文件中指定外部文件路徑,如下所示:

spring.config.location=file:/path/to/myconfig.properties

這樣應用程序將首先嘗試加載該路徑下的文件,如果找不到,則加載默認配置文件。

二、加載外部資源文件

除加載配置文件外,Spring Boot也可以加載外部的資源文件,如HTML、CSS和JavaScript等。具體方式如下:

@Configuration
public class MyWebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/myresources/**")
                .addResourceLocations("file:/path/to/myresources/");
    }
}

該示例代碼中,實現了WebMvcConfigurer接口,並通過addResourceHandlers方法配置了資源文件的路徑,對於我們想要提供外部訪問的資源文件,可以通過「/myresources/**」路徑進行訪問,並且實際資源文件存儲在「/path/to/myresources/」目錄下。

三、加載外部模板文件

類似於加載資源文件,Spring Boot也可以加載外部模板文件,如JSP、Velocity和Thymeleaf等。具體方式如下:

@Configuration
public class MyWebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/mytemplates/**")
                .addResourceLocations("file:/path/to/mytemplates/");
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    @Bean
    public ITemplateResolver templateResolver() {
        FileTemplateResolver resolver = new FileTemplateResolver();
        resolver.setPrefix("file:/path/to/mytemplates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }
}

該示例代碼中使用了Thymeleaf模板引擎,首先通過addResourceHandlers方法配置了模板文件的路徑,然後定義了一個TemplateResolver對象的Bean,並將實際模板文件存儲路徑通過前綴「file:/path/to/mytemplates/」進行配置,同時指定了模板文件的後綴和模式。

四、使用命令行參數加載配置文件

除了指定外部配置文件路徑,Spring Boot還支持通過命令行參數來加載外部配置文件。可以通過如下的命令來啟動應用程序:

java -jar myproject.jar --spring.config.name=myconfig --spring.config.location=file:/path/to/

上述命令中,應用程序將會加載名為「myconfig」的外部配置文件,同時也指定了該文件所在的路徑。如果文件名和文件路徑都不指定,則會加載默認的「application」文件。

五、使用Bean加載配置文件

最後一種加載外部配置文件的方式是使用@Bean註解,來創建一個配置文件加載器。具體實現如下:

@Configuration
@PropertySource("classpath:/path/to/myconfig.properties")
public class MyConfig {
    @Value("${my.key}")
    private String myKey;

    @Bean
    public MyBean myBean() {
        return new MyBean(myKey);
    }
}

class MyBean {
    private final String myKey;

    MyBean(String myKey) {
        this.myKey = myKey;
    }
}

上述代碼中,首先通過@PropertySource註解加載外部配置文件,然後使用@Value註解將配置文件中的屬性注入到一個Java對象中。最後,通過@Bean註解創建一個Bean,並將Java對象作為構造函數參數傳遞到該對象中。

原創文章,作者:JKLB,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/143689.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
JKLB的頭像JKLB
上一篇 2024-10-22 23:35
下一篇 2024-10-22 23:35

相關推薦

發表回復

登錄後才能評論