其他分享
首页 > 其他分享> > Spring Boot自定义Start组件开发

Spring Boot自定义Start组件开发

作者:互联网

Start 组件开发,核心是自动注解类的注解顺序,即根据条件进行注解。

首先我们来理解@Configuration、@Value、@Bean注解
在这里插入图片描述
以DataSourceAutoConfiguration类注解为例,
@Configuration表名此类是个配置类;
@ConditionalOnClass表明当DataSource.class, EmbeddedDatabaseType.class存在时才会继续进行下面的配置;@EnableConfigurationProperties从IOC容器中获取application.properties配置文件的Bean;
@Import 导入相关类。
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20190911100209250.png
@ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的。
@EnableConfigurationProperties详细说明

自动加载核心注解说明在这里插入图片描述

有了以上的了解后,来创建 Maven 项目, 目录结构如下:
在这里插入图片描述
添加pom.xml依赖(根据组件功能添加)

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.0.0.RELEASE</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

在使用Spring官方的Starter时通常可以在application.properties中来配置参数覆盖掉默认的值。即sex的值会被配置文件中的值替换掉。
PersonProperties 类

@ConfigurationProperties(prefix = "spring.person")
public class PersonProperties {
    // 姓名
    private String name;
    // 年龄
    private int age;
    // 性别
    private String sex = "M";

    // Getter & Setter
    }

PersonService类

public class PersonService {

    private PersonProperties properties;

    public PersonService() {
    }

    public PersonService(PersonProperties properties) {
        this.properties = properties;
    }

    public void sayHello(){
        System.out.println("大家好,我叫: " + properties.getName() + ", 今年" + properties.getAge() + "岁"
                + ", 性别: " + properties.getSex());
    }
}

PersonServiceAutoConfiguration 类

@Configuration 
@EnableConfigurationProperties(PersonProperties.class)
@ConditionalOnClass(PersonService.class)
@ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true)
public class PersonServiceAutoConfiguration {

    @Autowired
    private PersonProperties properties;

    @Bean
    @ConditionalOnMissingBean(PersonService.class)  // 当容器中没有指定Bean的情况下,自动配置PersonService类
    public PersonService personService(){
        PersonService personService = new PersonService(properties);
        return personService;
    }
}

spring.factories文件
注意:META-INF是自己手动创建的目录,spring.factories也是手动创建的文件,在该文件中配置自己的自动配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.cnbi.PersonServiceAutoConfiguration

最后将项目打包 mvn clean install

在另一个项目中添加依赖 如何在项目中添加本地jar包

		<dependency>
			<groupId>cnbi</groupId>
			<artifactId>helloworld-spring-boot-starter</artifactId>
			<version>1.0</version>
		</dependency>

配置application.properties

spring.person.age=23
spring.person.name=ss
spring.person.sex=F

启动测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class LarkApplicationTests {
	@Autowired
	@SuppressWarnings("ALL")
	private PersonService personService;

	@Test
	public void testHelloWorld() {
		personService.sayHello();
	}

}

2019-09-11 09:25:19.453 INFO 7272 — [ main] com.tiamo.lark.LarkApplicationTests : Started LarkApplicationTests in 4.893 seconds (JVM running for 6.7)

大家好,我叫: ss, 今年23岁, 性别: F

标签:PersonService,自定义,spring,Boot,class,public,Spring,注解,properties
来源: https://blog.csdn.net/qq_19782697/article/details/100727302