Spring读取properties文件的值

方法一:spring普通读取

properties的文件

jdbc.name:飙签
jdbc.age:20
jdbc.shuai:帅呆了

测试方法

import com.wwy.jdbc.AnnoConfig;
import com.wwy.jdbc.Buser;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

public class TestJbcdYml {

    /*读取方法*/
    @Test
    public void testJdbcYmlValue() throws IOException {
        ApplicationContext context=new AnnotationConfigApplicationContext(AnnoConfig.class);
        //jdbc.properties必须存在src目录下面
        EncodedResource encodedResource = new EncodedResource(new ClassPathResource("jdbc.properties"),"UTF-8");
        //读取properties文件的值
        Properties properties=PropertiesLoaderUtils.loadProperties(encodedResource);
        //spring注入Bean管理
        Buser buser=context.getBean(Buser.class);
        buser.setName(properties.getProperty("jdbc.name"));
        buser.setAge(properties.getProperty("jdbc.age"));
        buser.setShuai(properties.getProperty("jdbc.shuai"));
        System.out.println(buser.toString());
    }
}

方法二:读取springboot里面的核心配置文件application.properties或application.yml配置文件

测试方法1

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OneController {

    @Value("${jdbc.name}")
    private String name;
    @Value("${jdbc.age}")
    private int age;

    @RequestMapping("/handle01")
    public String handle01(){
        return "name="+name+";age="+age;
    }
}

测试方法2

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OneController {

    @Autowired
    private Environment evm;

    @RequestMapping("/handle01")
    public String handle01(){
        return "name="+evm.getProperty("jdbc.name")+";age="+evm.getProperty("jdbc.age");
    }
}

方法三:springboot读取自定义配置文件信息

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor //有参构造
@NoArgsConstructor  //无参构造
@Component //DI注入
@Configuration
//prefix键文件前缀
@ConfigurationProperties(prefix = "jdbc")
//指定配置文件的所在位置
@PropertySource(value = "author.properties",encoding = "utf-8")
public class User {
    private String name;
    private String age;
}
import com.example.springboot03.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OneController {

    @Autowired
    private User user;

    @RequestMapping("/handle01")
    public String handle01(){
        return "name="+user.getName()+";age="+user.getAge();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值