目录
2.5 EnableConfigurationProperties
5.3.4 HelloServiceAutoConfigration
为了简化配置,使spring的使用更加方便,例如:可以回忆一下springboot中是如何继承redis的。
spring配置方式的进化过程:
springboot starter作用:
使用springboot后集成常用的第三方框架变得非常简单了。
springboot starter带来的问题
starter使SpringBoot集成第三方框架变得简单,但对刚刚上手SpringBoot的人来说,可能只知道配置属性是在application.xml或application.yml中添加,但他们各自的属性都有哪些,具体怎么配置,却无从下手
自动装配相关的注解示例: 
为更好的理解装配过程,复习一下元注解。
作用:为容器导入bean的定义
常用的三种方式:
1) @Import(value={A.class}) 将A类导入容器中,交给IOC管理
2) @Import(value={XxxImportBeanDefinitionRegister.class}) 也是将bean导入容器中,与上面不同的是实现了ImportBeanDefinitionRegistrar接口,该接口用来动态注册bean
3) @Import(value={XxxImportSelector.class}) 可以批量导入,需要实现ImportSelector接口,该接口返回一个数组(需要注册的bean的全路径),实现批量注册。
被挂上@Configuration注解,表明它是一个配置类,作用等同于xml配置,里面有被@Bean注解的方法,也等同于xml配置的各种
作用就是将@ConfigurationProperties修饰的Bean加入到IOC容器中。@ConfigurationProperties与@EnableConfigurationProperties注解结合,可以方便的读取配置文件。
META-INF/spring.factories
如:
一个空的jar包,提供依赖管理。这些依赖可能用于自动装配或其他的第三方jar
命名规范
编写一个自动配置的模块,用于完成自动配置, 上一步的启动器依赖于自动配置, 用户在使用时自需引入启动器模块,即可自动导入自动配置模块。
创建一个空工程,在该工程中创建两个模块,一个为start模块(启动类),一个为自动配置模块。
创建一个名为:springbootstartdemo的空项目
创建空目录后创建两个项目模块 demo-spring-boot-starter 与 demo-spring-boot-starter-autoconfigurer
1.创建好空目录后👇 进入项目设置

2.创建两个maven项目普通项目即可

启动模块只需要导入自动配置模块,所以不需要集成springboot父项目。 自动配置模块需要依赖一spring-boot-start(Spring Boot核心starter,包含自动配置、日志、yal配置文件支持),所以需要spring-boot-starter-parent为父项目。
一个为start模块,启动模块,不需要做任何开发,只用在pom.xml配置文件中导入自动配置模块。使用者通过导入启动模块,启动模块导入自动配置模块,(可以将start启动模块想象成使用导火索)
请注意上面提到的命名规范。

自动配置模块

启动类的作用是导入自动配置模块,并不要进行编码开发
在自动配置模块如下:
该类的作用时读取项目的配置文件, 使用@ConfigurationProperties注解,并指定读取demo.starter前缀的配置文件。
- @ConfigurationProperties(prefix = "demo.starter")
- public class HelloProperties {
-
- //greeting问候
- private String greeting;
-
- public String getGreeting() {
- return greeting;
- }
-
- public void setGreeting(String greeting) {
- this.greeting = greeting;
- }
定义的接口
- package com.zking.boot;
-
- public interface IHelloService {
- String sayHello(String name);
- }
该类用于通过传入的参数,与配置文件中的配置,组成一个字符串返回, 没有业务意义,仅用于演示spring start的运行原理。
- public class HelloService implements IHelloService {
-
- HelloProperties helloProperties;
-
- public HelloProperties getHelloProperties() {
- return helloProperties;
- }
-
- public void setHelloProperties(HelloProperties helloProperties) {
- this.helloProperties = helloProperties;
- }
-
- @Override
- public String sayHello(String name) {
- return helloProperties.getGreeting() + " " + name;
- }
- }
该类用于进行自动配置
- @Configuration //表明该类为配置类
- @ConditionalOnWebApplication //指定该配置类在web环境下有效
- @EnableConfigurationProperties(HelloProperties.class)
- public class HelloServiceAutoConfigration {
-
- @Autowired
- HelloProperties helloProperties;
-
- @Bean
- public IHelloService helloService() {
- HelloService helloService = new HelloService();
- helloService.setHelloProperties(helloProperties);
- return helloService;
- }
-
- }
编写spring.factories, 可以参mybatis-spring-boot-autoconfigure编写
- # Auto Configure
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- org.mybatis.spring.boot.autoconfigure.MybatisLanguageDriverAutoConfiguration,\
- org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
具体配置
- # Auto Configure
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- org.lisen.starter.HelloServiceAutoConfigration

将工程安装到本地仓库,以便于其他的工程使用。
因为启动模块需要依赖与自动配置模块,所以先安装自动配置模块,再安装启动模块。

到此为止一个自动装配模块就开发完成,下面看看如何使用。
创建一个springboot项目,并在pom.xml中引入上面的自动配置模块(只需引入启动类,启动类自动引入配置模块)。

pom.xml
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.2.RELEASE</version>
- </parent>
-
-
- <properties>
- <java.version>1.8</java.version>
- <mysql.driver.version>5.1.44</mysql.driver.version>
- </properties>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.lisen.starter</groupId>
- <artifactId>demo-spring-boot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
项目配置文件: application.properties
- server.port=8080
- server.servlet.context-path=/
- server.tomcat.uri-encoding=utf-8
-
- demo.starter.greeting=hello
启动类:
- @SpringBootApplication
- public class AppStarter {
-
- public static void main(String[] args) {
-
- SpringApplication.run(AppStarter.class, args);
-
- }
-
- }
controller
- @RestController
- public class HelloController {
-
- @Autowired
- private IHelloService helloService;
-
- @RequestMapping("/hello")
- public String hello() {
- return helloService.sayHello("zs");
- }
-
- }
最后启动服务,调用http://localhost:8080/hello,如果提示信息正常输出则说明运行成功。