springboot自定义starter

1.新建一个springboot项目 

1.1 写一个接口,定义一个say方法

public class HelloService {
private HelloProperties helloProperties;

public HelloService(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}

public HelloProperties getHelloProperties() {
return helloProperties;
}

public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}

public String say(){
return helloProperties.getName();
}
}
1.2 定义HelloProperties ,
@ConfigurationProperties(prefix = "com.zy")
public class HelloProperties {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
1.3 主配置类 启动proeperties
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloConfiguration {
@Autowired
private HelloProperties helloProperties;


@Bean
public HelloService helloService(){
return new HelloService(helloProperties);
}
}
1.4 在resources下新建METAfactories文件,在里面写上
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zy.HelloConfiguration

1.5 打包该项目 jar包

2.新建另一个springboot项目

2.1 导入上面项目的maven
<dependency>
<groupId>org.example</groupId>
<artifactId>zy-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

2.2 application.properties中添加
com.zy.name=zhouyang

2.3新建一个HelloController

@RestController
public class HelloController {
@Resource
private HelloService helloService;
@RequestMapping("hello")
public String hello(){
return helloService.say();
}
}
2.4 测试






posted @ 2020-08-27 10:36  z街角的风铃y  阅读(200)  评论(0)    收藏  举报