方法一: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();
}
}
版权声明:本文为qq_37129192原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。