Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Autowired Environment is always null

Tags:

spring

I am having a simple RestController:

@RestController
@PropertySource("classpath:application.properties")
public class Word2VecRestController {

    private final static Logger LOGGER = Logger.getLogger(Word2VecRestController.class);

    // @Resource is not working as well
    @Autowired
    Environment env;

    // This is working for some reason 
    // but it's null inside the constructor
    @Value("${test}") 
    String test;

    public Word2VecRestController() {

        LOGGER.info(env.getProperty("test"));

        System.out.println("");

    }

    @GetMapping("/dl4j/getWordVector")
    public ResponseEntity<List<Double[]>> getWordVector(String word) {
        return null;
    }

}

The problem is, that env is always null. I've seen somewhere that I could try to use @Resource instead of @Autowired but that didn't help.

application.properties:

test=helloworld

I've tried to use

@Value("${test}")
String test;

but the problem here is that these are null during construction of the object where I need it.

like image 538
Stefan Falk Avatar asked Jan 24 '17 20:01

Stefan Falk


People also ask

Why Autowired is getting null?

If the application starts and your field appears to be null it is generally due to one of the following issues: Using @Autowired on a static field. Omitted @Autowired on a field. Instance of bean not visible to Spring.

Why is @autowired not working?

When @Autowired doesn't work. There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.

Why is my Spring bean null?

Spring dependency injection only works with Spring-managed objects or Beans. If the object in which a Bean is getting injected is not a spring managed object, you will get null @Autowired fields. To fix this, make sure only the framework create and manage the related dependencies.

Why do we use @autowired annotation?

The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.


1 Answers

Field injection is done by Spring after the constructor is called. This is why Environment is null in the Word2VecRestController constructor. You can try constructor injection if you need it in the constructor:

@RestController
@PropertySource("classpath:application.properties")
public class Word2VecRestController {

    private final static Logger LOGGER = Logger.getLogger(Word2VecRestController.class);

    @Autowired
    public Word2VecRestController(Environment env, @Value("${test}") String test) {

        LOGGER.info(env.getProperty("test"));

        System.out.println("");

    }

    @GetMapping("/dl4j/getWordVector")
    public ResponseEntity<List<Double[]>> getWordVector(String word) {
        return null;
    }

}

PS: if you use Spring Boot, you do not need the @PropertySource("classpath:application.properties"), this is automatically done for you.

like image 147
Christophe L Avatar answered Nov 16 '22 02:11

Christophe L