当前位置:   article > 正文

SpringBoot_4_使用userbean,实现文件加载

使用userbean,实现文件加载

SpringBoot_4

  1. @PropertySource

@PropertySource:加载指定的配置文件【properties】.

先前我们通过@ConfifigurationProperties加载全局配置文件中的值到javabean中,但是我们在具体使用的时候不会把所用的配置都保存在全局配置文件中的,可能会将不同的配置保存在不同的配置文件中,那么这时我们就需要@PropertySource注解为指定的javabean类加载指定的配置文件

例如:

  1. package com.wangxing.springboot.bean;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.context.annotation.PropertySource;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @ConfigurationProperties(prefix = "person")
  7. @PropertySource(value = {"classpath:person.properties"})
  8. public class PersonBean {
  9. private int perid;
  10. private String pername;
  11. private int perage;
  12. private String peraddress;
  13. public int getPerid() {
  14. return perid;
  15. }
  16. public void setPerid(int perid) {
  17. this.perid = perid;
  18. }
  19. public String getPername() {
  20. return pername;
  21. }
  22. public void setPername(String pername) {
  23. this.pername = pername;
  24. }
  25. public int getPerage() {
  26. return perage;
  27. }
  28. public void setPerage(int perage) {
  29. this.perage = perage;
  30. }
  31. public String getPeraddress() {
  32. return peraddress;
  33. }
  34. public void setPeraddress(String peraddress) {
  35. this.peraddress = peraddress;
  36. }
  37. }
  1. person.properties
  2. person.perid=1002
  3. person.pername=lisi
  4. person.perage=24
  5. person.peraddress=北京
  1. package com.wangxing.springboot.controller;
  2. import com.wangxing.springboot.bean.PersonBean;
  3. import com.wangxing.springboot.bean.StudentBean;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. @Controller
  9. public class TestController {
  10. @Autowired
  11. private PersonBean personBean;
  12. @RequestMapping(value = "/perinfo")
  13. @ResponseBody
  14. public String getPersonInfo(){
  15. String perinfo=personBean.getPerid()+"\t"+
  16. personBean.getPername()+"\t"+
  17. personBean.getPerage()+"\t"+
  18. personBean.getPeraddress();
  19. return perinfo;
  20. }

名称

用途

@PropertySource(value = "classpath:studentbean.properties")

标注在类上,指定的配置文件【properties】,可以引入多个配置文件,例如

@PropertySource({ "classpath:studentbean.properties",

"classpath:person.properties"})

@Value("${xxxx}")
 

标注在成员变量上,设置当前成员变量的初始值,可以直接设值@Value("zhangsan"),也可以从@PropertySource指定的配置文件【properties】中取值设置给成员变量@Value("${xxxx}")

@ConfigurationProperties(prefix = "person")

标注在类上,表示设置当前类在配置文件【properties】的配置名称【prefix = "person"】

2.@ImportResource

@ImportResource:导入基于XML的Spring的配置文件,让配置文件里面的内容生效;

  1. package com.wangxing.springboot.bean;
  2. public class UserBean {
  3. private int userid;
  4. private String username;
  5. private int userage;
  6. private String useraddress;
  7. public int getUserid() {
  8. return userid;
  9. }
  10. public void setUserid(int userid) {
  11. this.userid = userid;
  12. }
  13. public String getUsername() {
  14. return username;
  15. }
  16. public void setUsername(String username) {
  17. this.username = username;
  18. }
  19. public int getUserage() {
  20. return userage;
  21. }
  22. public void setUserage(int userage) {
  23. this.userage = userage;
  24. }
  25. public String getUseraddress() {
  26. return useraddress;
  27. }
  28. public void setUseraddress(String useraddress) {
  29. this.useraddress = useraddress;
  30. }
  31. }
  1. package com.wangxing.springboot.controller;
  2. import com.wangxing.springboot.bean.UserBean;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.web.servlet.mvc.Controller;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. public class TestController implements Controller {
  11. private UserBean userBean;
  12. public UserBean getUserBean() {
  13. return userBean;
  14. }
  15. public void setUserBean(UserBean userBean) {
  16. this.userBean = userBean;
  17. }
  18. @Override
  19. public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
  20. String userinfo=userBean.getUserid()+"\t"+
  21. userBean.getUsername()+"\t"+
  22. userBean.getUserage()+"\t"+
  23. userBean.getUseraddress();
  24. System.out.println("userinfo=="+userinfo);
  25. return null;
  26. }
  27. }

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="userBean" class="com.wangxing.springboot.bean.UserBean">
  6. <property name="userid" value="1004"></property>
  7. <property name="username" value="lisi"></property>
  8. <property name="userage" value="24"></property>
  9. <property name="useraddress" value="beijing"></property>
  10. </bean>
  11. <bean name="/test" class="com.wangxing.springboot.controller.TestController">
  12. <property name="userBean" ref="userBean"></property>
  13. </bean>
  14. </beans>

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;

