泽西岛建设者

发布于 2024-11-01 03:38:22 字数 36 浏览 2 评论 0原文

如何在系统启动时加载某些内容?我的程序启动时没有“主”!?

How it's possible to load something at the start of the system? I`m not have a "main" where my program starts !?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱冒险 2024-11-08 03:38:22

您可以使用 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 带注释的类的列表。作为应用程序程序员,您负责创建这些对象。 JAX-RS 运行时将迭代对象列表并在内部注册它们。注册这些对象时,JAX-RS 还将为 @Context 带注释的字段和 setter 方法注入值。

You could use a singleton object defined in 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;
    }
}

From RESTful Java (if you have the book, see p.142):

The getSingletons() method returns a list of preallocated JAX-RS web services and @Provider-annotated classes. You, as the application programmer, are responsible for creating these objects. The JAX-RS runtime will iterate through the list of objects and register them internally. When these objects are registered, JAX-RS will also inject values for @Context annotated fields and setter methods.

榆西 2024-11-08 03:38:22

一般来说,jersey是由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

generally, the jersey is built by maven. So, when you execute the maven command, there's a initialized project generated.

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

More info please see:
https://jersey.java.net/documentation/latest/index.html

情话难免假 2024-11-08 03:38:22

您可以编写一个实现 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-app 元素的子元素。

<listener>
    <listener-class>com.mypackage.MyContextListener</listener-class>
</listener>

You could write a class that implements 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
        }
    }

Then you would just add this to your web.xml file as child to the web-app element.

<listener>
    <listener-class>com.mypackage.MyContextListener</listener-class>
</listener>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文