使用afBedSheet's documentation中的示例:
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"])
}
}
如果我将此行添加为Example类
中main()方法的第一行 registry := IocService([AppModule#]).start.registry
afBedSheet应用程序无法启动并抛出:
[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
如何访问afBedSheet应用程序的IocService以获取绑定服务?
答案 0 :(得分:1)
由于BedSheet为您创建了一个IoC注册表,因此无需创建自己的IoC注册表。
此外,不应该在Web应用程序之外访问注册表。
如果您需要在网络应用启动时发生某些事情,请向RegistryStartup提供func / closure:
class AppModule {
@Contribute { serviceType=RegistryStartup# }
static Void contributeRegistryStartup(Configuration conf, MyService myService) {
conf.add |->| {
myService.soSomething()
}
}
}
要在网络请求期间访问,@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!")
}
}