當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
[SpringBoot2]容器功能_底层注解配置绑定_@Configuration@Import@Conditional@ImportResource
生活随笔
收集整理的這篇文章主要介紹了
[SpringBoot2]容器功能_底层注解配置绑定_@Configuration@Import@Conditional@ImportResource
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
@Configuration&@Bean
- 告訴SpringBoot這是一個配置類==配置文件
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
- 原生配置文件引入
配置綁定
如何使用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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [SpringBoot2]HelloWo
- 下一篇: gradle idea java ssm