SpringBoot自定義Start

                                   SpringBoot自定義Start

一、start介紹

啓動器(Start)包含許多依賴項,這些依賴項是使項目快速啓動和運行所需的依賴項。

總結:整合特定功能所需要的的所有jar包。

二、原理

springboot在啓動的時候會將所依賴所有start的jar包中的META-INF下的spring-factories中配置的XXAutoConfiguration類注入到spring容器中。

1、Spring Boot在啓動時掃描項目所依賴的JAR包,尋找包含spring.factories文件的JAR包,

2、然後讀取spring.factories文件獲取配置的自動配置類AutoConfiguration,
3、然後將自動配置類下滿足條件(@ConditionalOnXxx)的@Bean放入到Spring容器中(Spring Context)
這樣使用者就可以直接用來注入,因爲該類已經在容器中了

三、start案例

以阿里開發的dubbo爲例:

 <dependency>
   <groupId>com.alibaba.boot</groupId>
   <artifactId>dubbo-spring-boot-starter</artifactId>
   <version>${dubbo.starter.version}</version>
</dependency>

下面就是該start所需要的的所有jar包依賴信息

<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">
    <parent>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-parent</artifactId>
        <version>0.2.0</version>
        <relativePath>../dubbo-spring-boot-parent</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-spring-boot-starter</artifactId>
    <packaging>jar</packaging>
    <name>Dubbo Spring Boot Starter</name>
    <description>Dubbo Spring Boot Starter</description>


    <dependencies>
        <!-- Spring Boot dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <scope>true</scope>
        </dependency>

        <!-- Dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>

        <!-- ZK -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-autoconfigure</artifactId>
            <version>${project.version}</version>
        </dependency>

    </dependencies>
</project>

四、自定義start

1、步驟

1、引入spring-boot-autoconfigure/spring-boot-configuration-processor依賴
2、配置XXautoConfigure實體類
3、配置spring.factories配置文件

2、搭建自定義spring-boot-start

項目目錄結構

UserProperties.class(該自定義start需要在被引入項目中配置的信息)

package com.yuyou.user;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.user")
public class UserProperties {
	
	private String name;
	
	private int age;
	
	private String address;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}

UserService.class(該start需要完成的功能方法)

package com.yuyou.user;

public class UserService {
	
	private UserProperties properties;
	
	public UserService(){
		
	}
	
	public UserService(UserProperties properties){
		this.properties = properties;	
	}
	
	public void getMyUser(){
		System.out.println("hello my baseinfo is:"+properties.getName()+"age:"+properties.getAge()+"address:"+properties.getAddress());
	}
}

UserServiceAutoConfiguration(該start自動配置類)

package com.yuyou.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(UserProperties.class)
@ConditionalOnClass(UserService.class)
@ConditionalOnProperty(prefix = "spring.user", value = "enable", matchIfMissing = true)
public class UserServiceAutoConfiguration {

	@Autowired
	private UserProperties properties;
	
	@Bean
	@ConditionalOnMissingBean(UserService.class)
	public UserService userService(){
		UserService userService = new UserService(properties);
		return userService;
	}
}

spring.properties

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yuyou.user.UserServiceAutoConfiguration

完成之後 mvn clean install

3、引入自定義start項目

pom依賴

<dependency>
         <groupId>com.yuyou</groupId>
		 <artifactId>user-spring-boot-start</artifactId>
		 <version>0.0.1-SNAPSHOT</version>
    </dependency>

application.yml 配置自定義start的properties

spring:
  user:
    name: springbootyin
    age: 22
    address: jiangsu

IndexController.class

package com.yuyou.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.user.UserService;

@RestController
public class IndexController {
	
	@Autowired
	private UserService userService;
	
	@Value("${spring.user.name}")
	private String name;
	
	@GetMapping("/get")
	public String get(){
		userService.getMyUser();
		return "success"+name;
	}

}

啓動項目:訪問http://localhost:8080/get打印自定義的配置信息

五、總結

@ConfigurationProperties: 註解主要用來把properties配置文件轉化爲對應的XxxProperties來使用的,並不會把該類放入到IOC容器中,如果想放入到容器中可以在XxxProperties上使用@Component來標註,也可以使用@EnableConfigurationProperties(XxxProperties.class)統一配置到Application上來,這種方式可以在Application上來統一開啓指定的屬性,這樣也沒必要在每個XxxProperties上使用@Component

@EnableConfigurationProperties(XxxProperties.class) 註解的作用是@ConfigurationProperties註解生效。如果只配置@ConfigurationProperties註解,在IOC容器中是獲取不到properties配置文件轉化的bean的

 

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