chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線(xiàn)課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

SpringBoot的核心注解2

jf_78858299 ? 來(lái)源:Java知音 ? 作者:小毛毛 ? 2023-04-07 14:34 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

他會(huì)調(diào)用 ((AbstractApplicationContext) applicationContext).refresh();方法,我們點(diǎn)進(jìn)來(lái)看

public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      // Prepare this context for refreshing.
      prepareRefresh();

      // Tell the subclass to refresh the internal bean factory.
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // Prepare the bean factory for use in this context.
      prepareBeanFactory(beanFactory);

      try {
         // Allows post-processing of the bean factory in context subclasses.
         postProcessBeanFactory(beanFactory);

         // Invoke factory processors registered as beans in the context.
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
         registerBeanPostProcessors(beanFactory);

         // Initialize message source for this context.
         initMessageSource();

         // Initialize event multicaster for this context.
         initApplicationEventMulticaster();

         // Initialize other special beans in specific context subclasses.
         onRefresh();

         // Check for listener beans and register them.
         registerListeners();

         // Instantiate all remaining (non-lazy-init) singletons.
         finishBeanFactoryInitialization(beanFactory);

         // Last step: publish corresponding event.
         finishRefresh();
      }

      catch (BeansException ex) {
         if (logger.isWarnEnabled()) {
            logger.warn("Exception encountered during context initialization - " +
                  "cancelling refresh attempt: " + ex);
         }

         // Destroy already created singletons to avoid dangling resources.
         destroyBeans();

         // Reset 'active' flag.
         cancelRefresh(ex);

         // Propagate exception to caller.
         throw ex;
      }

      finally {
         // Reset common introspection caches in Spring's core, since we
         // might not ever need metadata for singleton beans anymore...
         resetCommonCaches();
      }
   }
}

這點(diǎn)代碼似曾相識(shí)啊 沒(méi)錯(cuò),就是一個(gè)spring的bean的加載過(guò)程我在,解析springIOC加載過(guò)程的時(shí)候介紹過(guò)這里面的方法,如果你看過(guò)Spring源碼的話(huà) ,應(yīng)該知道這些方法都是做什么的?,F(xiàn)在我們不關(guān)心其他的,我們來(lái)看一個(gè)方法叫做 onRefresh();方法

protected void onRefresh() throws BeansException {
   // For subclasses: do nothing by default.
}

他在這里并沒(méi)有實(shí)現(xiàn),但是我們找他的其他實(shí)現(xiàn),我們來(lái)找

圖片

我們既然要找Tomcat那就肯定跟web有關(guān),我們可以看到有個(gè)ServletWebServerApplicationContext

@Override
protected void onRefresh() {
   super.onRefresh();
   try {
      createWebServer();
   }
   catch (Throwable ex) {
      throw new ApplicationContextException("Unable to start web server", ex);
   }
}

我們可以看到有一個(gè)createWebServer();方法他是創(chuàng)建web容器的,而Tomcat不就是web容器,那他是怎么創(chuàng)建的呢,我們繼續(xù)看

private void createWebServer() {
   WebServer webServer = this.webServer;
   ServletContext servletContext = getServletContext();
   if (webServer == null && servletContext == null) {
      ServletWebServerFactory factory = getWebServerFactory();
      this.webServer = factory.getWebServer(getSelfInitializer());
   }
   else if (servletContext != null) {
      try {
         getSelfInitializer().onStartup(servletContext);
      }
      catch (ServletException ex) {
         throw new ApplicationContextException("Cannot initialize servlet context",
               ex);
      }
   }
   initPropertySources();
}

factory.getWebServer(getSelfInitializer());他是通過(guò)工廠(chǎng)的方式創(chuàng)建的

public interface ServletWebServerFactory {

   WebServer getWebServer(ServletContextInitializer... initializers);

}

