spring boot 自定义 starter

本文用于 spring boot starter 开发。源码参见

https://gitee.com/emperors/spring-boot-integration/tree/master/hello-spring-boot-startergit

本工程依赖

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.lombok>1.16.18</version.lombok>
        <version.spring-boot>1.5.10.RELEASE</version.spring-boot>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${version.lombok}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${version.spring-boot}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

命名

注:关于artifactId 官方命名格式为: spring-boot-starter-{Nameing} 非官方建议命名格式:{Nameing}-spring-boot-starterweb

配置。spring

@Data
@ConfigurationProperties("starter")
public class HelloStarterProperties {
    /**
     * starter 描述
     */
    private String descr;
}

服务。app

/**
 * <p> 对外服务。</p>
 *
 * @author panqingcui <br>
 * @create 2018-06-28 10:29 <br>
 */
@Data
public class HelloStarterService {
    private String descr;
    public String helloStartar(){
        return "HELLO "+descr;
    }
}

自动配置类ide

/**
 * <p> 自动配置。</p>
 *
 * @author panqingcui <br>
 * @create 2018-06-28 10:32 <br>
 */
@Configuration //标识 当前类是配置类
@EnableConfigurationProperties(value = HelloStarterProperties.class) //启动配置文件,value用来指定咱们要启用的配置类,能够有多个,多个时咱们能够这么写value={xxProperties1.class,xxProperteis2.class....}
@ConditionalOnClass(HelloStarterService.class) //该注解的参数对应的类必须存在,不然不解析该注解修饰的配置类
@ConditionalOnProperty(prefix = "starter", value = "enable", matchIfMissing = false) //表示只有咱们的配置文件是否配置了以starter为前缀的资源项值,而且在该资源项值为enable,若是没有配置咱们默认设置为enable
public class HelloStarterServiceAutoConfiguration {
    @Autowired
    private HelloStarterProperties helloStarterProperties;

    @Bean
    @ConditionalOnMissingBean(HelloStarterService.class)//该注解表示,若是存在它修饰的类的bean,则不须要再建立这个bean
    public HelloStarterService helloService() {
        HelloStarterService helloStarterService = new HelloStarterService();
        helloStarterService.setDescr(helloStarterProperties.getDescr());
        return helloStarterService;
    }
}

自动加载配置spring-boot

resources 下新建META-INF/spring.factoriesui

添加内容:spa

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pqc.hello.starter.HelloStarterServiceAutoConfiguration

最简单的starter建立完成。添加依赖。在外部能够直接引用。code

<dependency>
			<groupId>com.pqc.test</groupId>
			<artifactId>hello-spring-boo-starter</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>

引用服务ci

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}
	@RestController
	class AaaController {

		@Autowired
		DiscoveryClient discoveryClient;
		@Autowired
		HelloStarterService helloStarterService;
	
		@GetMapping("/service-b")
		public String test(){
			return helloStarterService.helloStartar();
		}

		@GetMapping("/service-a")
		public String dc() {
			String services = "Services: " + discoveryClient.getServices();
			System.out.println(services);
			return services;
		}

	}

}

下面会研究。starter的注解方式,引用一个注解就能够搞定。

相关文章
相关标签/搜索