Spring @Value Annotation Always Evaluating as Null

Spring @Value annotation always evaluating as null?

If instances of Config are being instantiated manually via new, then Spring isn't getting involved, and so the annotations will be ignored.

If you can't change your code to make Spring instantiate the bean (maybe using a prototype-scoped bean), then the other option is to use Spring's load-time classloader weaving functionality (see docs). This is some low-level AOP which allows you to instantiate objects as you normally would, but Spring will pass them through the application context to get them wired up, configured, initialized, etc.

It doesn't work on all platforms, though, so read the above documentation link to see if it'll work for you.

Spring Boot: @Value returns always null

You can't use @Value on static variables. You'll have to either mark it as non static or have a look here at a way to inject values into static variables:

https://www.mkyong.com/spring/spring-inject-a-value-into-static-variables/

EDIT: Just in case the link breaks in the future. You can do this by making a non static setter for your static variable:

@Component
public class MyComponent {

private static String directory;

@Value("${filesystem.directory}")
public void setDirectory(String value) {
this.directory = value;
}
}

The class needs to be a Spring bean though or else it won't be instantiated and the setter will be not be accessible by Spring.

Spring - @Value returns null

System.out.println(new myClass().getInjectMeThis());

Spring will only parse @Value annotations on beans it knows. The code you use creates an instance of the class outside the scope of Spring and as such Spring will do nothing with it.

Assuming you have setup your application context correctly a @Value cannot be null as that will stop the correct startup of your application.

Your XML file contains a <context:component-scan /> assuming myClass is part of that package the easiest solution is to add @Component to myClass and then retrieve the instance from the context instead of creating a new instance.

java - Spring @Value annotation returns null

Spring can not inject @Value to a static field directly.

you can either add inject the value through an annotated setter like this:

private static String keystoreType;

@Value("${client.keystore.type}")
public void setKeystoreType(String keystoreType) {
SendMessageController.keystoreType = keystoreType;
}

Or Change :

    @Value("${client.keystore.type}")
private static String keystoreType;

to :

@Value("${client.keystore.type}")
private String keystoreType;

@Value in Springboot returns null

Okay, i solved it with the top answer of this Question. I put the variables and @Values in the signature of the constructor and not as class variables.

Spring MVC why my property is null when invoke an @Value property

Couple of things you need to check & fix in your code.

  1. From your given snippet you are missing the @Component & @Configuration & @PropertySource annotation

    @Component 
    @Configuration
    @PropertySource("classpath:config.properties")
    public class BgpService{}
  2. You are trying to assign/use the value of serviceName field on static level (class) hence you need to define the field serviceName as static.

    @Value("${serviceName}")
    private static String serviceName;
  3. And add a post construct method in order to assign value to another field. Currently your initialisation is invalid.

    @PostConstruct
    public void init(){
    fullName = serviceName+"/rest"
    }


Related Topics



Leave a reply



Submit