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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

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

一、@Configuration

  • 基本使用
    Full模式與Lite模式
    ? 示例
    ? 最佳實戰
    ? 配置 類組件之間無依賴關系用Lite模式加速容器啟動過程,減少判斷
    ? 配置 類組件之間有依賴關系,方法會被調用得到之前單實例組件,用Full模式
  • 例子
  • /*** 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));} }

    二、@Import

  • 底層是個class數組
  • 例子
  • * 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);}

    四、@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 {}======================測試=================boolean haha = run.containsBean("haha");boolean hehe = run.containsBean("hehe");System.out.println("haha:"+haha);//trueSystem.out.println("hehe:"+hehe);//true

    總結

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

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