3
我試圖將RESTEasy與AppEngine上的Weld結合起來,但有麻煩做構造函數注入。CDI/Weld無法做RESTEasy資源的構造函數注入
我已經添加了RESTEasy CdiInjectorFactory上下文參數和Weld servlet偵聽器。
我的RESTEasy應用程序類的樣子:
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> classes = new HashSet<Class<?>>();
classes.add(CustomerResource.class);
return classes;
}
}
和CustomerResource:
@Path("/rest/app/customers")
public class CustomerResource {
private final CustomerService customerService;
@Inject
public CustomerResource(CustomerService customerService) {
this.customerService = customerService;
}
..
}
向客服就像是一個簡單的服務:
public class CustomerService {
public List<Customer> getCustomers() {
..
}
}
當我啓動服務器我在日誌中看到:
[INFO] 2014-01-30 21:34:53 INFO org.jboss.weld.Version:146 - WELD-000900: 2.1.2 (Final)
[INFO] 2014-01-30 21:34:53 WARN org.jboss.weld.environment.servlet.Listener:137 - @Resource injection not available in simple beans
[INFO] 2014-01-30 21:34:54 INFO org.jboss.weld.Bootstrap:199 - WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
[INFO] 2014-01-30 21:34:55 DEBUG org.jboss.resteasy.cdi.ResteasyCdiExtension:68 - Discovered CDI bean which is javax.ws.rs.core.Application subclass com.mycomp.config.MyApplication.
[INFO] 2014-01-30 21:34:55 DEBUG org.jboss.resteasy.cdi.ResteasyCdiExtension:68 - Bean class com.mycomp.config.MyApplication does not have the scope defined. Binding to @javax.enterprise.context.ApplicationScoped().
[INFO] 2014-01-30 21:34:55 DEBUG org.jboss.resteasy.cdi.ResteasyCdiExtension:68 - Discovered CDI bean which is a JAX-RS resource com.mycomp.rest.app.CustomerResource.
[INFO] 2014-01-30 21:34:55 DEBUG org.jboss.resteasy.cdi.ResteasyCdiExtension:68 - Bean class com.mycomp.rest.app.CustomerResource does not have the scope defined. Binding to @javax.enterprise.context.RequestScoped().
[INFO] 2014-01-30 21:34:55 INFO org.jboss.weld.environment.servlet.Listener:147 - No supported servlet container detected, CDI injection will NOT be available in Servlets, Filters or Listeners
[INFO] 2014-01-30 21:34:56 WARN org.jboss.weld.Interceptor:47 - WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled
[INFO] 2014-01-30 21:34:56 WARN org.jboss.weld.Interceptor:47 - WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled
從此日誌記錄中,我可以看到我的RESTEasy應用程序和資源類被發現。請注意,我沒有看到有關CustomerService的提及。
當我訪問我的資源在瀏覽器中,我得到:
[INFO] 2014-01-30 21:47:26 DEBUG org.jboss.resteasy.cdi.CdiInjectorFactory:68 - Using CdiConstructorInjector for class class com.mycomp.config.MyApplication.
[INFO] 2014-01-30 21:47:26 DEBUG org.jboss.resteasy.cdi.CdiConstructorInjector:68 - Beans found for class com.mycomp.config.MyApplication : [Managed Bean [class com.mycomp.config.MyApplication] with qualifiers [@Any @Default]]
[INFO] 2014-01-30 21:47:26 INFO org.jboss.resteasy.spi.ResteasyDeployment:82 - Deploying javax.ws.rs.core.Application: class com.mycomp.config.MyApplication$Proxy$_$$_WeldClientProxy
[INFO] 2014-01-30 21:47:26 INFO org.jboss.resteasy.spi.ResteasyDeployment:82 - Adding class resource com.mycomp.rest.app.CustomerResource from Application class com.mycomp.config.MyApplication$Proxy$_$$_WeldClientProxy
[INFO] 2014-01-30 21:47:26 INFO org.jboss.resteasy.spi.ResteasyDeployment:82 - Adding class resource com.mycomp.rest.HeartbeatResource from Application class com.mycomp.config.MyApplication$Proxy$_$$_WeldClientProxy
[INFO] Jan 30, 2014 9:47:26 PM com.google.appengine.tools.development.ApiProxyLocalImpl log
[INFO] SEVERE: javax.servlet.ServletContext log: unavailable
[INFO] java.lang.RuntimeException: Could not find constructor for class: com.mycomp.rest.app.CustomerResource
是的RESTEasy試圖創建一個新的CustomerResource實例,而不是從焊縫採摘呢?
我也嘗試過使用字段注入(而不是構造函數注入),然後我可以看到CustomerService很好地注入到CustomerResource中。
我只喜歡構造函數注入,並想知道在使用Weld時RESTEasy資源類是否支持這種方法?