我正在尝试使用macwire DI在Playframework 2.4上构建应用程序,我遇到来自Play的httpFilters的问题!这是我正在尝试做的一个例子
df_qtr <- list( fmli1, fmli2, ...)
DI模块
class ExampleFilter extends Filter {
def apply(nextFilter: RequestHeader => Future[Result])
(requestHeader: RequestHeader): Future[Result] = {
nextFilter(requestHeader).map { result =>
result.withHeaders("Example" -> "test")
}
}
}
class Filters(ex:ExampleFilter) extends HttpFilters {
val filters = Seq(ex)
}
Loader class
trait Module extends EhCacheComponents with BuiltInComponents {
lazy val exampleFilter = wire[ExampleFilter]
lazy val filters = wire[Filters]
application.conf的一部分是
class Loader extends ApplicationLoader {
def load(context: Context) = {
new MyComponents(context).application
}
}
class MyComponents(context: Context) extends BuiltInComponentsFromContext(context) with Module {
lazy val router: Router = wire[Routes] withPrefix "/"
}
和ExampleFilter不起作用。我收到没有“示例”标题的回复
答案 0 :(得分:1)
看起来我找到了解决方案
class MyComponents(context: Context) extends BuiltInComponentsFromContext(context) with Module {
override lazy val httpFilters = Seq(ExampleFilter)
lazy val router: Router = wire[Routes] withPrefix "/"
}
,只需从 BuiltInComponents 特征覆盖 httpFilters (如上所述),然后将 ExampleFilter 更改为对象即可运行
答案 1 :(得分:0)
看起来您的Filters
位于未命名(默认)的包中。把它放在包中的某个地方,比如appfilters.Filters
。
所以
package appfilters
...
class Filters(ex:ExampleFilter) extends HttpFilters {
...
和
play.http.filters = appfilters.Filters