图片怎么做网站背景,中国企业排行榜,aspnet东莞网站建设,深圳市建Value批注中的${...}占位符用于访问在PropertySource注册的属性。 这对于Spring应用程序中的Configuration bean非常有用#xff0c;但不仅如此。 为确保可行#xff0c; PropertySourcesPlaceholderConfigurer必须存在于所有需要占位符解析的应用程序上下文中。 在此博客文… Value批注中的${...}占位符用于访问在PropertySource注册的属性。 这对于Spring应用程序中的Configuration bean非常有用但不仅如此。 为确保可行 PropertySourcesPlaceholderConfigurer必须存在于所有需要占位符解析的应用程序上下文中。 在此博客文章中您将学习如何在Spring 4应用程序中配置占位符分辨率以及如何使用Value注释注入不同类型的对象包括JSR-310 Date-TimeJSR-354 MoneyCurrency或java.util.Optional 。 注册 在没有XML配置的Spring应用程序中必须在所有应用程序上下文中注册静态PropertySourcesPlaceholderConfigurer Bean。 要注册PropertySourcesPlaceholderConfigurer只需将相同类型的静态bean与要访问的属性源一起添加到配置中。 要导入多个属性源请使用PropertySources批注Java 8之前的版本或多个PropertySource批注Java 8。 Configuration
PropertySource(classpath:application.properties)
ComponentScan
class ApplicationConfig {Beanpublic static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}} 将属性源添加到配置程序的另一种方法是调用其setLocation方法 Configuration
ComponentScan
class ApplicationConfig {Beanpublic static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {PropertySourcesPlaceholderConfigurer c new PropertySourcesPlaceholderConfigurer();c.setLocation(new ClassPathResource(application.properties));return c;}}注入简单属性 现在您可以使用Value批注和占位符轻松访问属性 Value(${my.string.property})
private String stringProperty;
Value(${my.int.property})
private int intProperty;
Value(${my.boolean.property})
private boolean boolProperty; 这些属性在application.properties文件中定义 my.string.propertySome text
my.int.property42
my.boolean.propertytrue 当无法解析该属性时您将获得异常 java.lang.IllegalArgumentException: Could not resolve placeholder placeholder in string value ${placeholder}忽略无法解析的占位符 如果要自动忽略所有无法解析的占位符请设置配置程序的适当标志 PropertySourcesPlaceholderConfigurer c new PropertySourcesPlaceholderConfigurer();c.setIgnoreUnresolvablePlaceholders(true);默认值 可以使用以下语法提供默认值 Value(${my.string.property:Sample})
private String stringProperty; 还支持空的默认值这将导致一个空的stringProperty Value(${my.string.property:})
private String stringProperty;空值 如果希望将空值视为null 则可以设置配置器的nullValue属性如下所示 PropertySourcesPlaceholderConfigurer c new PropertySourcesPlaceholderConfigurer();
c.setNullValue(); 这可能会有所帮助尤其是在使用java.util.Optional 请参见下文。 注入非简单属性 要使用Value批注注入复杂的属性您需要在应用程序上下文中提供Spring的ConversionService 。 注册默认转换服务可以注入列表数组和其他可转换类型。 通常在Spring的servlet上下文中将注册ConversionService 例如通过EnableWebMvc 但是为了手动注册可以使用以下代码。 请注意bean的名称必须为conversionService Bean
public static ConversionService conversionService() {return new DefaultFormattingConversionService();
} DefaultFormattingConversionService支持所有常见的转换器和格式化程序包括JSR-354货币和货币JSR-310日期时间和/或Joda-Time的格式化程序。 注入列表/数组 要从属性插入列表或数组请使用逗号分隔的字符串定义属性的值 my.intList.property1,2,3,4,5
my.stringArray.property1,2,3,4,5 然后像这样注入它们 Value(${my.intList.property})
private ListInteger intList;Value(${my.stringArray.property})
private ListInteger stringArray;注入java.util.Optional Java 8的Optional提供了使用可选属性的绝佳机会。 使用Value注入Optional的Value是必须将属性值解析为null值并且要实现必须相应地设置配置程序的nullValue属性如先前所示。 Value(${my.optional.property:})
private OptionalString optional; 如果没有属性my.optional.property 则optional将包含Optional.empty 因此可以在代码中很好地使用它 if (optional.isPresent()) {// do something cool
}注入 注册的ConversionService包含JSR-310日期时间的格式化程序。 以下示例适用于当前语言环境中的LocalDate和LocalDateTime # format for en_US locale
my.localDate.property9/28/15
my.localDateTime.property9/28/15 10:05 PMValue(${my.localDate.property})
private LocalDate localDate;
Value(${my.localDateTime.property})
private LocalDateTime localDateTime;注入 将javax.money放在类路径上后就可以注入MonetaryAmount和CurrencyUnit my.monetaryAmount.propertyUSD 299
my.currencyUnit.propertyUSDValue(${my.monetaryAmount.property})
private MonetaryAmount monetaryAmount;
Value(${my.currencyUnit.property})
private CurrencyUnit currencyUnit;注入自定义类型 使用ConversionService 注册自定义转换器相对容易。 在下面的示例java.util.Pattern将从字符串值my.pattern.property[0-9].*创建java.util.Pattern对象。 为了实现这一点我们需要添加自定义转换 DefaultFormattingConversionService cs new DefaultFormattingConversionService();cs.addConverter(String.class, Pattern.class, (ConverterString, Pattern) Pattern::compile); 现在可以像下面这样注入属性 Value(${my.pattern.property})
private Pattern pattern;附加功能–在Thymeleaf视图中访问Spring的属性 如果您正在使用Thymeleaf并且想要访问在Spring的环境中注册的属性使用PropertySourcesPlaceholderConfigurer或仅使用PropertySource 则可以使用Thymeleaf的功能使用SpringEL的语法$ { myBean.doSomething}。 所有属性都可以通过Environment接口使用因此在Thymeleaf中访问它就像调用其getProperty方法一样简单 div th:fragmentfooter th:aligncenterspan th:text${environment.getProperty(app.version)}/span/div结束语 您可以在我的Spring的快速入门原型中找到Value注释和PropertySourcesPlaceholderConfigurer一些简单用法 https : //github.com/kolorobot/spring-mvc-quickstart-archetype 。 如果您使用的是Spring Boot则可能需要阅读有关类型安全配置属性的信息 http : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-类型安全配置属性 翻译自: https://www.javacodegeeks.com/2015/09/placeholders-support-in-value-annotations-in-spring.html