[英]how to inject Application scope bean on rest web service java
我必須在ContextCacheRefresh Web服務上注入initApplicationContext bean,但是不成功的initApplicationContext值總是為null。 有沒有人知道如何處理它?
@ManagedBean(name = "initApplicationContext", eager = true)
@ApplicationScoped
public class InitApplicationContext {
.......................
}
和網絡服務
@Path("/serviceContext")
public class ContextCacheRefresh {
@ManagedProperty(value = "#{initApplicationContext}")
private InitApplicationContext initApplicationContext;
@GET
@Path("/refreshContext")
public Response refreshUserListOn(@QueryParam("param") String param
) { ......
使用@ManagedProperty
,您將無法讓JSF將資源注入非JSF上下文。 你的選擇是
轉換托管bean以使用CDI注釋( @Named
來聲明托管bean和@Inject
而不是現在正在使用的JSF注釋。
只需使用以下命令從普通的servlet上下文中提取bean:
//inject the servlet context @javax.ws.rs.core.Context ServletContext servletContext public InitApplicationContext getInitContext(){ return (InitApplicationContext)servletContext.getAttribute("initApplicationContext"); }
你正在努力的事情對我來說似乎有點狡猾。 為什么您的Web應用程序首先關注您的RESTful端點?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.