从最简单的配置文件开始说起
在resource下面,创建application.properties, 虽然网上说,IntelliJ Spring Boot启动以后会自动生成该文件,然而,在我的学习过程里,并没有。不纠结这个部分的,所以,自己创建了一个application.properties, 后又看到,除了properties这种方式的配置,还有yaml这种书写更简单的方式,如下格式:
my:
webserver:
#HTTP 监听端口
port: 80
#嵌入Web服务器的线程池配置
threadPool:
maxThreads: 100
minThreads: 8
idleTimeout: 60000
这里我用到的是application.properties, properties文件的路径如图:
application.properties.png
第一步,在properties文件里配置一些信息,如图:
application.properties
第二步,使用注解@ConfigurationProperties来从application.properties文件里读取前缀(prefix)为my的配置,属性的名称,类型要和配置文件里配置的一样。
@ConfigurationProperties(prefix = "my")
public class MyBean {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第三步,创建一个类似Beans的配置类,管理所有的Beans实例。
注意:
@Configuration //这是一个配置类,spring会扫描到这个类,@Bean才会生效, 可以有很多的beans.
@EnableConfigurationProperties //将MyBean这个类(或者CaoBean等多个类)的配置到上下文环境中,并且在本类里需要使用的@Autowired注解注入才能生效
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //这是一个配置类,spring会扫描到这个类,@Bean才会生效, 可以有很多的@bean.
//将MyBean这个类(或者CaoBean等多个类)的配置到上下文环境中,并且在本类里需要使用的@Autowired注解注入才能生效
@EnableConfigurationProperties({MyBean.class,CaoBean.class})
public class DataConfig {
@Autowired
MyBean myBean;
@Autowired
CaoBean caoBean;
public String setMyConfig(){
return "Hello" + myBean.getName() + " " + "Your ID is: " + myBean.getId() + "!!!";
}
public String setCaoConfig(){
System.out.println("caoBean " + caoBean.getName());
return "Bye" + caoBean.getName() + " " + "Your ID is: " + caoBean.getId() + "!!!";
}
@Bean //@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值(返回一个对象)是spring上下文环境中的一个bean。
public BeanWithoutProperties getBeanWithoutProperites()
{
//给BeanWithoutProperties属性赋值,这里setId和setName的参数是利用的myBean.getId()和caoBean.getName()传入。
BeanWithoutProperties beanWithoutProperites = new BeanWithoutProperties();
beanWithoutProperites.setId(myBean.getId());
beanWithoutProperites.setName(caoBean.getName());
beanWithoutProperites.setScore((float) 9.5);
return beanWithoutProperites;
}
@Bean(name = "get02")
public BeanWithoutProperties02 getBean()
{
BeanWithoutProperties02 beanWithoutProperties02 = new BeanWithoutProperties02();
beanWithoutProperties02.setId(909090);
beanWithoutProperties02.setName("JIAJIA");
return beanWithoutProperties02;
}
}
注意到这里@Bean注解下面BeanWithoutProperties这个类。
BeanWithoutProperties这个类和MyBean/CaoBean不一样,MyBean/CaoBean类的配置信息是通过从@ConfigurationProperties和@EnableConfigurationProperties注解来从application.preporties文件里绑定属性值;而BeanWithoutProperits类不会从配置文件里读取信息,给BeanWithoutProperties属性赋值,可以用set方法直接赋值,在上面的代码里,setId(myBean.getId())和setName(caoBean.getName())的参数是利用的myBean.getId()和caoBean.getName()传入。所以,通过这种间接的方式,其他Bean也可以获取到配置文件里的参数。
类似MyBean/CaoBean类,我们需要创建一个BeanWithoutProperties类,如下:
public class BeanWithoutProperties {
private int id;
private String name;
private float score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setScore(float score) {
this.score = score;
}
public float getScore() {
return score;
}
}
第四步,调用bean实例。
在我学习过程里,我是在controller层测试调用Bean。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
//表明这是一个 Controller
@Controller
//RestController是一种Rest风格的Controller,可以直接返回对象而不返回视图,返回的对象可以使JSON,XML等
//使用自动配置,主动添加并解析bean,配置文件等信息
//@EnableAutoConfiguration
public class SpringController {
@Autowired
DataConfig dataConfig;
@Autowired
BeanWithoutProperties beanWithoutProperites;
@RequestMapping("/MyWorld")
//表示返回JSON格式的结果,如果前面使用的是@RestController可以不用写
@ResponseBody
String myWorld() {
return "+++ " + dataConfig.setMyConfig();
//return "Hello" + myBean.getName() + " " + "Your ID is: " + myBean.getId() + "!!!";//返回结果为字符串
//return "hello";
}
@RequestMapping("/CaoWorld")
//表示返回JSON格式的结果,如果前面使用的是@RestController可以不用写
@ResponseBody
String caoWolrd() {
return "=== " + dataConfig.setCaoConfig();
//return "Hello" + myBean.getName() + " " + "Your ID is: " + myBean.getId() + "!!!";//返回结果为字符串
//return "hello";
}
@RequestMapping("/NewWorld")
//表示返回JSON格式的结果,如果前面使用的是@RestController可以不用写
@ResponseBody
BeanWithoutProperties getNewBean() {
return beanWithoutProperites;
}
@RequestMapping("/JiaWorld")
//表示返回JSON格式的结果,如果前面使用的是@RestController可以不用写
@ResponseBody
String getNewBean02() {
ApplicationContext ctx = new AnnotationConfigApplicationContext(DataConfig.class);
//1. 第一种方式通过name
//BeanWithoutProperties02 b2 = (BeanWithoutProperties02) ctx.getBean("get02");
//2. 第二种方式通过class
BeanWithoutProperties02 b2 = ctx.getBean(BeanWithoutProperties02.class);
return b2.getId() + "***" + b2.getName();
}
}
调用Bean实例方法,亲测有2种方式:
1、通过@Autowired实现Bean注入:
@Autowired
BeanWithoutProperties beanWithoutProperites;
2、 用AnnotationConfigApplicationContext来实现注入,调用getBean实现获取Bean实例。
获取Bean实例有2中方式,一种是通过name, 一种是通过class。
(注:BeanWithoutProperties02是跟BeanWithoutProperties一样的Bean类,只是这里为了学习不同的调用方式,额外增加了它)
@RequestMapping("/JiaWorld")
//表示返回JSON格式的结果,如果前面使用的是@RestController可以不用写
@ResponseBody
String getNewBean02() {
ApplicationContext ctx = new AnnotationConfigApplicationContext(DataConfig.class);
//1. 第一种方式通过name
//BeanWithoutProperties02 b2 = (BeanWithoutProperties02) ctx.getBean("get02");
//2. 第二种方式通过class
BeanWithoutProperties02 b2 = ctx.getBean(BeanWithoutProperties02.class);
return b2.getId() + "***" + b2.getName();
}
参考文献:
非常值得一看的Spring boot 配置属性注入Bean类
关于@Value注解的用法
使用 AnnotationConfigApplicationContext 注册配置类
网友评论