springboot原理

第一步 创建SpringApplication

在这里插入图片描述

启动流程:
1.创建SpringApplication对象
initialize(sources) 调用了这个方法创建对象
再进入了这个流程
(保存主配置类)
在这里插入图片描述
这样执行完了以后就创建了SpringApplication对象
2.运行run方法

第二步 启动应用


public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();

plicationContext context = null;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();
//获取SpringApplicationRunListeners;从类路径下META‐INF/spring.factories
SpringApplicationRunListeners listeners = getRunListeners(args);
//回调所有的获取SpringApplicationRunListener.starting()方法
listeners.starting();
try {
//封装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
//准备环境
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
//创建环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准
备完成
Banner printedBanner = printBanner(environment);
//创建ApplicationContext;决定创建web的ioc还是普通的ioc
context = createApplicationContext();
analyzers = new FailureAnalyzers(context);
//准备上下文环境;将environment保存到ioc中;而且applyInitializers();
//applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法
//回调所有的SpringApplicationRunListener的contextPrepared();
//
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
//prepareContext运行完成以后回调所有的SpringApplicationRunListener的contextLoaded();
//s刷新容器;ioc容器初始化(如果是web应用还会创建嵌入式的Tomcat);Spring注解版
//扫描,创建,加载所有组件的地方;(配置类,组件,自动配置)
refreshContext(context);
//从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
//ApplicationRunner先回调,CommandLineRunner再回调
afterRefresh(context, applicationArguments);
//所有的SpringApplicationRunListener回调finished方法
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
//整个SpringBoot应用启动完成以后返回启动的ioc容器;
return context;
}
catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex);
throw new IllegalStateException(ex);
}
}

3.事件监听机制

在这里插入图片描述

下面来手动实现这几个类
在这里插入图片描述

public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        System.out.println("HelloApplicationContextInitializer........initialize..."+configurableApplicationContext);
    }
}




@Component
public class HelloApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("HelloApplicationRunner...run");
    }
}



@Component
public class HelloCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("HelloCommandLineRunner.....run..."+ Arrays.asList(args));
    }
}





public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {

    public HelloSpringApplicationRunListener(SpringApplication application,String[] args){

    }


    @Override
    public void starting() {
        System.out.println("HelloSpringApplicationRunListener.....starting");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        Object o =environment.getSystemProperties().get("os.name");  //获取一些环境信息 比如操作系统名字啊 之类的
        System.out.println("HelloSpringApplicationRunListener......environmentPrepared...."+o);
    }

    @Override  //ioc容器准备好了
    public void contextPrepared(ConfigurableApplicationContext context) {  //ioc容器也传了进来 想用的话就可以用的
        System.out.println("HelloSpringApplicationRunListener.....contextPrepared");
    }

    @Override  //表示环境已经加载完成
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("HelloSpringApplicationRunListener...contextLoaded");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {

    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("HelloSpringApplicationRunListener...running");
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {

    }
}

执行结果

按照输出顺序   (执行顺序)
HelloSpringApplicationRunListener.....starting
HelloSpringApplicationRunListener......environmentPrepared....Windows 10
HelloApplicationContextInitializer........initialize...org.springframework.context.annotation.AnnotationConfigApplicationContext@658c5a19, started on Thu Jan 01 08:00:00 CST 1970
HelloSpringApplicationRunListener.....contextPrepared
HelloSpringApplicationRunListener...contextLoaded
HelloApplicationRunner...run
HelloCommandLineRunner.....run...[]
HelloSpringApplicationRunListener...running

SpringBoot 自定义starters(场景启动器) 作用_____简化springboot

starter:
1.这个场景需要使用到的依赖是什么?
2.如何编写自动配置

@Configuration   //指定这一个类是配置类
@ConditionalOnxxx   //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter   //指定自动配置类的顺序
@Bean   //给容器中添加组件
@ConfigurationProperties  结合相关的xxxProperties类来绑定相关的配置
@EnableConfigurationProperties  //让xxxProperties生效 加入到容器中    

自动配置类要能加载

将启动就加载的自动配置类  配置在META-INF/spring.factories

3.模式
在这里插入图片描述
启动器只用来做依赖导入;
专门写一个自动配置模块;
启动器依赖自动配置;别人只需要引入启动器(starter);

在这里插入图片描述
创建两个model 一个是starter 一个是自动配置类
然后starter要引入 config类的坐标

<!--        引入自动配置模块-->
        <dependency>
            <groupId>com.cuitstarter</groupId>
            <artifactId>dlf-spring-boot-starter-autoconfigurer</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

然后

在这里插入图片描述
config包

自动配置类    到时候别的项目引用它的时候 直接在yml里面可以注入相应的信息
@ConfigurationProperties(prefix = "atguigu.hello")
public class HelloProperties {
    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;
    }
}





@Configuration
@ConditionalOnWebApplication   //表示如果是web应用才会生效
@EnableConfigurationProperties({HelloProperties.class})
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloProperties helloProperties;
    @Bean
    public helloService helloService(){
        helloService helloService = new helloService();
        helloService.setHelloProperties(helloProperties);
            return helloService;
    }
}

Service

public class helloService {
    HelloProperties helloProperties;
    public String sayHelloAtdlf(String name){
        return helloProperties.getPrefix()+"-"+name+"-"+helloProperties.getSuffix();
    }

    public HelloProperties getHelloProperties(){
        return helloProperties;
    }

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

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  config.HelloServiceAutoConfiguration

其他项目引入

pom文件引入  starter的坐标
<dependency>
    <groupId>com.cuit.starter</groupId>
    <artifactId>dlf-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>






controller


@RestController
public class test {
    @Autowired
    helloService helloService;
    @RequestMapping("/test")
    public String he(){
        return helloService.sayHelloAtdlf("dlf");
    }

}

就能够正常使用了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值