如何在系统启动时加载某些东西?我的程序启动时没有“main”!Jersey构造函数
0
A
回答
3
您可以使用在Application#getSingletons()
中定义的单例对象。
public class MyApp extends Application
{
public Set<Class<?>> getClasses()
{
return null;
}
public Set<Object> getSingletons()
{
Set<Object> set = new HashSet<Object>();
Foo foo = /* init foo somehow */;
set.add(foo);
return set;
}
}
从RESTful Java(如果你有这本书,见第142页):
的
getSingletons()
方法返回的预分配的JAX-RS Web服务和@Provider
-annotated类的列表。作为应用程序员,您负责创建这些对象。 JAX-RS运行时将迭代对象列表并在内部注册它们。当这些对象被注册时,JAX-RS也将注入@Context
注释字段和setter方法的值。
1
一般来说,球衣是由maven构建的。所以,当你执行maven命令时,会生成一个已初始化的项目。
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \
-DarchetypeVersion=2.4.1
更多信息请访问: https://jersey.java.net/documentation/latest/index.html
0
你可以写一个实现了ServletContextListener类。
public class MyContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
//stuff that happens when server is started
}
@Override
public void contextDestroyed(ServletContextEvent event) {
//stuff that happens when server is turned off
}
}
然后,您只需将此添加到您的web.xml文件中作为web应用程序元素的子项。
<listener>
<listener-class>com.mypackage.MyContextListener</listener-class>
</listener>
相关问题
- 1. 构造函数内的构造函数
- 2. 混合Guice构造函数注入和Jersey @Context
- 3. Jersey Singleton使用Spring:方法不能转换为构造函数
- 4. 构造函数
- 5. 构造函数中的调用基构造函数和其他构造函数
- 6. C++ - 构造函数,复制构造函数,移动构造函数,析构函数
- 7. 没有参数的构造函数或参数构造函数
- 8. 无参数构造函数调用2参数构造函数
- 9. 构造函数和析构函数
- 10. 构造函数和析构函数 - C++
- 11. C++类构造函数/析构函数
- 12. 析构函数和构造函数
- 13. 重载构造映射构造函数
- 14. 构造函数在JavaScript中的构造?
- 15. NullPointerException构造函数
- 16. 构造函数InterstitialAd
- 17. 构造函数GetInfo
- 18. Java构造函数?
- 19. 用构造函数
- 20. 卡构造函数
- 21. Java构造函数
- 22. ArrayList构造函数
- 23. Codewars构造函数
- 24. 构造函数链
- 25. Overloadding构造函数
- 26. PHP构造函数
- 27. 多构造函数
- 28. DispatcherTimer构造函数
- 29. JavaScript构造函数
- 30. Javascript函数构造