gpt4 book ai didi

java - 在构造函数中连接一个 bean 和一个值

转载 作者:行者123 更新时间:2023-11-30 10:57:40 24 4
gpt4 key购买 nike

我正在使用 Spring。我有这个类:

@Service
public class FooService {

@Value("${xml.file.path}")
String xmlFilePath;

@Autowired
ResourceLoader ctx;
}

我真的很讨厌连接属性,更愿意使用构造函数,但我想到的任何东西都是一个奇怪的“类 FooService 中的构造函数 FooService 不能应用于给定类型”。在这种情况下是否可以使用建筑布线?

最佳答案

这应该有效:

@Service
public class FooService {
private String xmlFilePath;
private ResourceLoader ctx;

@Autowired
public FooService(@Value("${xml.file.path}") String xmlFilePath, ResourceLoader ctx) {
super();
this.xmlFilePath = xmlFilePath;
this.ctx = ctx;
}
}

关于java - 在构造函数中连接一个 bean 和一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32524092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com