近期在研究谷歌出的一个轻量级的di框架guice,整合实现jsr311标准的jersey框架,网上这方面的资料不多,所以记录下学习过程以及抛出问题的,大家一起学习讨论。
Pom配置文件:
web.xml的配置:
<listener> <listener-class>com.nd.sdp.guice.WafGuiceServletContext</listener-class> </listener> <filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> </filter> <filter-mapping> <filter-name>guiceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
public class WafGuiceServletContext extends GuiceServletContextListener { Logger logger = Logger.getLogger(this.getClass()); public static Injector injector; @Override protected Injector getInjector() { injector = Guice.createInjector(new ServletModule() { // Configure your IOC @Override protected void configureServlets() { logger.info("======getInjector======="); bind(ServletContainer.class).in(Singleton.class); Map<String, String> props = new HashMap<String, String>(); props.put("javax.ws.rs.Application", WafApplication.class.getName()); // props.put("jersey.config.server.wadl.disableWadl", "true"); serve("/*").with(ServletContainer.class, props); } }); return injector; } }
import javax.inject.Inject; import org.apache.log4j.Logger; import org.glassfish.jersey.server.ResourceConfig; public class WafApplication extends ResourceConfig { Logger logger = Logger.getLogger(this.getClass()); @Inject public WafApplication() { logger.info("============WafApplication=========="); register(new MyApplicationBinder()); // Set package to look for resources in packages(true, "com.nd.sdp.controller"); } }
import org.glassfish.hk2.utilities.binding.AbstractBinder; import com.google.inject.Singleton; import com.nd.sdp.service.StudentService; import com.nd.sdp.service.StudentServiceImpl; public class MyApplicationBinder extends AbstractBinder { @Override protected void configure() { // TODO Auto-generated method stub bind(StudentService.class).to(StudentServiceImpl.class).in(Singleton.class); } }
tomcat7启动运行,抛出异常信息:
警告: The following warnings have been detected: WARNING: Unknown HK2 failure detected: MultiException stack 1 of 3 org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=StudentService,parent=StudentController,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1565623650) at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74) at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:214) at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:237) at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:360) at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471) at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:161) at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2270) at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:687) at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:652) at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:169) at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185) at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74) at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:116) at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:116) at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:116) at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:94) at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:63) at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197) at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:295) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:267) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:316) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1073) at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:377) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221) at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263) at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178) at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91) at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62) at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118) at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
知道怎么接这个bug的同学欢迎指教!谢谢!