想让Spring的配置文件生效,加载进来;@ImportResource标注在一个主上.

3.@Configuration   @Bean  @Import

  1. package com.wangxing.springboot.bean;
  2. public class UserBean {
  3. private int userid;
  4. private String username;
  5. private double userdouble;
  6. private boolean usersex;
  7. public int getUserid() {
  8. return userid;
  9. }
  10. public void setUserid(int userid) {
  11. this.userid = userid;
  12. }
  13. public String getUsername() {
  14. return username;
  15. }
  16. public void setUsername(String username) {
  17. this.username = username;
  18. }
  19. public double getUserdouble() {
  20. return userdouble;
  21. }
  22. public void setUserdouble(double userdouble) {
  23. this.userdouble = userdouble;
  24. }
  25. public boolean isUsersex() {
  26. return usersex;
  27. }
  28. public void setUsersex(boolean usersex) {
  29. this.usersex = usersex;
  30. }
  31. }
  1. package com.wangxing.springboot.config;
  2. import com.wangxing.springboot.bean.UserBean;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;=
  5. @Configuration
  6. public class UserConfig {
  7. @Bean
  8. public UserBean getBean(){
  9. UserBean userBean= new UserBean();
  10. userBean.setUserid(1001);
  11. userBean.setUsername("zhangsan");
  12. userBean.setUserdouble(168.9);
  13. userBean.setUsersex(true);
  14. return userBean;
  15. }
  16. }

 

  1. package com.wangxing.springboot.controller;
  2. import com.wangxing.springboot.bean.UserBean;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. @RequestMapping("/user")
  8. public class UserController {
  9. @Autowired
  10. private UserBean userBean;
  11. @RequestMapping("/testUserBean.do")
  12. public void testUserBean(){
  13. int userid= userBean.getUserid();
  14. String username= userBean.getUsername();
  15. double userdouble= userBean.getUserdouble();
  16. boolean usersex= userBean.isUsersex();
  17. System.out.println(userid+","+username+","+userdouble+","+usersex);
  18. }
  19. }
  1. package com.wangxing.springboot.springbootdemo7;
  2. import com.wangxing.springboot.config.UserConfig;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.ComponentScans;
  7. import org.springframework.context.annotation.Import;
  8. @SpringBootApplication
  9. @ComponentScan(basePackages = "com.wangxing.springboot")
  10. @Import(UserConfig.class)
  11. public class Springbootdemo7Application {
  12. public static void main(String[] args) {
  13. SpringApplication.run(Springbootdemo7Application.class, args);
  14. }
  15. }

名称

用途

@Configuration

标注在类上,表示当前类是一个java Config形式的配置类,可以代替基于xml的配置文件

@Bean

标注在方法上,告诉springIOC容器创建javabean类对象

相当于在基于xml配置文件中的

<bean id=”” class=””></bean>

@Import(xxxConfig.class)
 

标注在主类上,表示导入xxxConfig.class【java Config形式的配置类】

配置文件【application.properties】中的占位符

1、随机数

${random.value}、${random.int}、${random.long}

${random.int(10)}、${random.int[1024,65536]}

