J Person
J Person

Reputation: 1229

Spring value injection unable to get value from properties

I've got a class like this:

@Component
public class FaultsConfiguration {
    private int interval;

    @Autowired
    public FaultsConfiguration(@Value("${faults.interval}") int interval) {
        this.interval = interval;
    }
}

In application.properties I have this:

faults.interval=130

I think the bean should get the value 130 injected into it. Instead when the bean is initialized I get this error:

java.lang.IllegalArgumentException: Could not resolve placeholder 'faults.interval' in value "${faults.interval}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:258)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at com.telensa.puma.etl.core.CoreApplication.main(CoreApplication.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

I've tried every variation on this I can find. I've tried alternative syntax like this:

@Value("${classpath:faults.interval}"

and this:

 @Value("#{faults.interval}"

and this:

"#{new Integer.parseInt('${faults.interval}')}"

and it all fails in a similar way.

I have managed to get the property injected by using @ConfigurationProperties instead without any change to application.properties, like this (using Lombok):

@Service
@ConfigurationProperties(prefix="faults")
@Data
@NoArgsConstructor
public class FaultsConfiguration {
    private int interval;
}

So clearly Spring is happy reading properties from application.properties.

So why doesn't it work when I try and use value injection?

Upvotes: 3

Views: 3183

Answers (3)

Lova Chittumuri
Lova Chittumuri

Reputation: 3303

we can get the value of "faults.interval" as mentioned Below once properties are configured properly.

@Component
public class FaultsConfiguration {

@Value("${faults.interval}") 
private int interval;

private int Testinterval;

    @Autowired
    public FaultsConfiguration(int interval) {
        this.Testinterval = interval;
    }
}

Upvotes: -1

Thomas Mwania
Thomas Mwania

Reputation: 405

The variable faults.interval declared in you application.properties is autowired as a bean. In that case, to use the variable you only need to declare as a variable in the class you want to use it. In your case, you should try declaring it as follows:

@Component
public class FaultsConfiguration {

   @Value("${faults.interval}")
   private int interval;
}

Upvotes: -1

elmehdi
elmehdi

Reputation: 479

In order to let spring recognize and resolve the properties file you need to add the bean of type PropertySourcesPlaceholderConfigurer to you config class (FaultsConfiguration )

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

The spring use this bean to resolve properties and place them on you expression, then you could use

(@Value("${faults.interval}") int interval

To inject your property

Don't forget to make spring aware of your properties file , add to FaultsConfiguration this :

@PropertySource("classpath:yourPropertiesFile.properties")

Upvotes: 4

Related Questions