springboot系列之自定义starter实例

1、idea建立一个empty project。

springboot系列之自定义starter实例_第1张图片

2、添加两个module,一个是自动配置(maven工程),一个是启动器(springboot工程),启动器依赖自动配置。

springboot系列之自定义starter实例_第2张图片

3、项目结构

springboot系列之自定义starter实例_第3张图片

springboot系列之自定义starter实例_第4张图片

springboot系列之自定义starter实例_第5张图片

4、内部代码

1)spring-boot-starter-autoconfigurer module:

package com.wms.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author:wangmingsai
 * @date:2018/8/28
 * @desc:
 */
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAuto {
    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}
package com.wms.starter;

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

/**
 * @author:wangmingsai
 * @date:2018/8/28
 * @desc:
 */
@ConfigurationProperties(prefix = "wms.hello")
public class HelloProperties {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.wms.starter;

/**
 * @author:wangmingsai
 * @date:2018/8/28
 * @desc:
 */
public class HelloService {

    HelloProperties helloProperties;

    public String hello() {
        return "hello" + helloProperties.getName();
    }

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}
spring.factories文件:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wms.starter.HelloAuto
pom文件:



	4.0.0

	com.wms
	spring-boot-starter-autoconfigurer
	0.0.1-SNAPSHOT
	jar

	spring-boot-starter-autoconfigurer
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.4.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		

	


2)springbootstarter:

启动器比较简单,只有pom文件,引入自动配置即可。



    4.0.0

    wms
    springboot-starter
    1.0-SNAPSHOT
    
        
            com.wms
            spring-boot-starter-autoconfigurer
            0.0.1-SNAPSHOT
        
    

 5、测试,新建springboot项目,pom文件中引入启动器

        
            wms
            springboot-starter
            1.0-SNAPSHOT
        

application.properties添加配置项 

wms.hello.name=chenzz

 测试controller:

package com.wms.teststarter;

import com.wms.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author:wangmingsai
 * @date:2018/8/29
 * @desc:
 */
@RestController
public class controller {

    @Autowired
    HelloService helloService;

    @GetMapping("hello")
    public String hello(){
        return helloService.hello();
    }
}

通过浏览器或者postman请求,获得响应:

springboot系列之自定义starter实例_第6张图片

你可能感兴趣的:(springboot)