写了一个自己的spring-boot-start,亲测可用

大家好,我是方圆


1. 先给你看看整体布局

在这里插入图片描述

1.1 pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wang</groupId>
	<artifactId>spring-boot-start-hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-start-hello</name>
	<description>自定义的一个spring boot的start pom</description>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
			<version>2.3.2.RELEASE</version>
		</dependency>
	</dependencies>
</project>
  • 其中我们要导入的是自动装配的包
  • 之后我们在使用它的时候,别的项目中导入的包名如下所示
        <dependency>
            <groupId>com.wang</groupId>
            <artifactId>spring-boot-start-hello</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

1.2 HelloService

这个Template非常简单,我们测试它的时候直接让它执行sylHello方法

public class HelloService {
    
    

    private String msg;

    public String sayHello() {
    
    
        return "hello! " + msg;
    }

    public String getMsg() {
    
    
        return msg;
    }

    public void setMsg(String msg) {
    
    
        this.msg = msg;
    }
}

1.3 HelloServiceProperties

@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
    
    

    private final static String MSG = "starter";

    private String msg = MSG;

    public String getMsg() {
    
    
        return msg;
    }

    public void setMsg(String msg) {
    
    
        this.msg = msg;
    }
}
  • 这个就有点儿说法了哦,Properties用来给Template注入属性,我们其中定义了一个默认值
  • @ConfigurationProperties(prefix = "hello"),指定前缀,这个可以供我们在application.properties文件中对其属性进行修改

1.4 HelloServiceConfiguration

我们可以在自动装配包的spring.factories文件中看到很多以AutoConfiguration结尾的类,我们这个命名也如此

//标记它为配置类
@Configuration
//指定配置属性的类
@EnableConfigurationProperties({
    
    HelloServiceProperties.class})
//当给定的类名在类路径上存在,则实例化当前Bean
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
    
    
	
	//将Properties类装配进来,为Template指定属性
    @Autowired
    private HelloServiceProperties helloServiceProperties;

	//标注@Bean,返回HelloService对昂
    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
    
    
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
}

1.5 spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.wang.autoconf.HelloServiceAutoConfiguration

到这里,我们自定义的start就写完了,测试一下!

2. 测试

我们其他项目中,导入它的依赖

        <dependency>
            <groupId>com.wang</groupId>
            <artifactId>spring-boot-start-hello</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

这儿样之后,它就能被Spring管理了,我们需要它的时候就能够自动装配

猜你喜欢

转载自blog.csdn.net/qq_46225886/article/details/107896387