可以看到 它是一個(gè)接口,為什么會(huì)是接口。因?yàn)槲覀儾恢故荰omcat一種web容器。

圖片

我們看到還有Jetty,那我們來(lái)看TomcatServletWebServerFactory

@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
   Tomcat tomcat = new Tomcat();
   File baseDir = (this.baseDirectory != null) ? this.baseDirectory
         : createTempDir("tomcat");
   tomcat.setBaseDir(baseDir.getAbsolutePath());
   Connector connector = new Connector(this.protocol);
   tomcat.getService().addConnector(connector);
   customizeConnector(connector);
   tomcat.setConnector(connector);
   tomcat.getHost().setAutoDeploy(false);
   configureEngine(tomcat.getEngine());
   for (Connector additionalConnector : this.additionalTomcatConnectors) {
      tomcat.getService().addConnector(additionalConnector);
   }
   prepareContext(tomcat.getHost(), initializers);
   return getTomcatWebServer(tomcat);
}

那這塊代碼,就是我們要尋找的內(nèi)置Tomcat,在這個(gè)過(guò)程當(dāng)中,我們可以看到創(chuàng)建Tomcat的一個(gè)流程。因?yàn)閞un方法里面加載的東西很多,所以今天就淺談到這里。如果不明白的話(huà), 我們?cè)谟昧硪环N方式來(lái)理解下,

大家要應(yīng)該都知道stater舉點(diǎn)例子

<dependency>
    <groupId>org.springframework.boot<span class="hljs-name"groupId>
    <artifactId>spring-boot-starter-data-redis<span class="hljs-name"artifactId>
<span class="hljs-name"dependency>
<dependency>
    <groupId>org.springframework.boot<span class="hljs-name"groupId>
    <artifactId>spring-boot-starter-freemarker<span class="hljs-name"artifactId>
<span class="hljs-name"dependency>

所以我們不防不定義一個(gè)stater來(lái)理解下,我們做一個(gè)需求,就是定制化不同的人跟大家說(shuō)你們好,我們來(lái)看

<parent>
    <groupId>org.springframework.boot<span class="hljs-name"groupId>
    <artifactId>spring-boot-starter-parent<span class="hljs-name"artifactId>
    <version>2.1.4.RELEASE<span class="hljs-name"version>
    <relativePath/>
<span class="hljs-name"parent>
<groupId>com.zgw<span class="hljs-name"groupId>
<artifactId>gw-spring-boot-srater<span class="hljs-name"artifactId>
<version>1.0-SNAPSHOT<span class="hljs-name"version>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot<span class="hljs-name"groupId>
        <artifactId>spring-boot-autoconfigure<span class="hljs-name"artifactId>
    <span class="hljs-name"dependency>
<span class="hljs-name"dependencies>

我們先來(lái)看maven配置寫(xiě)入版本號(hào),如果自定義一個(gè)stater的話(huà)必須依賴(lài)spring-boot-autoconfigure這個(gè)包,我們先看下項(xiàng)目目錄

圖片

public class GwServiceImpl  implements GwService{
    @Autowired
    GwProperties properties;

    @Override
    public void Hello()
    {
        String name=properties.getName();
        System.out.println(name+"說(shuō):你們好啊");
    }
}

我們做的就是通過(guò)配置文件來(lái)定制name這個(gè)是具體實(shí)現(xiàn)

@Component
@ConfigurationProperties(prefix = "spring.gwname")
public class GwProperties {

    String name="zgw";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

這個(gè)類(lèi)可以通過(guò)@ConfigurationProperties讀取配置文件

@Configuration
@ConditionalOnClass(GwService.class)  //掃描類(lèi)
@EnableConfigurationProperties(GwProperties.class) //讓配置類(lèi)生效
public class GwAutoConfiguration {

