【问题标题】:Wiring a bean and a value in a constructor在构造函数中连接 bean 和值
【发布时间】:2015-09-11 12:58:02
【问题描述】:

我正在使用 Spring。我有这门课:

@Service
public class FooService {

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

    @Autowired
    ResourceLoader ctx;
}

我真的很讨厌连接属性,并且更喜欢使用构造函数,但我想出的任何事情都会得到一个奇怪的“类 FooService 中的构造函数 FooService 不能应用于给定类型”。这种情况下可以使用施工布线吗?

【问题讨论】:

    标签: java spring autowired spring-ioc


    【解决方案1】:

    这应该可行:

    @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;
        }
    }
    

    【讨论】:

    • 我太笨了,现在我意识到问题是测试被破坏了,因为它使用了默认构造函数。奇怪的是该消息具有误导性!
    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2012-02-05
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    相关资源
    最近更新 更多