SpringBoot自動配置(一)-- 自定義starter

一、創建自定義starter

先創建一個my-starter的module,主要配置依賴如下

	<artifactId>my-starter</artifactId>
	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

建一個自動讀取配置的類

	@ConfigurationProperties(prefix = "hello")                                                                                
	public class HelloServiceProperties {
	                                                                                                                          
	    private String msg;                                                                                                   
	                                                                                                                          
	    public String getMsg() {                                                                                              
	        return msg;                                                                                                       
	    }                                                                                                                     
	                                                                                                                          
	    public void setMsg(String msg) {                                                                                      
	        this.msg = msg;                                                                                                   
	    }                                                                                                                                                                                                                                               
	}

在application.yml中配置hello.msg就能讀取到

弄個自動配置的bean

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

弄個自動配置的配置類,主要自動裝載上面的HelloService類

	@Configuration
	@EnableConfigurationProperties(HelloServiceProperties.class)
	@ConditionalOnClass(HelloService.class)
	@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
	public class HelloAutoConfiguration {
	
	    @Autowired
	    private HelloServiceProperties helloServiceProperties;
		
		//當容器中沒有HelloService就自動注入一個bean
	    @Bean
	    @ConditionalOnMissingBean(HelloService.class)
	    public HelloService helloService() {
	        HelloService helloService = new HelloService();
	        helloService.setMsg(helloServiceProperties.getMsg());
	        return helloService;
	    }
	}

還有一個重要步驟:在resources文件夾下建一個META-INF文件夾,再建一個spring.factories文件,寫上

	org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.season.HelloAutoConfiguration

最後利用maven進行clean、install

二、測試starter

創建另一個my-starter-test的module,主要配置依賴如下

	<artifactId>my-starter-test</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.season</groupId>
            <artifactId>my-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

然後再application.yml中寫上

	hello:
	  msg: season

啓動類如下:

	@RestController
@SpringBootApplication
public class HelloSpringBootStarterTestApplication {

    @Autowired
    private HelloService helloService;

	    @RequestMapping("/")
	    public String index() {
	        return helloService.sayHello();
	    }
	
	    public static void main(String[] args) {
	        SpringApplication.run(HelloSpringBootStarterTestApplication.class, args);
	    }
	}

啓動,然後訪問本地8080端口就能看到輸出”hello season“了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章