与此问题类似:http://forum.springsource.org/showthread.php?111992-Loading-a-list-from-properties-file-using-Value-annotation(对此没有回复)
我想在.properties文件中有一个值列表,即:
my.list.of.strings=ABC,CDE,EFG
Run Code Online (Sandbox Code Playgroud)
并直接加载到我的班级,即:
@Value("${my.list.of.strings}")
private List<String> myList;
Run Code Online (Sandbox Code Playgroud)
据我所知,这样做的另一种方法是将它放在spring配置文件中,并将其作为bean引用加载(如果我错了,请纠正我),即
<bean name="list">
<list>
<value>ABC</value>
<value>CDE</value>
<value>EFG</value>
</list>
</bean>
Run Code Online (Sandbox Code Playgroud)
但有没有办法做到这一点?使用.properties文件?ps:如果可能的话,我想用任何自定义代码执行此操作.
之前我曾经在其他项目中工作,我只是重新做同样的事情,但由于某种原因,它不起作用.Spring @Value
不是从属性文件中读取,而是从字面上理解值
AppConfig.java
@Component
public class AppConfig
{
@Value("${key.value1}")
private String value;
public String getValue()
{
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml中:
<context:component-scan
base-package="com.test.config" />
<context:annotation-config />
<bean id="appConfigProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:appconfig.properties" />
</bean>
Run Code Online (Sandbox Code Playgroud)
appconfig.properties
key.value1=test value 1
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我有:
@Autowired
private AppConfig appConfig;
Run Code Online (Sandbox Code Playgroud)
应用程序启动很好,但是当我这样做
appConfig.getValue()
Run Code Online (Sandbox Code Playgroud)
它返回
${key.value1}
Run Code Online (Sandbox Code Playgroud)
它不会解析为属性文件中的值.
思考?
我正在开发一个Spring Boot应用程序.我需要在启动时解析XML文件(countries.xml).问题是我不明白把它放到哪里以便我可以访问它.我的文件夹结构是
ProjectDirectory/src/main/java
ProjectDirectory/src/main/resources/countries.xml
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是将它放在src/main/resources中,但是当我尝试创建File(countries.xml)时,我得到一个NPE,堆栈跟踪显示我的文件在ProjectDirectory中查找(所以src/main/resources /没有添加).我尝试创建File(resources/countries.xml),路径看起来像ProjectDirectory/resources/countries.xml(所以再没有添加src/main).
我尝试添加这个没有结果
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addResourceHandlers(registry);
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以手动添加src/main /,但我想了解为什么它不能正常工作.我也尝试过使用ResourceLoader的示例 - 同样没有结果.
任何人都可以建议问题是什么?
更新: 仅供将来参考 - 在构建项目后,我遇到了访问文件的问题,因此我将File更改为InputStream
InputStream is = new ClassPathResource("countries.xml").getInputStream();
Run Code Online (Sandbox Code Playgroud) 我有以下使用@Value注释的属性.我有一个使用默认分隔符':'定义的默认值
@Value("${prop.url:http://myurl.com}")
Run Code Online (Sandbox Code Playgroud)
有没有办法逃避':' http://myurl.com
或我必须在我的配置中定义一个不同的分隔符值.
我想使用@Value注释来注入Double属性,例如:
@Service
public class MyService {
@Value("${item.priceFactor}")
private Double priceFactor = 0.1;
// ...
Run Code Online (Sandbox Code Playgroud)
并使用Spring属性占位符(属性文件):
item.priceFactor=0.1
Run Code Online (Sandbox Code Playgroud)
我得到例外:
org.springframework.beans.TypeMismatchException:无法将类型'java.lang.String'的值转换为必需类型'java.lang.Double'; 嵌套异常是java.lang.NumberFormatException:对于输入字符串:"$ {item.priceFactor}"
有没有办法使用来自属性文件的Double值?
java spring spring-el spring-properties property-placeholder
spring.profiles.active和spring.config.activate.on-profile之间的确切区别是什么?
“WARN”,“msg”:“从位置“类路径资源[application.yaml]”导入的属性“spring.profiles”无效,应替换为“spring.config.activate.on-profile”[origin:class路径资源 [application.yaml]
枚举
public enum Property {
A,
AB,
ABC;
}
Run Code Online (Sandbox Code Playgroud)
领域
@Value("${custom.property}")
protected Property property;
Run Code Online (Sandbox Code Playgroud)
application.properties(小写)
custom.property=abc
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,我有一个错误:
无法将[java.lang.String]类型的值转换为必需的类型[com.xxx.Property]:找不到匹配的编辑器或转换策略.
鉴于(大写):
custom.property=ABC
Run Code Online (Sandbox Code Playgroud)
工作良好.
有没有办法绑定值不区分大小写的值?像ABC,Abc,AbC,abc任何模式都应该有效.
注:我看到这个问题- 春季3.0 MVC结合枚举大小写敏感的,但在我的情况我有超过10枚举/值(并期望有更多)班,并实施10层不同的自定义属性的粘合剂将是痛苦的,我需要一些通用的解决方案.
我正在使用Spring Boot应用程序.在某些@Component
类@Value
字段中加载,而不是在其他类上null
.
似乎在创建/ @Value
之后加载了.@Bean
@Component
我需要从我的属性文件中加载一些值@Bean
.
你有什么建议吗?
有没有办法我们可以使用Spring启动应用程序中的application.properties文件中的相对路径查找文件资源,如下所述
spring.datasource.url=jdbc:hsqldb:file:${project.basedir}/db/init
Run Code Online (Sandbox Code Playgroud) 我有application-dev.yml
包含内容的属性文件:
spring.profiles: dev
config.some.value:
- ELEMENT1
- ELEMENT2
Run Code Online (Sandbox Code Playgroud)
另一个application-staging.yml
内容:
spring.profiles: staging
config.some.value:
- ELEMENT1
- ELEMENT2
- ELEMENT3
Run Code Online (Sandbox Code Playgroud)
所以我基本上不知道列表的大小。当我application.yml
像这样在 main 中引用这个列表时:
some.value: ${config.some.value}
Run Code Online (Sandbox Code Playgroud)
我明白了Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'value'
。如何正确引用?