spring-properties

Spring @Value TypeMismatchException:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'

时光总嘲笑我的痴心妄想 提交于 2019-12-02 23:36:42
I want to use the @Value annotation to inject a Double property such as: @Service public class MyService { @Value("${item.priceFactor}") private Double priceFactor = 0.1; // ... and using Spring property placeholder (Properties files): item.priceFactor=0.1 I get Exception: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'; nested exception is java.lang.NumberFormatException: For input string: "${item.priceFactor}" Is there a way to use a Double value coming from a properties file? Try changing the following

Spring @Value(“${}”) often null

假如想象 提交于 2019-11-30 14:48:50
问题 I'm using Spring Boot application. In some @Component class @Value fields are loaded, instead on other classes they are always null . Seems that @Value (s) are loaded after my @Bean / @Component are created. I need to load some values from a properties file in my @Bean . Have you some suggestion? 回答1: The properties(and all of the bean dependencies) are injected after the bean is constructed(the execution of the constructor). You can use constructor injection if you need them there.

Spring @Value(“${}”) often null

佐手、 提交于 2019-11-30 12:23:58
I'm using Spring Boot application. In some @Component class @Value fields are loaded, instead on other classes they are always null . Seems that @Value (s) are loaded after my @Bean / @Component are created. I need to load some values from a properties file in my @Bean . Have you some suggestion? The properties(and all of the bean dependencies) are injected after the bean is constructed(the execution of the constructor). You can use constructor injection if you need them there. @Component public class SomeBean { private String prop; @Autowired public SomeBean(@Value("${some.prop}") String prop

Spring @Value escape colon(:) in default value

∥☆過路亽.° 提交于 2019-11-29 02:50:34
I have the following property annotated with @Value. I have a default value defined using the default separator of ':" @Value("${prop.url:http://myurl.com}") Is there a way to escape the ':' in http://myurl.com or do I have to define a different separator value in my configuration. Chris Thompson Update: For spring 4.2 and higher, no single quotes are needed. Spring will see the first colon as special, and use all the rest as a single string value. For spring 4.2 and higher, @Value("${prop.url:http://myurl.com}") For the previous versions, I believe single quotes will do the trick: @Value("$

Spring Boot access static resources missing scr/main/resources

淺唱寂寞╮ 提交于 2019-11-27 18:40:45
I am working on a Spring Boot application. I need to parse an XML file (countries.xml) on start. The problem is that I do not understand where to put it so that I could access it. My folders structure is ProjectDirectory/src/main/java ProjectDirectory/src/main/resources/countries.xml My first idea was to put it in src/main/resources, but when I try to create File (countries.xml) I get a NPE and the stacktrace shows that my file is looked in the ProjectDirectory (so src/main/resources/ is not added). I tried to create File (resources/countries.xml) and the path would look like ProjectDirectory

Spring @Value escape colon(:) in default value

a 夏天 提交于 2019-11-27 17:07:20
问题 I have the following property annotated with @Value. I have a default value defined using the default separator of ':" @Value("${prop.url:http://myurl.com}") Is there a way to escape the ':' in http://myurl.com or do I have to define a different separator value in my configuration. 回答1: Update: For spring 4.2 and higher, no single quotes are needed. Spring will see the first colon as special, and use all the rest as a single string value. For spring 4.2 and higher, @Value("${prop.url:http:/

Spring Boot access static resources missing scr/main/resources

☆樱花仙子☆ 提交于 2019-11-26 19:34:09
问题 I am working on a Spring Boot application. I need to parse an XML file (countries.xml) on start. The problem is that I do not understand where to put it so that I could access it. My folders structure is ProjectDirectory/src/main/java ProjectDirectory/src/main/resources/countries.xml My first idea was to put it in src/main/resources, but when I try to create File (countries.xml) I get a NPE and the stacktrace shows that my file is looked in the ProjectDirectory (so src/main/resources/ is not

Reading a List from properties file and load with spring annotation @Value

柔情痞子 提交于 2019-11-26 00:27:14
问题 I want to have a list of values in a .properties file, ie: my.list.of.strings=ABC,CDE,EFG And to load it in my class directly, ie: @Value(\"${my.list.of.strings}\") private List<String> myList; As I understand, an alternative of doing this is to have it in the spring config file, and load it as a bean reference (correct me if I\'m wrong), ie <bean name=\"list\"> <list> <value>ABC</value> <value>CDE</value> <value>EFG</value> </list> </bean> But is there any way of doing this? using a