Should An Embedded Jetty Server Be A Spring Bean

When we look for examples of containerless Java applications with an embedded Jetty server, it’s easy to find samples where the Server object is instantiated and run directly. As I started writing my own containerless web app with Spring, the question was raised: Should an embedded Server be managed by Spring, or should it exist only in public-static-void-main() … outside the context of the Spring portion of the application?

There is an advantage to keeping the server out of spring. When the spring context and the server are both instantiated in main(), it is very easy to find the startup code and follow what’s going on. However, if the server is instantiated by Spring and started as part of its lifecycle, the very basic architectural questions of “where is the server instantiated” and “how is it started” are hidden behind the Spring framework.

Compare this

// easy to navigate to
Server server = new MainServer();

With this

// what is the main class here?
Server server = applicationContext.getBean(Server.class);

Although to be fair, this is a more general issue with the nature of Inversion of Control containers, and easily remedied by familiarizing oneself with Spring.

On the other hand, there are some definite advantages to making the server be a managed bean inside Spring.

For one thing, application properties (such as port numbers, keystores, and keystore passwords) can be managed by a PropertySourcesPlaceholderConfigurer and injected via Environment into the Server. This frees you to use the configuration support provided by Spring, including profiles. Notice in this snippet how there is no custom parsing of properties files or environment variables necessary…

public class MainServer extends Server {

   public MainServer(Environment environment) {
      int securePort = Integer.parseInt(environment.getProperty("secure.port"));

For another thing, if the server is a spring bean, all of its dependencies can be automatically injected and it can itself be automatically injected into other beans that require it. This makes wiring of the server with other services that require it (such as a shutdown thread) easier.

public class ShutdownMonitorThread extends Thread {

    private int shutdownPort;
    private Server server;

    public ShutdownMonitorThread(final Server serverToStop, Environment environment) {
        server = serverToStop;
        shutdownPort = Integer.parseInt(environment.getProperty("shutdownPort"));

    // server.stop() can be called when shutdown request is received.

Given these advantages, overall I think the stronger advantage lies with making the server be a spring bean.

Leave a comment

Filed under Software Engineering

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s