    /**
    * 功能描述 托管給spring
    * @author zgw
    * @return
    */
    @Bean
    @ConditionalOnMissingBean
    public GwService gwService()
    {
        return new GwServiceImpl();
    }
}

這個(gè)為配置類(lèi),為什么這么寫(xiě)因?yàn)?,spring-boot的stater都是這么寫(xiě)的,我們可以參照他仿寫(xiě)stater,以達(dá)到自動(dòng)配置的目的,然后我們?cè)谕ㄟ^(guò)spring.factories也來(lái)進(jìn)行配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gw.GwAutoConfiguration

然后這樣一個(gè)簡(jiǎn)單的stater就完成了,然后可以進(jìn)行maven的打包,在其他項(xiàng)目引入就可以使用,在這里列出代碼地址

聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • XML
    XML
    +關(guān)注

    關(guān)注

    0

    文章

    188

    瀏覽量

    34367
  • spring
    +關(guān)注

    關(guān)注

    0

    文章

    341

    瀏覽量

    15763
  • Boot
    +關(guān)注

    關(guān)注

    0

    文章

    154

    瀏覽量

    37466
  • 注解
    +關(guān)注

    關(guān)注

    0

    文章

    18

    瀏覽量

    2825
  • SpringBoot
    +關(guān)注

    關(guān)注

    0

    文章

    177

    瀏覽量

    625
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    SpringBoot配置Mybatis的2個(gè)錯(cuò)誤和修正

    SpringBoot】配置Mybatis錯(cuò)誤
    發(fā)表于 04-19 10:31

    Spring Boot的注解原理是什么

    首先,先看SpringBoot的主配置類(lèi): @SpringBootApplicationpublic class StartEurekaApplication { public static
    的頭像 發(fā)表于 08-27 09:24 ?2557次閱讀

    Spring Boot中常見(jiàn)的各類(lèi)型注解的使用方式

    大家好,我是程序汪,企業(yè)開(kāi)發(fā)項(xiàng)目SpringBoot已經(jīng)是必備框架了,其中注解是開(kāi)發(fā)中的小工具(誰(shuí)處可見(jiàn)哦),用好了開(kāi)發(fā)效率大大提升,當(dāng)然用錯(cuò)了也會(huì)引入缺陷。
    的頭像 發(fā)表于 06-20 16:38 ?2341次閱讀

    Spring Boot常用注解與使用方式

    企業(yè)開(kāi)發(fā)項(xiàng)目SpringBoot已經(jīng)是必備框架了,其中注解是開(kāi)發(fā)中的小工具(誰(shuí)處可見(jiàn)哦),用好了開(kāi)發(fā)效率大大提升,當(dāng)然用錯(cuò)了也會(huì)引入缺陷。
    的頭像 發(fā)表于 07-08 10:57 ?1977次閱讀

    求一種SpringBoot定時(shí)任務(wù)動(dòng)態(tài)管理通用解決方案

    SpringBoot的定時(shí)任務(wù)的加強(qiáng)工具,實(shí)現(xiàn)對(duì)SpringBoot原生的定時(shí)任務(wù)進(jìn)行動(dòng)態(tài)管理,完全兼容原生@Scheduled注解,無(wú)需對(duì)原本的定時(shí)任務(wù)進(jìn)行修改
    的頭像 發(fā)表于 02-03 09:49 ?1384次閱讀

    Java注解及其底層原理解析2

    什么是注解? 當(dāng)我們開(kāi)發(fā)SpringBoot項(xiàng)目,我們只需對(duì)啟動(dòng)類(lèi)加上`@SpringBootApplication`,就能自動(dòng)裝配,不需要編寫(xiě)冗余的xml配置。當(dāng)我們?yōu)轫?xiàng)目添加lombok
    的頭像 發(fā)表于 02-09 14:18 ?924次閱讀
    Java<b class='flag-5'>注解</b>及其底層原理解析<b class='flag-5'>2</b>

    什么是 SpringBoot

    本文從為什么要有 `SpringBoot`,以及 `SpringBoot` 到底方便在哪里開(kāi)始入手,逐步分析了 `SpringBoot` 自動(dòng)裝配的原理,最后手寫(xiě)了一個(gè)簡(jiǎn)單的 `start` 組件,通過(guò)實(shí)戰(zhàn)來(lái)體會(huì)了 `
    的頭像 發(fā)表于 04-07 11:28 ?2070次閱讀
    什么是 <b class='flag-5'>SpringBoot</b>?

    SpringBoot常用注解及使用方法1

    基于 SpringBoot 平臺(tái)開(kāi)發(fā)的項(xiàng)目數(shù)不勝數(shù),與常規(guī)的基于`Spring`開(kāi)發(fā)的項(xiàng)目最大的不同之處,SpringBoot 里面提供了大量的注解用于快速開(kāi)發(fā),而且非常簡(jiǎn)單,基本可以做到開(kāi)箱即用! 那
    的頭像 發(fā)表于 04-07 11:51 ?1092次閱讀

    SpringBoot常用注解及使用方法2

    基于 SpringBoot 平臺(tái)開(kāi)發(fā)的項(xiàng)目數(shù)不勝數(shù),與常規(guī)的基于Spring開(kāi)發(fā)的項(xiàng)目最大的不同之處,SpringBoot 里面提供了大量的注解用于快速開(kāi)發(fā),而且非常簡(jiǎn)單,基本可以做到開(kāi)箱即用!
    的頭像 發(fā)表于 04-07 11:52 ?1144次閱讀

    Springboot常用注解合集

    前幾章,在系統(tǒng)啟動(dòng)類(lèi)里面,都加入了此啟動(dòng)注解,此注解是個(gè)組合注解,包括了`@SpringBootConfiguration`、`@EnableAutoConfiguration`和`@ComponentScan`
    的頭像 發(fā)表于 04-07 14:27 ?1175次閱讀
    <b class='flag-5'>Springboot</b>常用<b class='flag-5'>注解</b>合集

    SpringBoot常用注解及原理

    SpringBootConfiguration繼承自@Configuration,二者功能也一致,標(biāo)注當(dāng)前類(lèi)是配置類(lèi), 并會(huì)將當(dāng)前類(lèi)內(nèi)聲明的一個(gè)或多個(gè)以@Bean注解標(biāo)記的方法的實(shí)例納入到spring容器中,并且實(shí)例名就是方法名。
    的頭像 發(fā)表于 04-07 14:30 ?889次閱讀

    SpringBoot核心注解1

    今天跟大家來(lái)探討下SpringBoot核心注解@SpringBootApplication以及run方法,理解下springBoot為什么不需要XML,達(dá)到零配置
    的頭像 發(fā)表于 04-07 14:34 ?1148次閱讀
    <b class='flag-5'>SpringBoot</b>的<b class='flag-5'>核心</b><b class='flag-5'>注解</b>1

    springboot核心注解

    Spring Boot 是基于 Spring 框架的開(kāi)源框架,它可以幫助開(kāi)發(fā)者快速構(gòu)建、部署和運(yùn)行獨(dú)立的、生產(chǎn)級(jí)的 Spring 應(yīng)用程序。Spring Boot 提供了一系列核心注解,這些注解可以
    的頭像 發(fā)表于 11-23 09:23 ?1048次閱讀

    一個(gè)注解搞定SpringBoot接口防刷

    技術(shù)要點(diǎn):springboot的基本知識(shí),redis基本操作,
    的頭像 發(fā)表于 11-28 10:46 ?732次閱讀

    SpringBoot核心注解由幾個(gè)注解組成

    簡(jiǎn)化應(yīng)用程序開(kāi)發(fā)的注解,其中核心注解包括 @SpringBootApplication、@RestController、@RequestMapping、@Autowired、@ComponentScan
    的頭像 發(fā)表于 12-03 15:09 ?1264次閱讀