前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springboot自动配置的运行原理(自定义一个自动配置)

Springboot自动配置的运行原理(自定义一个自动配置)

作者头像
凡人飞
发布2020-09-18 18:38:20
3860
发布2020-09-18 18:38:20
举报
文章被收录于专栏:指缝阳光指缝阳光

1.新建一个空的maven项目,在pom中导入

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

<scope>test</scope>

</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-autoconfigure</artifactId>

<version>2.0.4.RELEASE</version>

</dependency>

2.新建一个类型安全的取值类,可以自动获取application.properties或application.yml的值

@ConfigurationProperties(prefix = "hello")

public class HelloServicesProperties {

private static final String MSG = "world";

//这儿是类型安全的属性填充值,在application中通过hello.msg=来设置,若不设置,默认为hello.msg=world

private String msg = MSG;

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

}

3.新建一个判断依据类,根据此类的存在与否来创建这个类的Bean,这个类可以是第三方类库的类。在调用该包使用时,即根据此类来调用相应的方法,此类中会包含有application中传递的值。

public class HelloService {

private String msg;

public String sayHello(){

return "Hello " + msg;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

}

4.新建自动配置类,会根据条件判断是否自动创建HelloService类。根据HelloServiceProperties提供的参数,并通过@ConditionalOnClass判断HelloService个类在类路径中是否存在,且当容器中没有这个Bean的情况下自动配置这个Bean。

@Configuration

@EnableConfigurationProperties(HelloServicesProperties.class)

@ConditionalOnClass(HelloService.class)

@ConditionalOnProperty(prefix = "hello",value = "enabled",matchIfMissing = true)

public class HelloServiceAutoConfiguration {

@Autowired

private HelloServicesProperties helloServicesProperties;

@Bean

@ConditionalOnMissingBean(HelloService.class)

public HelloService helloService(){

HelloService helloService = new HelloService();

helloService.setMsg(helloServicesProperties.getMsg());

return helloService;

}

}

5.新建一个Model来使用上面自定义的自动配置:创建Module方式很简单,选中上面的Project右键单击,New一个Module,这个Module是一个SpringBoot项目,创建成功之后选择新建的Module按F4打开Module设置,然后选择右边的加号添加依赖,如下:

6.在新建Model的pom中引入自定义的自动配置

<dependency>

<groupId>com.wisely</groupId>

<artifactId>spring-boot-starter-hello</artifactId>

<version>1.0-SNAPSHOT</version>

</dependency>

7.在Model的入口类中编写一个控制来测试我们自定义的自动配置

@RestController

@SpringBootApplication

public class TestAutoApplication {

@Autowired

private HelloService helloService;

@RequestMapping("/")

public String index(){

return helloService.sayHello()+" 测试自动配置";

}

public static void main(String[] args) {

SpringApplication.run(TestAutoApplication.class, args);

}

}

8.此时启动后可以在浏览器输入:localhost:8080来访问,默认没有写配置文件时,显示:

9.可以通过application.properties中修改配置来配置,如:hello.msg=xjf,此时访问显示为:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/08/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档