1、新建springboot项目
方式一:spring官网提供了图形化的新建springboot项目的方式
https://start.spring.io
如图所示,填写项目坐标,选择项目依赖,下载即可。
方式二:使用IDEA直接创建
IDEA集成了spring Initializer创建项目的方式,我们使用此种方式创建
点击下一步,填写项目坐标信息:
选择项目依赖:
然后next填写项目名称,点击finish,然后就默默的等待就好!
2、项目结构
3、新建 AppController
@RestController
public class AppController {
@GetMapping("/get")
private String get(){
return "hello,App!";
}
}
启动 SpringbootFirstappApplication 并观察结果:
浏览器访问:
ok,可以正常访问,至此,项目成功创建。
4、yaml配置注入
4.1 在springboot项目中的resources目录下新建一个文件 application.yml
4.2 编写一个实体类 Person
@Component
public class Person {
private String name;
private int age;
//此处省略有参无参构造、get、set方法、toString()方法
}
4.3、我们原来是如何给bean注入属性值的!@Value
@Component
public class Person {
@Value("zhangsan")
private String name;
@Value("18")
private int age;
//此处省略有参无参构造、get、set方法、toString()方法
}
4.4、用springboot为我们创建好的测试类跑一遍
@SpringBootTest
class SpringbootFirstappApplicationTests {
@Autowired
Person person;
@Test
void contextLoads() {
System.out.println(person.toString());
}
}
4.5 接下来我们使用yaml的方式看看如何给属性赋值
在创建好的application.yml文件中加入以下内容:
person:
name: zhangsan
age: 18
然后注入到person类中,需要使用注解@ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
//....
}
然后继续使用测试类跑一遍试试:
总结一下
- 编写yaml配置文件,配置对应的属性值
- 在对应的类上面添加注解 @ConfigurationProperties
- 在注解中填写配置文件的值 (prefix = “person”)
5、自动装配
我们从一个注解开始
@SpringBootApplication
public class SpringbootFirstappApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstappApplication.class, args);
}
}
@EnableAutoConfiguration
继续往下走:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
我们看到有一个引入了这样一个类
AutoConfigurationImportSelector
打开这个类从这里开始
org.springframework.boot.autoconfigure.
AutoConfigurationImportSelector#selectImports
往下找,发现这样一个方法
getCandidateConfigurations //顾名思义 获取候选配置类
继续往下走,我们会看到这样一块内容
“META-INF/spring.factories”
从上面内容可以看到,是在这个文件里面获取配置类相关信息
我们在spring-boot-autoconfigure这个包中找到这个文件
打开这个文件。
发现一堆以AutoConfiguration结尾的文件,我们以RabbitAutoConfiguration为例打开看看
打开这里的RabbitProperties
这里就很熟悉了,这就是yaml的方式给对象赋值,也就是说这里的属性都可以在yaml的配置文件中配置
可以打开yaml文件输入spring.rabbitmq看看工具给我们的提示
可以看到一堆属性值,而这些值都是RabbitProperties这个类中的属性。
总结一下:
- SpringBoot在启动的时候从META-INF/spring.factories中获取EnableAutoConfiguration指定的值
- 将这些值作为自动配置类被导入 , 自动配置类就生效 , 帮我们进行自动配置工作;
- 有了自动配置类 , 就免去了我们手动编写配置注入功能组件等的工作;
6、模拟springboot的套路,定义一个自动配置类;
- 我们将我们的项目改造一下:
@Component
@ConfigurationProperties(prefix = "person")
public class PersonProperties {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- 新建文件PeresonAutoConfiguration
@Configuration
@EnableConfigurationProperties({PersonProperties.class})
public class PeresonAutoConfiguration {
@Autowired
private PersonProperties personProperties;
@Bean
public Person person(){
System.out.println("PeresonAutoConfiguration.....start");
Person person = new Person();
person.setAge(personProperties.getAge());
person.setName(personProperties.getName());
System.out.println(person.toString());
return person;
}
}
- 在resource下面新建 META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.xxx.springboot.springbootfirstapp.PeresonAutoConfiguration
激活自动装配
@EnableAutoConfiguration
//@SpringBootApplication
@EnableAutoConfiguration
public class SpringbootFirstappApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstappApplication.class, args);
}
}
由于引导类@SpringBootApplication注解包含自动配置这个注解,所以也可以直接启动,效果是一样的。
总结一下:
- 编写配置类,并与yaml配置文件对应
- 编写自动转配类XXXAutoAconfigutation,并菩提之刚刚写好的配置文件
- 目录 META-INF/spring.factories 中配置自定义装配类
- 激活自动装配@EnableAutoConfiguration
自定义starter就是这个套路,现在可以去套路你自己的starter包了。