tags:

views:

748

answers:

2

So is there a way to initialize and start a command line Spring app without writing a main method. It seems like all such main methods have the same form

public static void main(final String[] args) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml", Boot.class);
    FooService fooService = (FooService) ctx.getBean("fooService");
    fooService.bar();
}

I suppose that's not complicated, but has someone found a way to provide a way to just specify the context.xml at the command line or, better yet, in a manifest file?

The goal here is to simplify the creation of spring applications as executable jars. I hope that I can specify some utility class as the Main-Class in the manifest. I suppose I would also need to specify the starting point for the app, a bean and a method on it where begins the process.

+4  A: 

I'll try to answer the question as I understand it:

How to package a jar containing a spring configuration such as I just need to use java -jar myjar.jar?

The code snippet you have in your question simply works. You don't have to parameterise the context.xml. You just need to bundle your code and its dependencies (spring, etc.) in a single jar with a proper manifest entry for the main class in a jar file.

I personaly use maven 2 and here is a pom.xml I would use that do just that:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>stackoverflow-autostart-spring-app</artifactId>
  <version>0.1</version>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring</artifactId>
      <version>2.5.2</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.stackoverflow.spring.autostart.Autostart</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

This is assuming some package name for the java code, the source code being in the src/main/java directory and the file context.xml in the src/main/resources directory.

So in this pom.xml there are several important points:

  1. the spring dependency (speaks for itself I believe)
  2. the configuration of the maven jar plugin, that adds the main class as a manifest entry
  3. the maven shade plugin, which is the plugin responsible for gathering all the dependencies/classes and packaging them into one single jar.

The executable jar will be available at target\stackoverflow-autostart-spring-app-0.1.jar when running mvn package.

I have this code all working on my box but just realised that I can't attach a zip file here. Anyone know of place I could do so and link here?

I created a git repository at github with the code related to this question if you want to check it out.

Hope this helps.

SaM
+3  A: 

Yes. Write a simple SpringMain which takes an arbitrary number of xml and properties files as the arguments. You can then (in the main method) initialize an application from these files. Starting your program is then simply a matter of:

java -cp myapp.jar util.SpringMain context.xml

You then use the lifecycle attributes (init-method) on your relevant beans to kick-start the application

oxbow_lakes