springboot自定义场景自动配置及测试
中国加油,武汉加油!
项目准备
- 新建springboot项目nz1904-springboot-auto
- 依赖选Web的Spring Web即可
- 注解加入这个
<!--自动配置支持的包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
- 删掉原来的主启动类(也可以不用这步骤)
1 案例 : 查询学生信息
1.1 写一个业务
package com.wpj.starter;
public class StudentService {
private Integer id;
private String name;
private Integer age;
public StudentService() {}
public StudentService(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
/**
* 打印学生信息
*/
public void printStu(){
System.out.println("自动配置完成: 该学生的id为: "+ id +", 姓名为: " + name + ", 年龄为: " + age);
}
}
1.2 编写属性配置
package com.wpj.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "wpj.auto") // 配置文件自定义前缀
public class StudentServiceProperties {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}
1.3 编写自动配置
package com.wpj.starter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // 标记是一个配置
@ConditionalOnClass(StudentService.class) // 必须有用该类才生效
@EnableConfigurationProperties(StudentServiceProperties.class) // 属性配置
public class StudentServiceAutoConfiguration {
@Bean // 实例化一个bean
@ConditionalOnMissingBean // 如果新ew一个了这个Bean就失效,以防冲突
public StudentService netWorkService(StudentServiceProperties ssp){
return new StudentService(ssp.getId(), ssp.getName(), ssp.getAge());
}
}
1.4 定义对外注解
package com.wpj.starter;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 自动配置
*/
@Inherited
@Documented //公共api,所有人都可以调用
@Target(ElementType.TYPE) // 应用于类上
@Retention(RetentionPolicy.RUNTIME) // 运行时生效
@Import(StudentServiceAutoConfiguration.class) // 导入配置
public @interface EnableStudentServiceAutoConfiguration {
}
2. 测试该业务
2.1 新建一个springboot项目nz1904-springboot-test
配置同项目准备
2.2 导入业务依赖
就是上面写的案例项目的maven地址
<dependency>
<groupId>com.wpj</groupId>
<artifactId>nz1904-springboot-auto</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2.3 写一个controller
package com.wpj.controller;
import com.wpj.starter.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("findStudent")
public String findStudent(){
studentService.printStu();
return "自动配置成功....";
}
}
2.4 主启动类添加自定义的注解
package com.wpj;
import com.wpj.starter.EnableStudentServiceAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableStudentServiceAutoConfiguration
public class StudentApplication {
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class, args);
}
}
2.5 application.properties添加学生数据和编码
wpj.auto.id=2017329125
wpj.auto.name=jieKaMi
wpj.auto.age=22
2.6 运行主启动类,访问localhost:8080/findStudent
2.7 测试完成
有个小问题,中文会乱码,暂未解决。。。