获取resources内容

1.Spring读取resources下的properties文件

项目中遇到将一些固定数据放在properties中怎么办?可以采用property、Resource、ResourceBundle等多种方式。
config.properties

key=marlon
value=18

PropertiesUtil.java

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * @author marlon
 * @create 2023-11-03 20:37
 * @description: 解析properties
 */
public class PropertiesUtil {

    public static void main(String[] args) throws IOException {
        PropertiesUtil util = new PropertiesUtil();
        util.testResource();

        util.testClassLoader();

        util.testSystemClassLoader();

        util.testNewFile();

        util.testBundle();
    }

    public void testResource() throws IOException {
        Resource resource = new ClassPathResource("config.properties");
        Properties properties = PropertiesLoaderUtils.loadProperties(resource);
        System.out.println("===================ClassPathResource===========================");
        System.out.println(properties.getProperty("key")+"==="+properties.getProperty("value"));
    }

    public void testClassLoader() throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = ClassLoader.getSystemResourceAsStream("config.properties");
        properties.load(inputStream);
        System.out.println("===================ClassLoader.getSystemResourceAsStream===========================");
        System.out.println(properties.getProperty("key")+"==="+properties.getProperty("value"));
    }

    public void testSystemClassLoader() throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties");
        properties.load(inputStream);
        System.out.println("===================ClassLoader.getSystemClassLoader===========================");
        System.out.println(properties.getProperty("key")+"==="+properties.getProperty("value"));
    }

    public void testNewFile() throws IOException {
        Properties properties = new Properties();
        FileInputStream inputStream = new FileInputStream(new File("src/main/resources/config.properties"));
        properties.load(inputStream);
        System.out.println("===================FileInputStream===========================");
        System.out.println(properties.getProperty("key")+"==="+properties.getProperty("value"));
    }

    public void testBundle() {
        ResourceBundle rb = ResourceBundle.getBundle("info");
        System.out.println("===================ResourceBundle===========================");
        for(String key : rb.keySet()){
            String value = rb.getString(key);
            System.out.println(key + ":" + value);
        }
    }
}

总结:new FileInputStream的方式是项目的根目录,需要加上src/main/resources路径;其他方式都是项目的resources资源下,ResourceBundle更特殊,可以不用写文件后缀。

2.springboot绑定properties

2.1 创建properties类

创建一个ClientProperties类,并添加注解@Configuration、@ConfigurationProperties(prefix = "aft.client")通过prefix设置前缀,前缀下的属性名要和ClientProperties类中的属性名保持一致,并且必须有set方法,也可以用lombok的@Data注解代替

@Configuration
@ConfigurationProperties(prefix = "aft.client")
@Data
public class ClientProperties {

    private String serviceAddr;
    private String managerAddr;
    private String id;
    private String encrypt;
}

2.2 属性注入

在需要使用的地方使用@Autoware注解进行属性注入

@Autowired
private ClientProperties clientProperties;

2.3 编写yaml或者properties配置文件

application.yaml如下:

aft:
  client:
    enabled: true
    serviceAddr: 111
    managerAddr: 222
    id: 333
    encrypt: 444

application.properties如下:

aft.client.enabled = true
aft.client.serviceAddr = 111
aft.client.managerAddr = 222
aft.client.id = 333
aft.client.encrypt = 444

2.4 启动类中开启config注解

@EnableConfigurationProperties
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值