Spring @Autowired and @Value on property not working

大兔子大兔子 提交于 2020-01-13 09:33:08

问题


I would like to use @Value on a property but I always get 0(on int).
But on a constructor parameter it works.

Example:

@Component
public class FtpServer {

    @Value("${ftp.port}")
    private int port;

    public FtpServer(@Value("${ftp.port}") int port)
    {
        System.out.println(port); // 21, loaded from the application.properties.
        System.out.println(this.port); // 0???
    }
}

The object is spring managed, else the constructor parameter wouldn't work.

Does anyone know what causes this weird behavior?


回答1:


Field injection is done after objects are constructed since obviously the container cannot set a property of something which doesn't exist. The field will be always unset in the constructor.

If you want to print the injected value (or do some real initialization :)), you can use a method annotated with @PostConstruct, which will be executed after the injection process.

@Component
public class FtpServer {

    @Value("${ftp.port}")
    private int port;

    @PostConstruct
    public void init() {
        System.out.println(this.port);
    }

}



回答2:


I think the problem is caused because Spring's order of execution:

  • Firstly, Spring calls the constructor to create an instance, something like:

    FtpServer ftpServer=new FtpServer(<value>);

  • after that, by reflection, the attribute is filled:

    code equivalent to ftpServer.setPort(<value>)

So during the constructor execution the attribute is still 0 because that's the default value of an int.




回答3:


This is a member injection:

@Value("${ftp.port}")
private int port;

Which spring does after instantiating the bean from its constructor. So at the time spring is instantiating the bean from the class, spring has not injected the value, thats why you are getting the default int value 0.

Make sure to call the variable after the constructor have been called by spring, in case you want to stick with member injection.



来源:https://stackoverflow.com/questions/46035971/spring-autowired-and-value-on-property-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!