- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
目前我有一个加载属性文件的 Spring xml 配置(Spring 4)。
上下文属性
my.app.service = myService
my.app.other = ${my.app.service}/sample
Spring xml配置
<bean id="contextProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="fileEncoding" value="UTF-8" />
<property name="locations">
<list>
<value>classpath:context.properties</value>
</list>
</property>
</bean>
<bean id="placeholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="contextProperties" />
<property name="nullValue" value="@null" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
Bean使用的属性
@Component
public class MyComponent {
@Value("${my.app.other}")
private String others;
}
这非常有效,others
值是 MyService/sample
,异常(exception)情况。但是,当我尝试用 JavaConfig 替换此配置时,我组件中的 @Value
不会以相同的方式工作。该值不是 myService/sample
而是 ${my.app.service}/sample
。
@Configuration
@PropertySource(name="contextProperties", ignoreResourceNotFound=true, value={"classpath:context.properties"})
public class PropertiesConfiguration {
@Bean
public static PropertyPlaceholderConfigurer placeholder() throws IOException {
PropertyPlaceholderConfigurer placeholder = new PropertyPlaceholderConfigurer();
placeholder.setNullValue("@null");
placeholder.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
return placeholder;
}
}
我是否遗漏了从 xml 到 Javaconfig 的转换?
ps :我还尝试实例化 PropertySourcesPlaceholderConfigurer
而不是 PropertyPlaceholderConfigurer
但没有成功。
最佳答案
更新以使用配置 PropertySourcesPlaceholderConfigurer
。只有 @PropertySource
注释是不够的:
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
return new PropertySourcesPlaceholderConfigurer();
}
@PropertySource
注解不会自动向 Spring 注册一个 PropertySourcesPlaceholderConfigurer
。因此我们需要显式配置 PropertySourcesPlaceholderConfigurer
下面的 JIRA ticket 提供了有关此设计背后的基本原理的更多信息:
https://jira.spring.io/browse/SPR-8539
更新:创建简单的 Spring Boot 应用程序以使用嵌套属性。它在上述配置下运行良好。
https://github.com/mgooty/property-configurer/tree/master/complete
关于java - Spring 占位符不解析 JavaConfig 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140794/
Java 在 JSR-330 中添加了官方依赖注入(inject)支持,例如 @Inject、@Named 等。这些可以与 Spring 或 Guice 等不同的框架一起使用。 Spring 还提供了
浏览 Spring Javaconfig 引用文档 http://docs.spring.io/spring-framework/docs/current/spring-framework-refer
什么是 JavaConfig 等价物? 尝试像这样声明它,但它仅充当单例 @Bean public CheckOutCounter checkOutCounter(){
找不到错误(( Spring MVC + Hibernate,JavaConfig WebAppConfig: package com.sprhib.init; import java.util.Pr
我在配置camel以使用java配置与mavencamel:run插件时遇到问题。 这是插件 xml: org.apache.camel
我编写了一个调度程序,它仅在处理 xml 文件时才能按预期工作。但我无法使用 Javaconfig 类运行它。以下是代码。 调度程序: public class DemoServiceBasicUsa
我们正在尝试将 AspectJ @Aspect 实现到我们现有的软件中,以便在进行服务调用后执行一些代码。 注意: 我们的服务接口(interface)和实现是@Autowired通过其余 Contr
有多个用@Configuration 注释的类,我想在顶级配置组件中决定在上下文中注册哪一个。 @Configuration public class FileSystemDataStoreConfi
这个问题在这里已经有了答案: Spring: @Component versus @Bean (16 个答案) 关闭 8 年前。 在 JavaConfig 中定义一个 bean 与仅仅注释一个类有何
这个问题在这里已经有了答案: How to import Java-config class into XML-config so that both contexts have beans? (3
Spring 大约需要 5 到 10 秒来自行配置,我将 XML 用于基础结构 bean,并使用带有注释的组件扫描来处理其他所有内容。 Spring JavaConfig 是否消除了组件扫描的需要以及
我有一个问题要解决:1)我们的项目使用 Spring JavaConfig 方法(所以没有 xml 文件)2)我需要创建自定义范围,xml中的示例如下所示:
我是 Spring 框架的新手,我在理解 @Required 时遇到了问题。注释与 Java 配置的应用程序相结合。 这是一个例子。 配置文件 @Configuration public class
我最近正在使用 spring data jpa 开发 spring web 应用程序 我在持久性配置方面遇到问题: @Configuration @EnableTransactionManagemen
我正在尝试设置一个在我的 Spring WebMVCConfig 中未找到的页面,它无法正常工作.. 这是我的配置: @Configuration @EnableWebMvc @PropertySou
我对 Spring 框架还很陌生,在理解 @Required 注释与 Java 配置的应用程序结合使用时遇到了问题。 这是一个示例。 配置文件 @Configuration public class
我们的 Spring 配置包含大约 1200 个 bean,我们使用 component-scan/@Autowired。如果我们将 ApplicationContext 导出为 Xml(并且仍然使用
目前我有一个加载属性文件的 Spring xml 配置(Spring 4)。 上下文属性 my.app.service = myService my.app.other = ${my.app.serv
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我正在将一些现有的 xml 配置转移到 Spring 的 Java 配置。在这个过程中,我遇到了一些被转移的Java代码,抛出一个checked Exception . @Bean public Po
我是一名优秀的程序员,十分优秀!