DroidQuelle
DroidQuelle

Reputation: 21

Jersey Constructor

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

Upvotes: 0

Views: 1013

Answers (3)

Cory Harter
Cory Harter

Reputation: 134

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>

Upvotes: 0

feuyeux
feuyeux

Reputation: 1196

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

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359776

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.

Upvotes: 3

Related Questions