Spring @Value annotation always evaluating as null?

前端 未结 6 1185
别跟我提以往
别跟我提以往 2020-12-05 09:33

So, I have a simple properties file with the following entries:

my.value=123
another.value=hello world

This properties file is being loaded

6条回答
  •  我在风中等你
    2020-12-05 10:25

    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.

提交回复
热议问题