日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot特点

發布時間:2025/6/15 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot特点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 依賴管理

? 父項目做依賴管理

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version></parent>

幾乎聲明了所有開發中常用的依賴的jar版本號, 自動版本仲裁機制.

??

? 開發導入starter場景啟動器??

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>

? ? ? 見到很多spring-boot-starter-*, *為某種場景.? ?只要引入starter,這個場景的所有常規需要的依賴我們都自動引入.

? ? ??

? 無需關注版本號,自動版本仲裁.

? 可以修改版本號

?

2.自動配置

  • 自動配好Tomcat
    • 引入Tomcat依賴。
    • 配置Tomcat
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.3.4.RELEASE</version><scope>compile</scope></dependency>
  • 自動配好SpringMVC
    • 引入SpringMVC全套組件
    • 自動配好SpringMVC常用組件(功能)
  • 自動配好Web常見功能,如:字符編碼問題
    • SpringBoot幫我們配置好了所有web開發的常見場景
  • 默認的包結構
    • 主程序所在包及其下面的所有子包里面的組件都會被默認掃描進來
    • 無需以前的包掃描配置
    • 想要改變掃描路徑,@SpringBootApplication(scanBasePackages="com.atguigu")
      • 或者@ComponentScan 指定掃描路徑
@SpringBootApplication 等同于 @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan("com.atguigu.boot")

?

  • 各種配置擁有默認值
    • 默認配置最終都是映射到某個類上,如:MultipartProperties
    • 配置文件的值最終會綁定每個類上,這個類會在容器中創建對象
  • 按需加載所有自動配置項
    • 非常多的starter
    • 引入了哪些場景這個場景的自動配置才會開啟
    • SpringBoot所有的自動配置功能都在 spring-boot-autoconfigure 包里面
    • ?
  • ......

2、容器功能

2.1、組件添加

1、@Configuration

  • 基本使用
  • Full模式與Lite模式
    • 示例
    • 最佳實戰
      • 配置 類組件之間無依賴關系用Lite模式加速容器啟動過程,減少判斷
      • 配置類組件之間有依賴關系,方法會被調用得到之前單實例組件,用Full模式
#############################Configuration使用示例###################################################### /*** 1、配置類里面使用@Bean標注在方法上給容器注冊組件,默認也是單實例的* 2、配置類本身也是組件* 3、proxyBeanMethods:代理bean的方法* Full(proxyBeanMethods = true)、【保證每個@Bean方法被調用多少次返回的組件都是單實例的】* Lite(proxyBeanMethods = false)【每個@Bean方法被調用多少次返回的組件都是新創建的】* 組件依賴必須使用Full模式默認。其他默認是否Lite模式****/ @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件 public class MyConfig {/*** Full:外部無論對配置類中的這個組件注冊方法調用多少次獲取的都是之前注冊容器中的單實例對象* @return*/@Bean //給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實例public User user01(){User zhangsan = new User("zhangsan", 18);//user組件依賴了Pet組件zhangsan.setPet(tomcatPet());return zhangsan;}@Bean("tom")public Pet tomcatPet(){return new Pet("tomcat");} }################################@Configuration測試代碼如下######################################## @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan("com.atguigu.boot") public class MainApplication {public static void main(String[] args) {//1、返回我們IOC容器ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);//2、查看容器里面的組件String[] names = run.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}//3、從容器中獲取組件Pet tom01 = run.getBean("tom", Pet.class);Pet tom02 = run.getBean("tom", Pet.class);System.out.println("組件:"+(tom01 == tom02));//4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892MyConfig bean = run.getBean(MyConfig.class);System.out.println(bean);//如果@Configuration(proxyBeanMethods = true)代理對象調用方法。SpringBoot總會檢查這個組件是否在容器中有。//保持組件單實例User user = bean.user01();User user1 = bean.user01();System.out.println(user == user1);User user01 = run.getBean("user01", User.class);Pet tom = run.getBean("tom", Pet.class);System.out.println("用戶的寵物:"+(user01.getPet() == tom));} }

?

@ComponentScan、@Import

* 4、@Import({User.class, DBHelper.class})* 給容器中自動創建出這兩個類型的組件、默認組件的名字就是全類名****/@Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件 public class MyConfig { }

?

@Conditional

條件裝配:滿足Conditional指定的條件,則進行組件注入

=====================測試條件裝配========================== @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件 //@ConditionalOnBean(name = "tom") @ConditionalOnMissingBean(name = "tom") public class MyConfig {/*** Full:外部無論對配置類中的這個組件注冊方法調用多少次獲取的都是之前注冊容器中的單實例對象* @return*/@Bean //給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實例public User user01(){User zhangsan = new User("zhangsan", 18);//user組件依賴了Pet組件zhangsan.setPet(tomcatPet());return zhangsan;}@Bean("tom22")public Pet tomcatPet(){return new Pet("tomcat");} }public static void main(String[] args) {//1、返回我們IOC容器ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);//2、查看容器里面的組件String[] names = run.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}boolean tom = run.containsBean("tom");System.out.println("容器中Tom組件:"+tom);boolean user01 = run.containsBean("user01");System.out.println("容器中user01組件:"+user01);boolean tom22 = run.containsBean("tom22");System.out.println("容器中tom22組件:"+tom22);}

2.2、原生配置文件引入

1、@ImportResource

======================beans.xml========================= <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><bean id="haha" class="com.atguigu.boot.bean.User"><property name="name" value="lisi"></property><property name="age" value="18"></property></bean><bean id="hehe" class="com.atguigu.boot.bean.Pet"><property name="name" value="tomcat"></property></bean> </beans> @ImportResource("classpath:beans.xml") public class MyConfig {}======================測試=================boolean haha = run.containsBean("haha");boolean hehe = run.containsBean("hehe");System.out.println("haha:"+haha);//trueSystem.out.println("hehe:"+hehe);//true

2.3、配置綁定

如何使用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。}}}

1.?@Component + @ConfigurationProperties

/*** 只有在容器中的組件,才會擁有SpringBoot提供的強大功能*/ @Component @ConfigurationProperties(prefix = "mycar") public class Car {private String brand;private Integer price;public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public Integer getPrice() {return price;}public void setPrice(Integer price) {this.price = price;}@Overridepublic String toString() {return "Car{" +"brand='" + brand + '\'' +", price=" + price +'}';} }

2、@EnableConfigurationProperties + @ConfigurationProperties

@EnableConfigurationProperties(Car.class) //1、開啟Car配置綁定功能 //2、把這個Car這個組件自動注冊到容器中 public class MyConfig { }

?

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的springboot特点的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。