java - 带有接口默认方法的 Spring 属性注入
问题描述
有没有办法在接口默认方法中返回注入的属性?我希望任何实现的类MyInterface
都能获得一个getBar()
返回属性值的方法,而不必在每个类中重新实现它。我本质上是在尝试做类似的事情
public interface MyInterface {
String getFoo();
default String getBar() {
return @Value("${my.prop.bar}"); //if only
}
}
通常我只会做一个私有字段并将@Value放在上面,但是接口不能有非常量字段。我认为我可以从接口更改为抽象类并使其工作,但我宁愿作为接口离开,让实现者有机会扩展他们想要的任何东西。
解决方案
推荐阅读
- pathname - 路径名的开头是否需要“/”?
- swift - SwiftUI - 子视图中的绑定未更新
- azure-devops - 无法使用 Azure DevOps API 添加工作项注释
- javascript - 如何为 HTML 可拖动元素的幽灵图像赋予高度?
- python - 在定义之前使用函数
- git - 错误 Git SVN 克隆:使用未初始化的值 $sha1
- python - 我如何只从 Python 中的超类继承一些变量
- sqlite - 如何在运行 Micronaut 功能测试时备份和恢复 sqlite db
- javascript - 如何最好地更新 Vuejs 模板中 v-for 循环下的 DOM 的一部分
- reactjs - 在 react 中使用 chartjs 显示值或百分比