对应常用的功能组件,我们可以将其制作成一个Starter.这样在整个系统中都能达到只需要导入依赖即可使用功能,无需其他配置。
1、Starter命名规则
1.1、官方命名方式
- 前缀:
spring-boot-starter-
- 规范:
spring-boot-starter-模块名
- 示例:
spring-boot-starter-web
1.2、第三方命名方式
- 后缀:
-spring-boot-starter
- 规范:
模块名-spring-boot-starter
- 示例:
mybatis-spring-boot-starter
2、自动装配Bean
通过使用@Configuration
以及Spring4中@Conditional
条件注解等来完成Bean的自动装配
3、配置自动装配Bean
在类路径下新建文件夹META-INF
,在该文件夹下新建spring.factories
文件。
# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=自动装配Bean的全限定路径
原理戳
4、示例程序
4.1、利用IDEA创建一个SpringBoot工程
- Maven依赖如下
4.0.0 com.hanson example-spring-boot-starter 0.0.1-SNAPSHOT jar example-spring-boot-starter 自定义Starter org.springframework.boot spring-boot-starter-parent 1.5.15.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter org.projectlombok lombok true
- 创建属性配置类
package com.hanson.example.spring.boot.starter.ext;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;@Data@ConfigurationProperties(prefix = "hanson")public class HansonProperties { private String name; private Integer age;}
- 创建应用属性配置类程序
package com.hanson.example.spring.boot.starter.ext;import lombok.Data;@Datapublic class HansonService { private HansonProperties hansonProperties; public String introduce() { return "hello,i am " + hansonProperties.getName() + " and i am " + hansonProperties.getAge() + "years old!"; }}
- 创建自动配置类
package com.hanson.example.spring.boot.starter;import com.hanson.example.spring.boot.starter.ext.HansonProperties;import com.hanson.example.spring.boot.starter.ext.HansonService;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@EnableConfigurationProperties(HansonProperties.class)public class HansonAutoConfiguration { @Bean public HansonService hansonService(HansonProperties hansonProperties) { HansonService hansonService = new HansonService(); hansonService.setHansonProperties(hansonProperties); return hansonService; }}
- 新增spring.factories文件 在类路径下创建META-INF文件夹,在该文件下创建一个spring.factories文件。其内容如下
# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.hanson.example.spring.boot.starter.HansonAutoConfiguration
- 打包安装
4.2、测试程序
- 利用IDEA 创建一个Spring Boot工程,依赖如下
4.0.0 com.hanson example-spring-boot-starter-test 0.0.1-SNAPSHOT jar example-spring-boot-starter-test 自定义Starter测试 org.springframework.boot spring-boot-starter-parent 1.5.16.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter com.hanson example-spring-boot-starter 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
- 编写配置文件
在application.yml或者application.properties添加一下配置
hanson.name=Hansonhanson.age=25
- 编写主程序测试
package com.hanson.example.spring.boot.starter.test;import com.hanson.example.spring.boot.starter.ext.HansonService;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplicationpublic class ExampleSpringBootStarterTestApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(ExampleSpringBootStarterTestApplication.class, args); HansonService bean = context.getBean(HansonService.class); System.out.println(bean.introduce()); }}
- 运行结果
2018-09-14 19:46:29.157 INFO 2184 --- [ main] .ExampleSpringBootStarterTestApplication : Started ExampleSpringBootStarterTestApplication in 0.966 seconds (JVM running for 2.281)hello,i am Hanson and i am 25years old!2018-09-14 19:46:29.167 INFO 2184 --- [ Thread-4] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@120d6fe6: startup date [Fri Sep 14 19:46:28 CST 2018]; root of context hierarchy2018-09-14 19:46:29.167 INFO 2184 --- [ Thread-4] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown