Spring @Value("${}") often null
Asked Answered
E

4

19

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?

Edra answered 20/2, 2015 at 18:45 Comment(3)
Are the "other classes" registered as spring beans? What is the difference between the ones that receive the values and the ones that don't?Imidazole
Sure, they are or Bean or ComponentEdra
Are you checking for the values in the constructor?Imidazole
I
53

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) {
        this.prop = prop;
        //use it here
    }
}

Another option is to move the constructor logic in method annotated with @PostConstruct it will be executed after the bean is created and all it's dependencies and property values are resolved.

Imidazole answered 20/2, 2015 at 20:17 Comment(5)
And who will call this constructor? I'm trying but this 'custom' constructor is not called.Edra
You probably also have the default and the custom constructor. Remove the one without parameters.Imidazole
I can't because I've an exception removing default constructor: No default constructor found; nested exception is java.lang.NoSuchMethodException: it.modem.ModemManager.<init>()Edra
Then try with the @PostConstruct methodImidazole
I yet done it. It works but I'm not in the constructor. So is not possibile have values inside the constructor?Edra
C
1

It can happen when you are resolving it into a static variable. I had observed this sometime back and resolved it just by removing static. As people always say, exercise caution while using static.

Chomp answered 18/3, 2019 at 16:42 Comment(0)
L
0

Have you tried:

@Component
@PropertySource("file:/your/file/path")
public class MyBean {

  private @Value("${property}") String property;
  ...
}
Longanimity answered 20/2, 2015 at 19:24 Comment(3)
Using @PropertySource("classpath:application.properties") I've an Exception during startup: No qualifying bean of type [org.springframework.core.io.Resource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}. ThanksEdra
Did you annotated the class with @Component ?Longanimity
Yes. @Evgeni has centered the problem. I can't user Value field inside the constructor as it is now.Edra
C
-2

Another possible reason is that the '@Value' lines are below the lines that need these properties/values.

I spent a lot of time debugging this problem, and found out that the order of lines matters!

Counterpoint answered 11/4, 2016 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.