Apploader中出现play.api.Configuration冲突问题求助
我之前在Play Framework项目里碰到过一模一样的问题,这是因为你的AppComponents同时继承了BuiltInComponentsFromContext和ConfigurationComponents,而这两个类型里的configuration方法签名不匹配,导致编译器判定为冲突成员:
Error:(63, 7) class AppComponents inherits conflicting members: method configuration in class BuiltInComponentsFromContext of type => play.api.Configuration and method configuration in trait ConfigurationComponents of type ()play.api.Configuration (Note: this can be resolved by declaring an override in class AppComponents.); other members with override errors are: ...
具体来说,BuiltInComponentsFromContext中的configuration用的是Scala的传名参数(=> play.api.Configuration),而ConfigurationComponents里的是普通无参方法(()play.api.Configuration),虽然看起来都是获取配置,但编译器会把它们当成不同的方法签名。
解决方法很简单:显式Override冲突方法
在你的AppComponents类里,手动overrideconfiguration方法,明确指定要使用哪个父类的实现,或者自定义实现即可。
举个例子,假设你的原代码是这样的:
class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with ConfigurationComponents { // 你的组件初始化代码 }
修改后:
class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with ConfigurationComponents { // 显式override,这里选择使用BuiltInComponentsFromContext的配置实现 override def configuration: play.api.Configuration = super.configuration // 如果你想改用ConfigurationComponents的实现,就指定父类: // override def configuration: play.api.Configuration = super[ConfigurationComponents].configuration // 其他组件代码... }
关于其他override错误
错误信息里提到还有其他成员存在override错误,处理逻辑是一样的:找到那些冲突的方法,逐个添加override声明,并指定要继承的父类实现即可。
如果不确定哪个实现更合适,优先选择BuiltInComponentsFromContext里的实现——它是从应用上下文初始化的配置,通常是Play应用的核心配置来源。
内容的提问来源于stack exchange,提问作者Manu Chadha

