我正在尝试通过自己覆盖 Application 类中的 getSingletons 方法来初始化我的 Web 服务资源。我正在使用 Jboss EAP 6.0 应用程序服务器。下面是代码
@ApplicationPath("/rest")
public class MyApplication extends Application {
private final Set<Object> singletons = new HashSet<Object>();
public MyApplication() {
singletons.add(new StudentFacade());
}
@Override
public Set<Object> getSingletons() {
return null; // Accidentally kept it as null but still my webservice was working. Dont know how it was working.
}
}
@Path("student")
public class StudentFacade {
@Resource(lookup = "java:global/ejb-beginner-sessions-ear/ejb-beginner-sessions-impl/StudentServiceBean!com.ejb.beginner.sessions.api.StudentService")
private StudentService studentService;
private static final Logger LOGGER = Logger.getLogger(StudentFacade.class.getName());
@GET
@Path("list")
@Produces(MediaType.APPLICATION_JSON)
public Student getStudents() {
LOGGER.info("studentService: " + studentService);
return new Student();
}
}
然后我意识到 getSingletons 方法返回 null,并想知道我的网络服务如何仍在工作。
我认为因为我没有返回单例,所以应用程序服务器正在自行初始化 Web 服务。因此,我删除了 @Resource 并使用了 @Inject,并且收到了 WELD 异常,表示未找到依赖项。
然后我将其更改为返回单例,然后@Resource没有查找ejb并且studentService为空。所以我使用 InitialContext 来查找 ejb,它起作用了。
try {
InitialContext initialContext = new InitialContext();
StudentService studentService = (StudentService) initialContext
.lookup("java:global/ejb-beginner-sessions-ear/ejb-beginner-sessions-impl/StudentServiceBean!com.ejb.beginner.sessions.api.StudentService");
LOGGER.info("studentService1: " + studentService);
} catch (NamingException e) {
e.printStackTrace();
}
谁能告诉我
- 为什么尽管我返回了 null,应用服务器还是初始化了 Web 服务
- 当我返回null时,为什么@Inject失败并且只有@Resource在工作。
- 当我返回单例时,为什么@Resource失败并且只有InitialContext可以工作。
最佳答案
"1. Why did the application server initialize the webservice despite I returned null"
当查找期间没有返回类/对象(或只是空集)时,行为将恢复为资源和提供程序的类路径扫描(这在 JAX-RS 规范中进行了解释)。一旦任何资源类或提供者从 getClasses
返回或getSingleton
,假设开发者处理注册,并且类路径注册被禁用。
"2. When I returned null, why @Inject failed and only @Resource was working."
我在您关于@Inject
的任何论点中都没有看到成功的案例。 ,所以我不会尝试对此发表评论。至于@Resource
,这似乎是注入(inject)资源的正确方法。启用类路径注册后,您不会创建 JAX-RS 类,而是运行时会创建。运行时还处理注入(inject)。如果您自己创建实例,运行时将不会尝试注入(inject)。
"3. When I returned singletons, why @Resource failed and only InitialContext was working."
上面已经部分回答了这个问题。当您实例化该类时,不会执行注入(inject)。当运行时创建实例时,它会经历注入(inject)过程。
<小时/>需要注意的一件事是,当您实例化时,资源类将是单例,这意味着每个应用程序只有一个实例。这可能是也可能不是期望的行为。当运行时实例化该类时,默认情况下它将处于请求范围内,这意味着每个请求创建一个实例。再说一遍,可能是也可能不是您想要的。如果你想保持类路径注册,并且仍然是单例,那么用@javax.inject.Singleton
注释资源类应该使其成为单例,并且仍然允许注入(inject),因为运行时仍然实例化该类。如果您希望这些类处于请求范围内,并且希望自己注册所有类,则覆盖 public Set<Class<?>> getClasses()
并将您的类(class)添加到集合中。
关于java - 从 Application 类重写 getSingletons 方法时,Ejb 查找失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847699/