目录
背景:
客户端 my-spring-boot-starter-client:
POM
HelloWorldProperties
HelloWorldAutoConfiguration
HelloWorld
meta-INF:
服务端:my-spring-boot-starter-consumer
POM:
application.properties
TestController
测试:
客户端打jar包
服务端引用坐标
启动服务端,请求 localhost:8080/print
背景:
一般公司中,都会有自己内部的jar包提供出来供开发人员引用,我们接触了SpringBoot发现里面有很多的Starter供我们引用,那我们能不能模仿者SpringBoot来自定义我们自己的Starter类型的jar呢
这边有两个工程,一个是自己提供给别人的jar包(我叫它为客户端), 一个是别人使用我们的jar(消费者)
客户端 my-spring-boot-starter-client:
用idea新建一个maven项目 my-spring-boot-starter-client,我的代码结构是下面这样的:
POM
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.5
com.my.boot.starter
my-spring-boot-starter-client
1.0-SNAPSHOT
8
8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-maven-plugin
true
HelloWorldProperties
package com.my.boot.starter.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "com.my.boot")
public class HelloWorldProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HelloWorldAutoConfiguration
package com.my.boot.starter.autoconfigure;
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;
@Configuration
@EnableConfigurationProperties({HelloWorldProperties.class})
public class HelloWorldAutoConfiguration {
private HelloWorldProperties properties;
@Autowired
public HelloWorldAutoConfiguration(HelloWorldProperties properties) {
this.properties = properties;
}
@Bean
public HelloWorld helloWorld() {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setProperties(properties);
return helloWorld;
}
}
HelloWorld
package com.my.boot.starter.autoconfigure;
public class HelloWorld {
private HelloWorldProperties properties;
public void setProperties(HelloWorldProperties properties) {
this.properties = properties;
}
public void print() {
System.out.println("name:" + this.properties.getName());
}
}
meta-INF:
package com.my.boot.starter.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "com.my.boot")
public class HelloWorldProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HelloWorldAutoConfiguration
package com.my.boot.starter.autoconfigure;
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;
@Configuration
@EnableConfigurationProperties({HelloWorldProperties.class})
public class HelloWorldAutoConfiguration {
private HelloWorldProperties properties;
@Autowired
public HelloWorldAutoConfiguration(HelloWorldProperties properties) {
this.properties = properties;
}
@Bean
public HelloWorld helloWorld() {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setProperties(properties);
return helloWorld;
}
}
HelloWorld
package com.my.boot.starter.autoconfigure;
public class HelloWorld {
private HelloWorldProperties properties;
public void setProperties(HelloWorldProperties properties) {
this.properties = properties;
}
public void print() {
System.out.println("name:" + this.properties.getName());
}
}
meta-INF:
package com.my.boot.starter.autoconfigure;
public class HelloWorld {
private HelloWorldProperties properties;
public void setProperties(HelloWorldProperties properties) {
this.properties = properties;
}
public void print() {
System.out.println("name:" + this.properties.getName());
}
}
meta-INF:
resources 下面新建 meta-INF 包,新增 spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.my.boot.starter.autoconfigure.HelloWorldAutoConfiguration
服务端:my-spring-boot-starter-consumer
用idea新建一个SpringBoot项目 my-spring-boot-starter-consumer,我的代码结构是下面这样的:
POM:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.5
com.my.boot.consumer
my-spring-boot-starter-consumer
0.0.1-SNAPSHOT
my-spring-boot-starter-consumer
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
com.my.boot.starter
my-spring-boot-starter-client
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
application.properties
com.my.boot.name=maple
TestController
package com.my.boot.consumer.controller;
import com.my.boot.starter.autoconfigure.HelloWorld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
private HelloWorld helloWorld;
@Autowired
public TestController(HelloWorld helloWorld) {
this.helloWorld = helloWorld;
}
@GetMapping("/print")
private void test() {
helloWorld.print();
}
}
测试:
客户端打jar包
com.my.boot.name=maple
TestController
package com.my.boot.consumer.controller;
import com.my.boot.starter.autoconfigure.HelloWorld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
private HelloWorld helloWorld;
@Autowired
public TestController(HelloWorld helloWorld) {
this.helloWorld = helloWorld;
}
@GetMapping("/print")
private void test() {
helloWorld.print();
}
}
测试:
客户端打jar包
客户端打jar包
my-spring-boot-starter-client
maven clean install
服务端引用坐标
com.my.boot.starter
my-spring-boot-starter-client
1.0-SNAPSHOT
启动服务端,请求 localhost:8080/print



