SpringBoot源码之自定义启动器

**

写在前面:

**

作为一名开发人员,相比较员spring和springMVC的开发来讲,使用SpringBoot微框架带给我们最大的感受就是 :终于可以不用再写那么多的配置文件了。仔细回想一下,在我们原来的spring+springMVC的开发模式中我们在xml文件中需要配置数据源、构建sqlSessionFactory工厂、创建dao实现类对象等等。到了SpringBoot中我们讲这些配置大部分都不在手动书写的。

是因为SpringBoot强大到用不到这些东西吗?

引用一句话就是:那有什么岁月静好,只不过是有人替你负重前行

对于一些重复性的配置,SpringBoot只不过是自己进行了封装而已,对于像配置数据源这些操作,我们也只是需要在application.yml中进行相关参数的书写,springboot将我们在yml中的参数通过 启动器 进行构建数据源的相关操作。

业务需求:

浏览器输入一个字符串,启动类为其加上我们在配置文件中书写的prefix和suffix。如下:

1.application.yml配置文件的参数

server.port=8989
server.context-path=/springboot

com.baizhi.prefix=I Like You---->
com.baizhi.suffix="Do you like me ?

2.发送请求,传入name

http://localhost:8989/springboot/test/test?name=bxr

3.预期结果,完成拼接业务

I Like You----> bxr Do you like me ?

自动义启动器开发流程:

1.创建开发的moudle,引入相关依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.baizhi</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>spring-boot-starter</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <!--继承springboot的父项目   仲裁中心:管理jar的版本号-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>
  <dependencies>
    <!--所有启动器的基本依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.1.2.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
    </pluginManagement>
  </build>
</project>

2.指定对应的xxxProperties,用于定义可配置的值

package com.baizhi.service;
import org.springframework.boot.context.properties.ConfigurationProperties;
//加入该注解指定配置的前缀
@ConfigurationProperties(prefix= "com.baizhi")
public class WelProperties {
    private String prefix;
    private String suffix;
    public String getPrefix() {
        return prefix;
    }
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
    public String getSuffix() {
        return suffix;
    }
    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

3.定义业务类,用于获取用户配置信息,并且实现我们的业务场景。

package com.baizhi.service;
import org.springframework.beans.factory.annotation.Autowired;
//定义业务类,用于获取用户的配置信息,并实现我们的应用 场景
public class WelService {
    @Autowired
    private WelProperties welProperties;
    public WelProperties getWelProperties() {
        return welProperties;
    }
    public void setWelProperties(WelProperties welProperties) {
        this.welProperties = welProperties;
    }
    /*实现我们的业务场景*/
    public String  wel(String name){
        return welProperties.getPrefix()+name+welProperties.getSuffix();
    }
}

4.定义自动配置类,并且需要将其我们的业务类实例化置于容器中

package com.baizhi.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* 定义自动装配类,并将我们我们的业务类实例化置于容器中
* */
@Configuration    //指定这个类是一个配置类
@ConditionalOnWebApplication     //web应用才生效
@EnableConfigurationProperties(value = WelProperties.class)  //让UserProperties生效加入到容器中
public class WelConf {
    @Autowired
    WelProperties welProperties;
    @Bean
    public WelService getWelService(){

        WelService welService = new WelService();
        welService.setWelProperties(welProperties);
        return welService;
    }
}

5.对配置类进行相关设置,从而让配置类生效

配置位置:resources---->META-INF----->spring.factories

# Auto Configure
#com.baizhi.service.UserConf就是我们的配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.baizhi.service.WelConf

6.将当前项目install到本地仓库(install 指的是 将当前项目打成jar包 并且导入到本地仓库),至此自定义启动器开发结束。

自定义启动器的测试流程:

1.创将新的moudle,引入相关依赖(web支持和自定义启动器)

    <dependencies>
        <!--引入web支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!--引入自定义的启动器-->
        <dependency>
            <groupId>com.baizhi</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

2.书写application.yml

server.port=8989
server.context-path=/springboot

com.baizhi.prefix=I Like You---->
com.baizhi.suffix="Do you like me ?

3.书写Controller

import com.baizhi.service.WelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class WelController {

    @Autowired
    private WelService welService;

    @RequestMapping("test")
    public String  wel(String name){

        return welService.wel(name);
    }
}

4.配置启动类,并通过插件启动测试

package com.baizhi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootPluginApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootPluginApplication.class, args);
    }

}

最终结果:

I Like You---->bxr"Do you like me ?

写在后面:

①、启动器只用来做依赖导入,即整个模块只需要导入其依赖的jar;

②、编写一个自动配置模块,以jar方式打包;

③、启动器依赖自动配置模块,用户只需要引入启动器(starter)就可以实现自动配置;

④、启动器模块命名规则:XXX(自定义启动器名)-spring-boot-starter(如:mybatis-spring-boot-starter;)

我的命名有瑕疵,不要学我~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值