1.先在pom下添加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <!-- <optional>true</optional>--> </dependency>
项目结构
2.创建配置获取的类
@ConfigurationProperties(prefix = "simplebean")//自动获取配置文件中前缀为prefix值的属性,把值传入对象参数 public class SimpleBean { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } @Override public String toString() { return "SimpleBean{" + "id=" + id + ", name='" + name + '\'' + '}'; } public void setName(String name) { this.name = name; } }
3.创建配置类
4.在resources下创建spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.zsl.config.MyAutoConfiguration
//5.也可用热插拔强制加注解引入某个类
5.创建测试项目测试即可