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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

SpringBoot @Configuration •@Import •@Conditional•@ImportResoure基本使用

發(fā)布時(shí)間:2025/3/15 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot @Configuration •@Import •@Conditional•@ImportResoure基本使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、@Configuration

  • 基本使用
    Full模式與Lite模式
    ? 示例
    ? 最佳實(shí)戰(zhàn)
    ? 配置 類組件之間無(wú)依賴關(guān)系用Lite模式加速容器啟動(dòng)過(guò)程,減少判斷
    ? 配置 類組件之間有依賴關(guān)系,方法會(huì)被調(diào)用得到之前單實(shí)例組件,用Full模式
  • 例子
  • /*** 1、配置類里面使用@Bean標(biāo)注在方法上給容器注冊(cè)組件,默認(rèn)也是單實(shí)例的* 2、配置類本身也是組件* 3、proxyBeanMethods:代理bean的方法* Full(proxyBeanMethods = true)、【保證每個(gè)@Bean方法被調(diào)用多少次返回的組件都是單實(shí)例的】* Lite(proxyBeanMethods = false)【每個(gè)@Bean方法被調(diào)用多少次返回的組件都是新創(chuàng)建的】* 組件依賴必須使用Full模式默認(rèn)。其他默認(rèn)是否Lite模式****/ @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個(gè)配置類 == 配置文件 public class MyConfig {/*** Full:外部無(wú)論對(duì)配置類中的這個(gè)組件注冊(cè)方法調(diào)用多少次獲取的都是之前注冊(cè)容器中的單實(shí)例對(duì)象* @return*/@Bean //給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實(shí)例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測(cè)試代碼如下######################################## @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)代理對(duì)象調(diào)用方法。SpringBoot總會(huì)檢查這個(gè)組件是否在容器中有。//保持組件單實(shí)例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));} }

    二、@Import

  • 底層是個(gè)class數(shù)組
  • 例子
  • * 4@Import({User.class, DBHelper.class})* 給容器中自動(dòng)創(chuàng)建出這兩個(gè)類型的組件、默認(rèn)組件的名字就是全類名****/@Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個(gè)配置類 == 配置文件 public class MyConfig { }

    三、@Conditional

  • 條件裝配:滿足Conditional指定的條件,則進(jìn)行組件注入
  • 例子:
  • =====================測(cè)試條件裝配========================== @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個(gè)配置類 == 配置文件 //@ConditionalOnBean(name = "tom") @ConditionalOnMissingBean(name = "tom") public class MyConfig {/*** Full:外部無(wú)論對(duì)配置類中的這個(gè)組件注冊(cè)方法調(diào)用多少次獲取的都是之前注冊(cè)容器中的單實(shí)例對(duì)象* @return*/@Bean //給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實(shí)例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);}

    四、@ImportResoure(原生配置文件引入)

  • 例子
  • ======================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="zhangsan"></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 {}======================測(cè)試=================boolean haha = run.containsBean("haha");boolean hehe = run.containsBean("hehe");System.out.println("haha:"+haha);//trueSystem.out.println("hehe:"+hehe);//true

    總結(jié)

    以上是生活随笔為你收集整理的SpringBoot @Configuration •@Import •@Conditional•@ImportResoure基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。