springboot读取模板文件 springboot怎样读取模板文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot读取模板文件 springboot怎样读取模板文件

Java大表哥   2021-09-07 我要评论
想了解springboot怎样读取模板文件的相关内容吗,Java大表哥在本文为您仔细讲解springboot读取模板文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot读取文件,读取模版文件,下面大家一起来学习吧。

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=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有所不同,它采用“树形结构”的书写风格,减少了冗余的代码。

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

示例如下:

server:
  port: 8080
spring:
  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;
@Component
public class ReadConfigValueDemo {
 
 @Value("${demo.username}")
    private String username;
 
 @Value("${demo.password}")
    private String password;
 // 业务代码
  
}

@ConfigurationProperties

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

1. 添加pom依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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值。这种情况在一些提供静态方法的工具类上可能出现。

@Service
public 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 {
 // ...属性
}

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们