【springBoot】springboot获取资源⽂件的三种⽅式【两种情况
下】
⾸先声明⼀点,springboot获取资源⽂件,需要看是
  1》从spring boot默认的application.properties资源⽂件中获取
  2》还是从⾃定义的资源⽂件中获取
带着这个想法去看下⾯⼏种⽅式
===============================================================================================
1》从spring boot默认的application.properties资源⽂件中获取
先给出来application.properties的内容
#⽅式1
pe1 = type1
com.sxd.title1 = 使⽤@ConfigurationProperties获取配置⽂件
#⽅式2
pe2 = type2
com.sxd.title2 = 使⽤@Value获取配置⽂件
#⽅式3
pe3 = type3
com.sxd.title3 = 使⽤Environment获取资源⽂件
#map
com.sxd.login[username] = sxd
com.sxd.login[password] = admin123
com.sxd.login[callback] = wwwblogs/sxdcgaq8080/
img标签的属性
#list
com.sxdList[0] = com1
com.sxdList[1] = com2
com.sxdList[2] = com3
View Code
①===第⼀种⽅式:使⽤@ConfigurationProperties获取配置⽂件
先搞⼀个绑定资源⽂件的bean
注意属性名和资源⽂件中的属性名相⼀致。
package com.sxd.beans;
import org.t.properties.ConfigurationProperties;
import t.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "com.sxd")
//@PropertySource("classpath:/application.properties")
//不⽤这个注解,默认就是加载application.properties资源⽂件
public class User {
private String type1;
private String title1;
private Map<String,String> login = new HashMap<>();
private List<String> comList = new ArrayList<>();
public String getType1() {
return type1;
}
public void setType1(String type1) {
}
public String getTitle1() {
return title1;
}
public void setTitle1(String title1) {
this.title1 = title1;
}
public Map<String, String> getLogin() {
return login;
}
public void setLogin(Map<String, String> login) {
this.login = login;
}
public List<String> getComList() {
return comList;
}
public void setComList(List<String> comList) {
thisList = comList;
}
}
View Code
然后在启动类中使⽤
package com.sxd.secondemo;
import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.t.properties.EnableConfigurationProperties; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
@EnableConfigurationProperties(User.class)
public class SecondemoApplication {
@Autowired
User user;
@RequestMapping("/")
public String hello(){
System.out.println("map的键:"+k+">>map的值:"+v);
});
System.out.println("list的值:"+i);
});
Type1()+Title1();
}
public static void main(String[] args) {
SpringApplication.run(SecondemoApplication.class, args);
}
}
View Code
结果如下:
控制台打印:
excel公式下拉自动计算访问地址:
②===第⼆种⽅式:使⽤@Value获取配置⽂件织梦站长
这⾥不⽤搞⼀个绑定资源⽂件的bean了。
只需要在你想⽤的地⽅使⽤@Value调⽤你想要的属性名对应的值即可。
package com.sxd.secondemo;
import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.t.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;玳瑁真品的图片
@RestController
@SpringBootApplication
public class SecondemoApplication {
@Value("${pe2}")
jsindexof用法
private String type;
@Value("${com.sxd.title2}")
private String title;
@RequestMapping("/")
public String hello(){
return type+title;
}
public static void main(String[] args) {
SpringApplication.run(SecondemoApplication.class, args);
}
}
View Code
访问结果:
③===第三种⽅式:使⽤Environment获取资源⽂件
也不⽤提前做什么使⽤,Environment就是⼀个全局的资源池,application.properties中的属性值都可以从这⾥获取到。
package com.sxd.secondemo;
import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.t.properties.EnableConfigurationProperties;
import nv.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SecondemoApplication {
@Autowired
Environment environment;
@RequestMapping("/")
public String hello(){
Property("pe3")+Property("com.sxd.title3");
}
public static void main(String[] args) {
SpringApplication.run(SecondemoApplication.class, args);
}
View Code
运⾏结果:
================================================================================================ 2》从⾃定义的资源⽂件中获取属性值
①===第⼀种⽅式:使⽤@ConfigurationProperties获取配置⽂件
⾃定义资源⽂件如下:
然后指定绑定⾃定义资源⽂件
package com.sxd.beans;
import org.t.properties.ConfigurationProperties;
import t.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "com.sxd")
@PropertySource("classpath:/test.properties")
//需要⽤这个注解,默认就是加载application.properties资源⽂件,替换@ConfigurationProperties取消location属性的效果
public class User {
private String type1;
private String title1;
public String getType1() {
return type1;
}
public void setType1(String type1) {
}
public String getTitle1() {
return title1;
}
public void setTitle1(String title1) {
this.title1 = title1;
}
}
View Code
最后在启动类中使⽤⼀下
package com.sxd.secondemo;
import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.t.properties.EnableConfigurationProperties;
import nv.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
@EnableConfigurationProperties(User.class)
public class SecondemoApplication {
@Autowired
User user;
@RequestMapping("/")
public String hello(){
Type1()+Title1();
}
public static void main(String[] args) {
SpringApplication.run(SecondemoApplication.class, args);
}
}
View Code
运⾏结果:
②===第⼆种⽅式:使⽤@Value获取配置⽂件
先设定⼀个⾃定义资源⽂件
如下,⾃定义资源⽂件需要满⾜application-{profile}.properties格式
然后在application.properties⽂件中指明加载这个资源⽂件
spring.profiles.active=test
#spring.profiles.include=test
这两种哪种都可以加载上⾃定义的资源⽂件,后⾯的test就是上⾯{profile}的值
最后在启动类中使⽤@Value获取⾃定义资源⽂件中的属性,这个时候⾃定义的资源⽂件已经在application,properties⽂件中被指明要被加载了,因此是可以被获取到的
package com.sxd.secondemo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.t.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SecondemoApplication {
@Value("${pe2}")
private String type;
@Value("${com.sxd.title2}")
private String title;
@RequestMapping("/")
public String hello(){
return type+title;
}
public static void main(String[] args) {
SpringApplication.run(SecondemoApplication.class, args);
}
}
View Code
运⾏结果:
>properties是什么文件

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。