Spring boot @ConfigurationProperties
2017-01-21 19:27
369 查看
有的时候我们需要将properties封装成一个bean,然后访问属性的时候就可以直接从bean手里拿。
@ConfigurationProperties的作用就是这个。
拿beetl举个例子,beetl如果使用@value的方式进行获取就是:
BeetlConfig.java
而如果使用@ConfigurationProperties。
BeetlProperties .java:
BeetlConfig.java:
beetl.properties:
@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{ @Autowired private 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 boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案 cannot resolve method location
- springboot @value和@configurationproperties注解的区别
- 在Spring Boot中使用 @ConfigurationProperties 注解
- 【spring Boot】spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案
- spring boot 注入properties属性文件的值 @ConfigurationProperties
- 在Spring Boot中使用 @ConfigurationProperties 注解
- spring boot 使用@ConfigurationProperties
- Spring Boot1.4版本后@ConfigurationProperties注解舍弃location参数后的解决方式
- 在spring-boot中使用@ConfigurationProperties
- spring boot -Properties & configuration
- spring boot@ConfigurationProperties
- spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案
- spring boot 学习使用@ConfigurationProperties注解
- spring-boot配置(一):@Configuration,@ConfigurationProperties和application.yml
- 学习Spring boot 注解 @Value @Component @ConfigurationProperties
- spring boot 使用@ConfigurationPropertiesu将配置自动封装成实体类
- Spring Boot中@ConfigurationProperties的使用
- Spring Boot核心原理-自动配置 以及@ConfigurationProperties 注解
- spring boot 使用@ConfigurationProperties
- Spring Boot 使用@ConfigurationProperties注解获取配置文件中的值