Java – how to use yaml attribute and constructor injection in spring boot?
•
Java
I know it should be a piece of cake, but I just didn't get anywhere
In my spring boot application, in application In the YML file, I have an entry like this:
some: constructor: property: value
I have a spring service (it's fake, but it proves the problem):
package somepackage; @Service public class DummyService { public DummyService(@Value("${some.constructor.property}") String path) {} }
However, startup failed:
How can I convince spring that it should use a non empty constructor and that it should get constructor parameters from a yaml file? Note: I don't use any XML bean configuration files or anything, and I don't want to
Solution
Just put the @ Autowired annotation on the constructor
@Autowired public DummyService(@Value("${some.constructor.property}") String path) {}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码