我试图自己初始化我的Web服务资源,从Application类重写getSingletons方法。我正在使用Jboss EAP 6.0 app服务器。以下是代码
@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服务是如何工作的。
我认为,因为我没有返回单例,所以应用程序服务器正在自己初始化webservices。所以我删除了@Resource并使用了@Inject,我得到了一个WELD异常,说没有找到依赖项。
然后我改变它以返回单例,然后@Resource没有查找ejb并且studentService为null。所以我使用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();
}
任何人都可以告诉我
答案 0 :(得分:1)
“1。为什么应用程序服务器初始化Web服务,尽管我返回了null”
如果在查找期间没有返回类/对象(或只是空集),则行为将恢复为资源和提供程序的类路径扫描(这在JAX-RS规范中进行了解释)。从getClasses
或getSingleton
返回任何资源类或提供程序后,系统会假定开发人员处理注册,并禁用类路径注册。
“2。当我返回null时,为什么@Inject失败,只有@Resource正在工作。”
我在@Inject
的任何论据中都没有看到成功案例,所以我不会试图对此发表评论。对于@Resource
,它似乎是注入资源的正确方法。启用类路径注册时,您不会创建运行时的JAX-RS类。运行时也处理注入。如果您自己创建实例,则运行时将不会尝试注入。
“3。当我返回单身人士时,为什么@Resource失败并且只有InitialContext正在运行。”
以上部分回答了这个问题。实例化类时,不执行注入。当运行时创建实例时,它会经历注入过程。
需要注意的一点是,在实例化时,资源类将是一个单例,这意味着每个应用程序只有一个实例。这可能是也可能不是期望的行为。当运行时实例化类时,默认情况下它将处于请求范围内,这意味着每个请求创建一个实例。再一次,可能是也可能不是你想要的。如果你想维护类路径注册,并且仍然是单例,那么使用@javax.inject.Singleton
注释资源类应该使它成为单例,并且仍然允许注入,因为运行时仍然实例化类。如果您希望这些类是请求作用域,并希望自己注册所有类,则覆盖public Set<Class<?>> getClasses()
并将您的类添加到集合中。