第一步:在com.pp.bean下创建一个Car类
package com.pp.springboothelloworld.bean;
public class Car {
private String name;
private Integer age;
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
第二步:在Persion中添加一句private Car car,并且将@ConfigurationProperties(prefix = “persion”)注释取消
package com.pp.springboothelloworld.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component //@Components说明把当前的内容放在容器中去
//@PropertySource(value = "persion.properties")
@ConfigurationProperties(prefix = "persion")
public class Persion {
// @Value("${persion.name}")的使用用,获取到配置文件的persion.name然后赋值给这里的name
@Value("${persion.name}")
private String name;
private Integer age;
private Boolean sex;
private Map<String,Object> maps;
private List<Object> lists;
private Car car;
@Override
public String toString() {
return "Persion{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", maps=" + maps +
", lists=" + lists +
",car="+car+
'}';
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
}
第三步:在application.perproties
#浏览器端口号
server.port=8080
persion.name=张三
persion.age=${random.int}
persion.maps.k1=a1
persion.maps.k2=a2
persion.lists=a,b,c
persion.car.name=宝马
第四步:在com.pp.controller中的HelloController类中定义一个Persion并且进行注解
package com.pp.springboothelloworld.controller;
import com.pp.springboothelloworld.bean.Persion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@Value("${persion.name}")
private String name;
@Autowired
private Persion persion;
@ResponseBody
@RequestMapping("hello")
public String hello(){
return "hello word"+persion.getMaps().get("k1");
}
}
注解里面将Persion的persion.getMaps().get(“k1”)获取注解文件的为k1的值
第五步:上面步骤运行结果
第六步:如果我们在HelloController中hello中修改下面内容
package com.pp.springboothelloworld.controller;
import com.pp.springboothelloworld.bean.Persion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@Value("${persion.name}")
private String name;
@Autowired
private Persion persion;
@ResponseBody
@RequestMapping("hello")
public String hello(){
return "hello word"+persion.getLists();
}
}
结果显示
上面就是对bean包下面的类进行测试,其中通过在application.perperties中定义好数据在去控制层进行编码,最后在浏览器总运行查看页面。
扩展:
1.除了上面的方法,我们也可以在 est/java/com/pp/springhelloworld类中定义的又测试方法
package com.pp.springboothelloworld;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootHelloworldApplicationTests {
@Test
void contextLoads() {
}
}
2.然后我们在里面进行写方法
package com.pp.springboothelloworld;
import com.pp.springboothelloworld.bean.Persion;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootHelloworldApplicationTests {
@Autowired
private Persion persion;
@Test
void contextLoads() {
System.out.println(persion.getLists());
}
}
上面的内容中,我们先使用注解对private Persion persion进行注解,然后再测试方法里面对persion.getLists()进行打印到控制台。
3.运行结果
源码地址:https://gitee.com/yangforever/project-learning/tree/master/demo/SpringBoot/spring-boot-helloworld2