Spring-Context Konfiguration ohne Application Server

Auch ausserhalb eines Application Servers wie tomcat lässt sich ein spring context recht einfach konfigurieren.
Lediglich um den richtigen Zeitpunkt zum Starten und Stoppen des Kontexts muss man sich selbst kümmern.

Dazu verwendet man zumeist einen sog. Launcher, was lediglich eine Klasse bezeichnet, welche das Starten und Stoppen des Kontexts in entsprechenden Methoden kapselt.

Am einfachsten ist dabei die Verwendung des GenericApplicationContext.

Update:

Wie Matthias in seinem Kommentar richtig Anmerkt, stellt der ClassPathXmlApplicationContext bereits start() und stop() zur Verfügung. Die Post-processor Beans können einfach in der Spring-Konfiguration hinzugefügt werden.

Hier ein Beispiel für eine einfache Launcher-Implementierung mit GenericApplicationContext.html:

package ...;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * The {@link SpringLauncher} class provides methods to set up
 * and destroy the spring context using a default or provided set of
 * classpath elements representing spring metadata (xml configurations).
 *
 * @author Olaf Otto
 */
public class SpringLauncher {
    private GenericApplicationContext _context = new GenericApplicationContext();
    private String _springConfigLocation = "foo/bar/spring-context.xml";

    /**
     * Sets the location of a classpath spring configuration, for example:<br />
     * <code>foo/bar/spring-context.xml</code>.
     *
     * @param location must not be null.
     */
    public void setConfigLocation(String location) {
        if (location == null) {
            throw new IllegalArgumentException("Method argument location must not be null.");
        }
        _springConfigLocation = location;
    }

    /**
     * Initializes and starts the spring context.
     *
     * @see GenericApplicationContext
     * @see GenericApplicationContext#refresh();
     * @see XmlBeanDefinitionReader
     */
    public void start() {
        if (_context.isActive()) {
            throw new IllegalStateException("start may not be invoked on an active context. Call stop() first.");
        }
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(_context);
        xmlReader.loadBeanDefinitions(new ClassPathResource(_springConfigLocation));
        _context.refresh();
    }

    /**
     * Stops a previously started spring context.
     *
     * @see GenericApplicationContext#close()
     */
    public void stop() {
        if (!_context.isActive()) {
            throw new IllegalStateException("stop may not be invoked on an inactive context. Call start() first.");
        }
        _context.close();
    }

    /**
     * Returns true if the context was started.
     */
    public boolean isStarted() {
        return _context.isActive();
    }
}

This entry was posted on Tuesday, February 5th, 2008 at 21:08. Posted in: J2EE, System architecture, java, spring. You can follow any responses to this entry through the RSS 2.0feed. You can leave a response, or trackback from your own site.

One Response to “Spring-Context Konfiguration ohne Application Server”

  1. Mathias says:

    Die Behauptung “Am einfachsten ist dabei die Verwendung des GenericApplicationContext. Im Gegensatz zum ClassPathXmlApplicationContext stellt dieser die refresh() und close() – Methoden zur Verfügung” stimmt so nicht.

    Beide Klassen implementieren ConfigurableApplicationContext wo diese Methoden definiert sind. Von daher kann man sich den Umweg über XmlBeanDefinitionReader sparen und gleich ClassPathXmlApplicationContext verwenden.

Leave a Reply