- 定义业务接口、需要的实体类等业务对象
- 定义配置属性类、
- 定义自动装配类、
- Resources目录下新建META-INF/spring.factories,加入自定义starter的配置项
创建一个 maven 项目
在项目内的 pom.xml 中导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.6.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.6.3</version>
<optional>true</optional>
</dependency>
创建一个 Hello 实体类
//你的业务类干什么事
public class Hello {
private String name;
private String msg;
public void show(){
//我的业务
System.out.println(name+"说:"+msg);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
手写 HelloAutoConfigure 配置类
//自动配置类
@EnableConfigurationProperties({HelloProperties.class})
@Configuration
public class HelloAutoConfigure {
@Autowired
HelloProperties hp;
@Bean//<bean name="hello" class="com.huarui.Hello"></bean>
public Hello hello(){
Hello hello=new Hello();
hello.setName(hp.getName());
hello.setMsg(hp.getMsg());
return hello;
}
}
再手写一个 HelloProperties 属性配置文件
//属性配置文件:给hello对象的属性赋值,不满意默认值,那么可以在apllication.properties
//中重新赋值
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
private String name="tom";
private String msg="我很刷牛";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
新建一个名为 spring.factories 的自动配置类
#自动配置类的路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.huarui.HelloAutoConfigure
根据图片提示即可得到需要的Jar包