千家信息网

springboot怎么读取模板文件

发表于:2024-10-24 作者:千家信息网编辑
千家信息网最后更新 2024年10月24日,这篇文章主要讲解了"springboot怎么读取模板文件",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"springboot怎么读取模板文件"吧!目录s
千家信息网最后更新 2024年10月24日springboot怎么读取模板文件

这篇文章主要讲解了"springboot怎么读取模板文件",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"springboot怎么读取模板文件"吧!

目录
  • springboot读取模板文件

    • 第一种

    • 第二种

    • 第三种

  • SpringBoot读取配置文件信息

    • 一、创建配置文件

      • application.properties

      • application.yml

    • 二、读取配置信息

      • @value

      • @ConfigurationProperties

    • 三、读取指定环境配置

    springboot读取模板文件

    前言:resources下的template目录下的模版文件

    templateDir: template/

    第一种

      Resource resource = new ClassPathResource(templateDir + templateName);

    在linux生产环境下无法读取,也可能是其他原因,内网不好看错误

    第二种

     ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("classpath:template/"+templateName); InputStream inputStream =resource.getInputStream() ;

    各种环境下都能读取

    第三种

     Resource resource = new PathResource(templateDir + "黑灰数据分享模板.xls");        File file = resource.getFile();

    不确定 linux环境

    SpringBoot读取配置文件信息

    一、创建配置文件

    当我们新建一个SpringBoot工程的时候,在资源文件夹resources下,会自动生成默认的application.properties配置文件。

    application.properties

    其书写风格为小数点间隔级别书写全路径。这个老代码里面见的比较多。

    示例如下:

    server.port=8080spring.datasource.url=jdbc:mysql://localhost:3306/demospring.datasource.username=rootspring.datasource.password=root# 演示内容  demo.username=testdemo.password=test
    application.yml

    application.yml和application.properties有所不同,它采用"树形结构"的书写风格,减少了冗余的代码。

    注意:变量的值和变量名之间有且仅有一个空格。字符串变量不需要引号,当然加上了也不会报错。

    示例如下:

    server:  port: 8080spring:  datasource:    url: jdbc:mysql://localhost:3306/demo    username: root    password: root    # 演示内容    demo:  username: test  password: test

    二、读取配置信息

    @value

    如果是要读取单个或几个配置值的信息,可以直接在业务Bean中引入这个成员变量,并加上@value注解声明。

    // 其他包import org.springframework.beans.factory.annotation.Value;@Componentpublic class ReadConfigValueDemo {  @Value("${demo.username}")    private String username;  @Value("${demo.password}")    private String password; // 业务代码  }
    @ConfigurationProperties

    如果需要读取的配置文件很多,或则是一组相关的配置文件,希望在系统组装起来复用,那么我们可以采用构建配置Bean的方式。

    1. 添加pom依赖

    这是为了第二步配置Bean的时候能扫描到配置文件信息

                org.springframework.boot            spring-boot-configuration-processor            true        

    2. 创建配置Bean

    通过ConfigurationProperties的prefix前缀属性,我们可以指定一组配置值,注意属性名要和配置文件一致,类名无所谓。

    import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix="demo")public class DemoConfig {    private String username;     private String password; public String getUsername() {        return username;    } public String getPassword() {        return password;    }}

    3. 业务代码中使用

    哪里需要这一组配置文件,就通过@Resource或则@Autowired注解自动注入即可。

    注意:注入配置Bean的类,本身必须也是Spring管理下的一个Bean,否则会注入null值。这种情况在一些提供静态方法的工具类上可能出现。

    @Servicepublic class DemoServiceImpl{ @Resource    private DemoConfig demoConfig;    public void test() {     // 读取配置Bean中的值  System.out.println(demoConfig.getUsername()); }}

    三、读取指定环境配置

    SpringBoot项目支持多套配置,例如生产环境prod、开发环境dev、测试环境test等。

    以application.yml格式为例:

    # 当前启用dev配置文件spring:  profiles:    active: dev

    这种情况下,application.yml和application-dev.yml均能生效。同名的配置项以具体环境下的配置文件为主。

    如果我们想指定配置Bean仅在某环境下启用,可以做如下处理:

    @Profile("dev") // 仅在dev环境下生效@Component@ConfigurationProperties(prefix="demo")public class DemoConfig { // ...属性  }
    @Profile("!prod") // prod环境下不生效@Component@ConfigurationProperties(prefix="demo")public class DemoConfig { // ...属性}

    感谢各位的阅读,以上就是"springboot怎么读取模板文件"的内容了,经过本文的学习后,相信大家对springboot怎么读取模板文件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

    0