使用afBedSheet的文件中的示例
using afIoc
using afBedSheet
class HelloPage {
Text hello(Str name, Int iq := 666) {
return Text.fromPlain("Hello! I'm $name and I have an IQ of $iq!")
}
}
class AppModule {
@Contribute { serviceType=Routes# }
static Void contributeRoutes(Configuration conf) {
conf.add(Route(`/index`, Text.fromPlain("Welcome to BedSheet!")))
conf.add(Route(`/hello/**`, HelloPage#hello))
}
}
class Example {
Int main() {
return afBedSheet::Main().main([AppModule#.qname, "8080"])
}
}如果我在示例类中将这一行添加为main()方法的第一行
registry := IocService([AppModule#]).start.registryafBedSheet应用程序不会启动并抛出:
[21:40:10 11-Aug-14] [info] [afIoc] Adding module definition for Example_0::AppModule
[21:40:10 11-Aug-14] [info] [afIoc] Starting IOC...
[21:40:10 11-Aug-14] [err] [afIoc] Err starting IOC
afIoc::IocErr: Service does not exist for Type 'afBedSheet::Routes' defined in contribution method Example_0::AppModule.contributeRoutes.
Ioc Operation Trace:
[ 1] Building IoC Registry
[ 2] Validating contribution definitions
Stack Trace:
afIoc::Utils.stackTraceFilter (Utils.fan:45)
afIoc::RegistryBuilder.build (RegistryBuilder.fan:108)
afIoc::IocService.onStart (IocService.fan:76)
fan.sys.Service$.start (Service$.java:206)
afIoc::IocService.start (IocService.fan:10)
Example_0::Example.main (/Users/coder/Documents/temp/testBedSheet/Example.fan:20)
java.lang.reflect.Method.invoke (Method.java:483)
fan.sys.Method.invoke (Method.java:559)
fan.sys.Method$MethodFunc.callOn (Method.java:230)
fan.sys.Method.callOn (Method.java:139)
fanx.tools.Fan.callMain (Fan.java:175)
fanx.tools.Fan.executeFile (Fan.java:98)
fanx.tools.Fan.execute (Fan.java:37)
fanx.tools.Fan.run (Fan.java:298)
fanx.tools.Fan.main (Fan.java:336)
ERROR: fan.afIoc.IocService.onStart如何访问IocService的afBedSheet应用程序以获得绑定服务?
发布于 2014-08-11 14:29:50
没有必要创建您自己的IoC注册表,因为BedSheet为您创建了一个。
另外,不应该需要访问web应用程序之外的注册表。
如果您需要在web应用程序启动时发生什么事情,请为RegistryStartup提供一个func /闭包
class AppModule {
@Contribute { serviceType=RegistryStartup# }
static Void contributeRegistryStartup(Configuration conf, MyService myService) {
conf.add |->| {
myService.soSomething()
}
}
}若要在web请求期间访问,请将@Inject服务放入处理程序:
class HelloPage {
@Inject MyService? myService
Text hello(Str name, Int iq := 666) {
myService.doSomething()
return Text.fromPlain("Hello! I'm $name and I have an IQ of $iq!")
}
}https://stackoverflow.com/questions/25242625
复制相似问题