例如:

  1. package com.wangxing.springboot.bean;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.context.annotation.PropertySource;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @ConfigurationProperties(prefix = "student")
  7. public class StudentBean {
  8. private int stuid;
  9. private String stuname;
  10. private int stuage;
  11. private String stuaddress;
  12. public int getStuid() {
  13. return stuid;
  14. }
  15. public void setStuid(int stuid) {
  16. this.stuid = stuid;
  17. }
  18. public String getStuname() {
  19. return stuname;
  20. }
  21. public void setStuname(String stuname) {
  22. this.stuname = stuname;
  23. }
  24. public int getStuage() {
  25. return stuage;
  26. }
  27. public void setStuage(int stuage) {
  28. this.stuage = stuage;
  29. }
  30. public String getStuaddress() {
  31. return stuaddress;
  32. }
  33. public void setStuaddress(String stuaddress) {
  34. this.stuaddress = stuaddress;
  35. }
  36. }
  1. application.properties
  2. student.stuid=${random.int}
  3. student.stuname=zhangsan
  4. student.stuage=${random.int[1024,65536]}
  5. student.stuaddress=${student.stuname}_地址

 

  1. package com.wangxing.springboot.controller;
  2. import com.wangxing.springboot.bean.StudentBean;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. @Controller
  8. public class TestController {
  9. @Autowired
  10. private StudentBean studentBean;
  11. @RequestMapping(value = "/test")
  12. @ResponseBody
  13. public String testStudent(){
  14. System.out.println(studentBean.getStuid());
  15. System.out.println(studentBean.getStuname());
  16. System.out.println(studentBean.getStuage());
  17. System.out.println(studentBean.getStuaddress());
  18. return "";
  19. }
  20. }

Profiles

  1. Profifile文件就是用来配置在不同环境下的配置数据。
  2. 因为在不同的环境下配置文件中配置的运行环境的数据是不同的,所以我们就需要灵活的在不同的运行环境下切换成对应的运行环境的数据,此时我们将不同的运行环境数据,配置到不同的配置文件中,通过在主配置文件application.properties中的spring.profiles.active属性完成切换。

测试.properties配置

  1. application-dev.properties【开发环境配置】
  2. server.port=8080
  3. application-prod.properties【生产环境配置】
  4. server.port=9090
  5. application.properties 【主配置】
  6. spring.profiles.active=prod 【指定使用生产环境配置】
  7. http://localhost:9090/testInfo
  8. 或者
  9. spring.profiles.active=dev 【指定使用开发环境配置】
  10. http://localhost:8080/testInfo

测试.yml配置

  1. application-devyml.yml【开发环境配置】
  2. server:
  3. port: 8080
  4. application-prodyml.yml【生产环境配置】
  5. server:
  6. port: 9090
  7. application.yml 【主配置】
  8. spring:
  9. profiles:
  10. active: prodyml 【指定使用生产环境配置】
  11. http://localhost:9090/testInfo
  12. 或者
  13. spring:
  14. profiles:
  15. active: devyml 【指定使用开发环境配置】

http://localhost:8080/testInfo

上面是通过在1.主配置文件中切换运行环境配置

还可以通过配置2.运行环境参数配置视图窗口来指定具体使用哪一个运行环境

“--spring.profiles.active=dev“

还可以通过3.命令行运行jar的时候指定具体使用哪一个运行环境

java -jar testspringboot002-0.0.1-SNAPSHOT.jar  --spring.profiles.active=dev;

还可以通过4.配置虚拟机参数指定具体使用哪一个运行环境;

“-Dspring.profiles.active=dev”

注意:运行环境配置文件的名称 application-{profiles}.properties/yml

主配置文件加载位置

spring boot 启动会扫描以下位置的application.properties或者 application.yml文件作为Spring boot的默认配置文件

– 项目根目录/config/

– 项目根目录/

– resource/config/

– resource:/  

以上是按照优先级从高到低的顺序,所有位置的文件都会被加载,高优先级配置内容会覆盖低优先级配置内容。

SpringBoot会从这四个位置全部加载主配置文件;互补配置

我们也可以通过配置spring.config.location来改变默认配置

项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;

java -jar testspringboot02-0.0.1-SNAPSHOT.jar --spring.confifig.location=F:/application.properties

外部配置加载顺序

Spring Boot 支持多种外部配置方式

1. 命令行参数

2. 来自java:comp/env的JNDI属性

3. Java系统属性(System.getProperties())

4. 操作系统环境变量

5. RandomValuePropertySource配置的random.*属性值

6. jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件

7. jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件

8. jar包外部的application.properties或application.yml(不带spring.profile)配置文件

9. jar包内部的application.properties或application.yml(不带spring.profile)配置文件

优先加载带profifile,再来加载不带profifile,jar包外向jar包内进行寻找

10. @Configuration注解类上的@PropertySource

11. 通过SpringApplication.setDefaultProperties指定的默认属性

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/796529
推荐阅读
相关标签
  

闽ICP备14008679号