您的位置:首页 > 编程语言 > Java开发

SpringBoot__自定义一个简单的Starter

2019-08-06 20:22 1086 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_37432174/article/details/98656270

在SpringBoot的源码中可以看到很多不同的Stater,Stater与很多的自动化配置有关,Stater是通过Spring以及SpringMVC的知识点实现的。

@Conditional
这个注解是条件注解,
@profile
特殊的条件注解,这个
@Conditional
条件注解就是Starter 的核心。当 classpath 下存在某一个 Class 时,某个配置才会生效。

结合代码来理解吧。
Starter就是普通的Maven项目,这里记住了这里是一个普通的Maven,不能是一个SpringBoot项目(SpringBoot不能被依赖)。

结构

依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>

HelloProperties
通过在application.yml中配置进行属性注入,你要是愿意也可以在
application.properties中进行配置进行属性注入,也可以给配置文件改名改路径但是要使用

@PropertySource(value="classpath:xxx.properties")
,这都是可以的。

/**
* @author ZSL
* @ClassName HelloYaml
* @description
*
*  @ConfigurationProperties 类型安全的属性注入,
*  即将 application.yml 文件中前缀为 helloyaml 的属性注入到这个类对应的属性上
*
* @date 2019/8/6
*/
@ConfigurationProperties(prefix = "helloyaml")
public class HelloYaml {

private String name;
private Integer age;

public HelloYaml(String name, Integer age) {
this.name = name;
this.age = age;
}

public HelloYaml() {
}

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;
}

@Override
public String toString() {
return "HelloYaml{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

将 application.yml 中配置的属性值直接注入到这个实例中, @ConfigurationProperties 类型安全的属性注入,即将 application.yml 文件中前缀为 helloyaml 的属性注入到这个类对应的属性上,这里要注意的是前缀存在中文或者大写都会报错

application.yml

hellyaml:
name: zsl
age: 18

HelloService
自定义一个类,用于当自动装配类HelloServiceAutoConfiguration
的返回对象

/**
* @author ZSL
* @ClassName HelloService
* @description
* @date 2019/8/6
*/
public class HelloService {

private String name;
private Integer age;

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;
}

public HelloService(String name, Integer age) {
this.name = name;
this.age = age;
}

public HelloService() {
}

@Override
public String toString() {
return "HelloService{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

HelloServiceAutoConfiguration
自定义一个自动装配类,首先它是一个配置类,它要是和我们的配置文件类HelloProperties相关,然后如何存在HelloService类才可以生效,HelloProperties需要在自动装配类中注入HelloService类中。

/**
* @author ZSL
* @ClassName HelloServiceAutoConfiguration
* @description:
*
* @Configuration 注解表明这是一个配置类
*
* @EnableConfigurationProperties 注解是使我们之前配置的 @ConfigurationProperties 生效,让配置的属性成功的进入 Bean 中
*
* @ConditionalOnClass 表示当项目当前 classpath 下存在 HelloService 时,后面的配置才生效
*
* @date 2019/8/6
*/
@Configuration
@EnableConfigurationProperties(HelloYaml.class)
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {

@Autowired
private HelloYaml helloYaml;

@Bean
HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setName(helloYaml.getName());
helloService.setAge(helloYaml.getAge());
return helloService;
}
}
  1. @Configuration 注解表明这是一个配置类
  2. @EnableConfigurationProperties注解的作用是:使使用

@ConfigurationProperties 注解的类生效。
如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。
@EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。

@EnableConfigurationProperties 文档中解释:
当@EnableConfigurationProperties注解应用到你的@Configuration时, 任何被@ConfigurationProperties注解的beans将自动被Environment属性配置。

这和我们前面的博客讲配置文件的位置问题和命名问题的在启动时在环境属性中进行配置类似。

  1. @ConditionalOnClass 表示当项目当前 classpath 下存在 HelloService 时,后面的配置才生效

SpringBoot框架还提供了很多@Condition
@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)

接下来在resources 目录下创建一个名为 META-INF 的文件夹,在META-INF 的文件夹下新建一个spring.factories 的文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=config.HelloServiceAutoConfiguration

那么我们要这个文件干嘛用了?
我们来看SpringBoot的启动类(这里随便拿了一个启动类来分析)

@SpringBootApplication
public class FreemarkerdemoApplication {

public static void main(String[] args) {
SpringApplication.run(FreemarkerdemoApplication.class, args);
}

}

启动类都有一个 @SpringBootApplication 注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
Class<?>[] exclude() default {};

@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};

@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};

@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}

这个@SpringBootApplication 注解是一个组合注解,这里我们来看@EnableAutoConfiguration:

@EnableAutoConfiguration 表示启用 Spring 应用程序上下文的自动配置,该注解会自动导入一个名为 AutoConfigurationImportSelector 的类,而这个类会去读取一个名为 spring.factories 的文件, spring.factories 中则定义需要加载的自动化配置类

这里大概跟踪一下上面说的源码

任意一个框架的 Starter ,都能看到它有一个 spring.factories 文件.

安装
因为是普通的Maven项目,所以我们打包安装一下,在本地仓库可以找到。
Install一下。

测试
新建一个SpringBoot项目,把刚刚打包的项目,依赖到这新建一个SpringBoot中,在输出自动装配类中的bean的返回值,也就是HelloService看看。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: