本文主要是介绍Spring boot @ConfigurationProperties,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
有的时候我们需要将properties封装成一个bean,然后访问属性的时候就可以直接从bean手里拿。
@ConfigurationProperties的作用就是这个。
拿beetl举个例子,beetl如果使用@value的方式进行获取就是:
BeetlConfig.java
import java.io.IOException;import org.beetl.core.resource.WebAppResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;@Configuration
public class BeetlConfigFactory{@Value(value = "${spring.beetl.prefix}")private String prefix;@Value(value = "${spring.beetl.suffix}")private String suffix;@Value(value = "${spring.beetl.content.type}")private String contentType;@Value(value = "${spring.beetl.root}")private String root;@Value(value = "${spring.beetl.order}")private int order;@Bean(initMethod = "init", name = "beetlConfig")public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());try {// WebAppResourceLoader 配置root路径是关键WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(patternResolver.getResource(root).getFile().getPath());beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);} catch (IOException e) {e.printStackTrace();}//读取配置文件信息return beetlGroupUtilConfiguration;}@Bean(name = "beetlViewResolver")public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();beetlSpringViewResolver.setPrefix(prefix);beetlSpringViewResolver.setSuffix(suffix);beetlSpringViewResolver.setContentType(contentType);beetlSpringViewResolver.setOrder(order);beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);return beetlSpringViewResolver;}
而如果使用@ConfigurationProperties。
BeetlProperties .java:
import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix="beetl",locations = "classpath:beetl.properties")
public class BeetlProperties {private String root;private String prefix;private String suffix;private int order;private String contentType;private String cofig;public String getRoot() {return root;}public void setRoot(String root) {this.root = root;}public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}public int getOrder() {return order;}public void setOrder(int order) {this.order = order;}public String getContentType() {return contentType;}public void setContentType(String contentType) {this.contentType = contentType;}public String getCofig() {return cofig;}public void setCofig(String cofig) {this.cofig = cofig;}}
BeetlConfig.java:
import java.io.IOException;import org.beetl.core.resource.WebAppResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;import com.shw.netdisk.config.BeetlProperties;@Configuration
public class BeetlConfigFactory{@Autowiredprivate BeetlProperties beetlProperties;@Bean(initMethod = "init", name = "beetlConfig")public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());try {// WebAppResourceLoader 配置root路径是关键WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(patternResolver.getResource(beetlProperties.getRoot()).getFile().getPath());beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);} catch (IOException e) {e.printStackTrace();}//读取配置文件信息return beetlGroupUtilConfiguration;}@Bean(name = "beetlViewResolver")public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();beetlSpringViewResolver.setPrefix(beetlProperties.getPrefix());beetlSpringViewResolver.setSuffix(beetlProperties.getSuffix());beetlSpringViewResolver.setContentType(beetlProperties.getContentType());beetlSpringViewResolver.setOrder(beetlProperties.getOrder());beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);return beetlSpringViewResolver;}}
beetl.properties:
beetl.root = classpath:/templates
beetl.prefix = /
beetl.suffix = .btl
beetl.order = 0
beetl.contentType = text/html;charset=UTF-8
这篇关于Spring boot @ConfigurationProperties的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!