當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot中@PropertySource和@ImportResource以及@Bean
生活随笔
收集整理的這篇文章主要介紹了
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
(value = {"classpath:user.properties"}) (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
(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; public class Hello {public 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;(SpringRunner.class) public class HelloServiceApplicationTests {// @Autowired // private Hello hello; // // @Test // public void hello() { // hello.helloService(); // }private ApplicationContext ioc;public void hello() {ioc.getBean("helloService");} }復(fù)制代碼請注意
若配置類已經(jīng)加了@Bean注解,此時配置類中的方法名不能跟類名一樣,也就是上面的Hello類中不能定義hello()的方法,否則報錯
通過自定義工廠來實(shí)現(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 {public PropertySource<?> createPropertySource( 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; (factory = YamlPropertySourceFactory.class, value = "classpath:cat.yml") (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;}public 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;(SpringRunner.class) public class CatApplicationTests {private Cat cat;public void hei() {System.out.println(cat);}} 復(fù)制代碼控制臺輸出
Cat{age=3, weight=5.0, height=20.0} 復(fù)制代碼總結(jié)
以上是生活随笔為你收集整理的SpringBoot中@PropertySource和@ImportResource以及@Bean的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python】Docx解析
- 下一篇: js 获取时间戳的方法