1.創建一個普通的maven項目
2.pom.xml引入依賴
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
3.創建主程序類
@SpringBootApplication
public class MainApplication {public static void main(String[] args) {SpringApplication.run(MainApplication.class, args);}
}
@SpringBootApplication:表明這是一個主程序類。
4.編寫業務邏輯
@RestController
public class HelloController {@RequestMapping("/hello")public String hello(){return "Hello, Spring Boot";}
}
@RestController:相當于@ResponseBody+@Controller。
5.運行主程序
6.簡化配置:創建application.properties,在該文件中修改配置信息,如修改端口號:server.port=8888
7.簡化部署:pom.xml中導入相關配置
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
maven中運行package,如果前面已經運行過了,可以先clean再package
在cmd中運行:
瀏覽器訪問:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version></parent>
它的父項目為:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.4.RELEASE</version></parent>
可以看到,這個也有一個父項目,再點進去
可以看到這是一個pring-boot-dependencies的配置文件,spring-boot-dependencies中幾乎聲明了所有常用的依賴的版本號。
1、查看spring-boot-dependencies里面規定當前依賴的版本 用的 key。
2、在當前項目里面重寫配置<properties><mysql.version>5.1.43</mysql.version></properties>
1、見到很多 spring-boot-starter-* : *就某種場景
2、只要引入starter,這個場景的所有常規需要的依賴我們都自動引入
3、SpringBoot所有支持的場景
https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
4、見到的 *-spring-boot-starter: 第三方為我們提供的簡化開發的場景啟動器。
5、所有場景啟動器最底層的依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.3.4.RELEASE</version><scope>compile</scope>
</dependency>
自動配好Tomcat
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.3.4.RELEASE</version><scope>compile</scope></dependency>
自動配好SpringMVC
自動配好Web常見功能,如:字符編碼問題
SpringBoot幫我們配置好了所有web開發的常見場景
默認的包結構
主程序所在包及其下面的所有子包里面的組件都會被默認掃描進來
無需以前的包掃描配置
想要改變掃描路徑,@SpringBootApplication(scanBasePackages=“com.deserts”)
或者@ComponentScan 指定掃描路徑
@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
各種配置擁有默認值
按需加載所有自動配置項
用法如下:
package com.deserts.boot.config;import com.deserts.boot.bean.Pet;
import com.deserts.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @ClassName MyConfig* @Description TODO* @Author deserts* @Date 2021/3/6 9:51*/
/*** 1.告訴spring boot這是一個配置類* 2.本身也是一個配置類* 3.proxyBeanMethods:是否為代理bean的方法,默認為true* 若為true,則容器中的組件都為單實例,有發生組件依賴時可用(Full);* 若為false,則每次調用不會再檢查容器中是否有該組件,直接創建實例(Lite).*/
@Configuration(proxyBeanMethods = true)
public class MyConfig {//注冊組件,id默認為方法名@Beanpublic User user01(){return new User("lisi", 20);}//注冊組件并指定id@Bean("pet01")public Pet cat(){return new Pet("cat");}
}
@Import:給容器中添加組件,組件名字默認為添加的組件的類的全類名。
條件裝配:滿足conditional指定的條件,則可進行裝配。
示例:當容器中有組件名為pet的組件時才會注冊該組件,也可放在整個配置類上
@ImportResource:導入某個資源文件的組件
如何使用Java讀取到properties文件中的內容,并且把它封裝到JavaBean中,以供隨時使用;
public class getProperties {public static void main(String[] args) throws FileNotFoundException, IOException {Properties pps = new Properties();pps.load(new FileInputStream("a.properties"));Enumeration enum1 = pps.propertyNames();//得到配置文件的名字while(enum1.hasMoreElements()) {String strKey = (String) enum1.nextElement();String strValue = pps.getProperty(strKey);System.out.println(strKey + "=" + strValue);//封裝到JavaBean。}}}
將配置文件的屬性綁定到該組件中,注意要使用@Component將該組件注冊到容器中
配置文件
打印結果:
@EnableConfigurationProperties(Car.class):
1.開啟Car配置綁定功能
2.把Car組件自動注冊到容器中
bean:
從主類的注解@SpringBootApplication開始,這是一個組合注解:
主要的注解:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {}
也是一個組合注解,主要是表明主類也是一個配置類。
定義組件掃描的規則,即指定掃描哪些;
這是一個組合注解:
主要是這兩個注解:
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {}
指定了默認的包規則。里面定義了這些注解:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {String[] basePackages() default {};Class<?>[] basePackageClasses() default {};
}
其中@Import導入了Registrar這個類:
這個類主要是批量地把某個包下的所有組件導入進來,AnnotationMetadata主要是獲取這個類標注在哪兒,debug:
我們可以看到,這個注解是標注在主類上的,因為這里的注解都是@SpringBootApplication合成注解中的一個小注解。然后在方法內執行的代碼:
AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
可以看到,獲取主類的名字后,這個方法獲取了主類的所在包名,并將包下所有的類都加入了容器中:
所以,@AutoConfigurationPackage的作用是將主類所在包下的所有類都加入容器中
點進加載的AutoConfigurationImportSelector類,這個類導入組件的主要方法:
public String[] selectImports(AnnotationMetadata annotationMetadata) {if (!this.isEnabled(annotationMetadata)) {return NO_IMPORTS;} else {AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());}}
可以看到,這個類利用getAutoConfigurationEntry()獲取要加載的字符串,再轉化成字符串傳出去;點進getAutoConfigurationEntry()并開始debug:
可以看到,這里獲取了130個類
為什么getAutoConfigurationEntry()能加載這么多配置類呢?在一步上打斷點debug,并點進去:
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());利用了工廠加載了這些東西
點進去可以看到工廠加載的來源是META-INF/spring.factories這個目錄:默認掃描當前系統
META-INF/spring.factories位置的文件;主要是這個包:
打開可以看到,所有配置的130個場景都在這個文件中了:
所以,Spring boot一啟動就把這130個組件全都加進來了。
上面可以看到Spring boot一啟動就加載了130個組件
按照條件裝配規則(@Conditional),最終會按需配置
每個配置類都有@Conditional相關注解,當達到某個條件按需加載,有些是只開啟類的部分方法,有的一整個類都不開啟:
@Bean@ConditionalOnBean(MultipartResolver.class) //容器中有這個類型組件@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) //容器中沒有這個名字 multipartResolver 的組件public MultipartResolver multipartResolver(MultipartResolver resolver) {//給@Bean標注的方法傳入了對象參數,這個參數的值就會從容器中找。//SpringMVC multipartResolver。防止有些用戶配置的文件上傳解析器不符合規范// Detect if the user has created a MultipartResolver but named it incorrectlyreturn resolver;}
給容器中加入了文件上傳解析器;
SpringBoot默認會在底層配好所有的組件。但是如果用戶自己配置了以用戶的優先
@Bean@ConditionalOnMissingBeanpublic CharacterEncodingFilter characterEncodingFilter() {}
總結:
SpringBoot先加載所有的自動配置類 xxxxxAutoConfiguration
每個自動配置類按照條件進行生效,默認都會綁定配置文件指定的值。xxxxProperties里面拿。xxxProperties和配置文件進行了綁定
生效的配置類就會給容器中裝配很多組件
只要容器中有這些組件,相當于這些功能就有了
定制化配置
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态