当前位置:首页 > 通信资讯 > 正文

springboot 读取文件(springboot根据模板导出pdf)

目录
  • springboot读取模板文件
    • 第一种
    • 第二种
    • 第三种
  • SpringBoot读取配置文件信息
    • 一、创建配置文件
      • application.properties
      • application.yml
    • 二、读取配置信息
      • @value
      • @ConfigurationProperties
    • 三、读取指定环境配置

    springboot读取模板文件

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

    springboot 读取文件(springboot根据模板导出pdf)

    ?
    1 templateDir: template/

    第一种

    ?
    1 Resource resource = new ClassPathResource(templateDir + templateName);

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

    第二种

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

    各种环境下都能读取

    第三种

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

    不确定 linux环境

    SpringBoot读取配置文件信息

    一、创建配置文件

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

    application.properties

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

    示例如下:

    ?
    1 2 3 4 5 6 7 server.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/demo spring.datasource.username=root spring.datasource.password=root # 演示内容 demo.username=test demo.password=test

    application.yml

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

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

    示例如下:

    ?
    1 2 3 4 5 6 7 8 9 10 11 12 server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/demo username: root password: root # 演示内容 demo: username: test password: test

    二、读取配置信息

    @value

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

    ?
    1 2 3 4 5 6 7 8 9 10 11 12 13 // 其他包 import org.springframework.beans.factory.annotation.Value; @Component public class ReadConfigValueDemo { @Value("${demo.username}") private String username; @Value("${demo.password}") private String password; // 业务代码 }

    @ConfigurationProperties

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

    1. 添加pom依赖

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

    ?
    1 2 3 4 5 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>

    2. 创建配置Bean

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

    ?
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 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值。这种情况在一些提供静态方法的工具类上可能出现。

    ?
    1 2 3 4 5 6 7 8 9 @Service public class DemoServiceImpl{ @Resource private DemoConfig demoConfig; public void test() { // 读取配置Bean中的值 System.out.println(demoConfig.getUsername()); } }

    三、读取指定环境配置

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

    以application.yml格式为例:

    ?
    1 2 3 4 # 当前启用dev配置文件 spring: profiles: active: dev

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

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

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

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

    原文链接:https://blog.csdn.net/erpenggg/article/details/118958796

    如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

    为您推荐:

    发表评论

    ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。