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

歡迎訪問 生活随笔!

生活随笔

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

javascript

SpringBoot中@PropertySource和@ImportResource以及@Bean

發(fā)布時間:2024/4/14 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot中@PropertySource和@ImportResource以及@Bean 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

@PropertySource

  • 加載指定的配置文件
  • 只能加載*.properties文件,不能加載yaml文件

新建一個user.properties

user.nickname=張三 user.age=19 user.sex=男 user.maps.weight=70 user.maps.height=170 user.address.addr=重慶市渝中區(qū) 復(fù)制代碼

UserBean

@Component @PropertySource(value = {"classpath:user.properties"}) @ConfigurationProperties(prefix = "user") public class User {private String nickname;private Integer age;private char sex;private Map<String,Integer> maps;private Address address;... } 復(fù)制代碼

@ImportResource

  • 導(dǎo)入Spring的配置文件,讓配置文件里面的內(nèi)容生效

SpringBoot中編寫的Spring配置文件是不能自動識別的

在主配置類上加入@ImportResource

@ImportResource(locations = {"classpath:beans.xml"}) 復(fù)制代碼

SpringBoot給容器添加組件的方式

1、配置類 == Spring配置文件 通過@Configuration聲明
2、使用@Bean給容器添加組件,組件id默認(rèn)為方法名

例子

package com.atgenee.demo.service;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class Hello {@Beanpublic Hello helloService() {System.out.println("添加組件");return new Hello();} } 復(fù)制代碼

測試

package com.atgenee.demo;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) @SpringBootTest public class HelloServiceApplicationTests {// @Autowired // private Hello hello; // // @Test // public void hello() { // hello.helloService(); // }@Autowiredprivate ApplicationContext ioc;@Testpublic void hello() {ioc.getBean("helloService");} }復(fù)制代碼

請注意

若配置類已經(jīng)加了@Bean注解,此時配置類中的方法名不能跟類名一樣,也就是上面的Hello類中不能定義hello()的方法,否則報錯


通過自定義工廠來實現(xiàn)自定義yaml文件加載

新建一個cat.yml文件

cat: age: 3 height: 20 weight: 5 復(fù)制代碼

工廠類

package com.atgenee.demo.factory;import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties;import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.PropertySourceFactory; import org.springframework.lang.Nullable;public class YamlPropertySourceFactory implements PropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {Properties propertiesFromYaml = loadYamlIntoProperties(resource);String sourceName = name != null ? name : resource.getResource().getFilename();return new PropertiesPropertySource(sourceName, propertiesFromYaml);}private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {try {YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();factory.setResources(resource.getResource());factory.afterPropertiesSet();return factory.getObject();} catch (IllegalStateException e) {// for ignoreResourceNotFoundThrowable cause = e.getCause();if (cause instanceof FileNotFoundException)throw (FileNotFoundException) e.getCause();throw e;}} } 復(fù)制代碼

新建配置類 Cat,配合@PropertySource 注解使用

package com.atgenee.demo.config;import com.atgenee.demo.factory.YamlPropertySourceFactory; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;@Component @PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:cat.yml") @ConfigurationProperties(prefix = "cat") public class Cat {private int age;private Double weight;private Double height;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Double getWeight() {return weight;}public void setWeight(Double weight) {this.weight = weight;}public Double getHeight() {return height;}public void setHeight(Double height) {this.height = height;}@Overridepublic String toString() {return "Cat{" +"age=" + age +", weight=" + weight +", height=" + height +'}';} } 復(fù)制代碼

Cat測試類

package com.atgenee.demo;import com.atgenee.demo.config.Cat; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) @SpringBootTest public class CatApplicationTests {@Autowiredprivate Cat cat;@Testpublic void hei() {System.out.println(cat);}} 復(fù)制代碼

控制臺輸出

Cat{age=3, weight=5.0, height=20.0} 復(fù)制代碼

總結(jié)

以上是生活随笔為你收集整理的SpringBoot中@PropertySource和@ImportResource以及@Bean的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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