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

歡迎訪問 生活随笔!

生活随笔

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

javascript

[SpringBoot2]容器功能_底层注解配置绑定_@Configuration@Import@Conditional@ImportResource

發(fā)布時間:2023/12/4 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [SpringBoot2]容器功能_底层注解配置绑定_@Configuration@Import@Conditional@ImportResource 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

@Configuration&@Bean

  • 告訴SpringBoot這是一個配置類==配置文件
#############################Configuration使用示例###################################################### /*** 1、配置類里面使用@Bean標注在方法上給容器注冊組件,默認也是單實例的* 2、配置類本身也是組件* 3、proxyBeanMethods:代理bean的方法* Full(proxyBeanMethods = true)、【保證每個@Bean方法被調(diào)用多少次返回的組件都是單實例的】* Lite(proxyBeanMethods = false)【每個@Bean方法被調(diào)用多少次返回的組件都是新創(chuàng)建的】* 組件依賴必須使用Full模式默認。其他默認是否Lite模式****/ @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件 public class MyConfig {/*** Full:外部無論對配置類中的這個組件注冊方法調(diào)用多少次獲取的都是之前注冊容器中的單實例對象* @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));//true 組件默認是單實例的//4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892MyConfig bean = run.getBean(MyConfig.class);System.out.println(bean);//如果@Configuration(proxyBeanMethods = true)代理對象調(diào)用方法。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));} }
User user01 = run.getBean("user01", User.class);Pet tom = run.getBean("tom", Pet.class);System.out.println("用戶的寵物:"+(user01.getPet() == tom));

當proxyBeanMethods = true時,用戶中的寵物就是容器中的寵物,當
proxyBeanMethods = false時,用戶中的寵物不是容器中的寵物

@Import

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

測試:

@SpringBootApplication 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);}//獲取組件System.out.println("=============================================");String[] beanNamesForType = run.getBeanNamesForType(User.class);for (String s : beanNamesForType){System.out.println(s);}DBHelper bean1 = run.getBean(DBHelper.class);System.out.println(bean1);}

@Conditional

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

=====================測試條件裝配========================== @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件 //@ConditionalOnBean:在容器中有某個組件 //@ConditionalOnBean(name = "tom") @ConditionalOnMissingBean(name = "tom") public class MyConfig {/*** Full:外部無論對配置類中的這個組件注冊方法調(diào)用多少次獲取的都是之前注冊容器中的單實例對象* @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);}

舉例:

@ConditionalOnBean(name = "tom")@Bean("tom22")public Pet tomcatPet(){return new Pet("tomcat");}

表示當容器中有組件tom的時候,才會創(chuàng)建組件tom22到容器中

@ConditionalOnMissingBean(name = "tom") public class MyConfig { //........ }

表示當容器中沒有組件tom的時候,才會創(chuàng)建Myconfig中的那些組件到容器中

@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="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

配置綁定

如何使用Java讀取到properties文件中的內(nèi)容,并且把它封裝到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 + @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 +'}';} }

application.properties:

mycar.brand=BYD mycar.price=10000000

測試:

@RestController public class HelloController {@AutowiredCar car;@RequestMapping("/car")public Car car(){return car;} }

@EnableConfigurationProperties + @ConfigurationProperties

@Configuration//告訴SpringBoot這是一個配置類==配置文件 @EnableConfigurationProperties(Car.class) //1.開啟Car配置綁定功能 //2.把這個Car這個組件自動注冊到容器中 public class MyConfig { //....... } @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 +'}';} }
  • 只用一個@ConfigurationProperties是不行的!

總結(jié)

以上是生活随笔為你收集整理的[SpringBoot2]容器功能_底层注解配置绑定_@Configuration@Import@Conditional@ImportResource的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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