
STEP 1 : Add spring-context to pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.junjun.util.spring</groupId> | |
<artifactId>spring-way-standalone</artifactId> | |
<version>1.0.0</version> | |
<properties> | |
<version.java>1.8</version.java> | |
<version.spring>4.3.8.RELEASE</version.spring> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>${version.spring}</version> | |
</dependency> | |
<dependency> | |
<groupId>log4j</groupId> | |
<artifactId>log4j</artifactId> | |
<version>1.2.17</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.5.1</version> | |
<configuration> | |
<source>${version.java}</source> | |
<target>${version.java}</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-jar-plugin</artifactId> | |
<configuration> | |
<archive> | |
<manifest> | |
<addClasspath>true</addClasspath> | |
<classpathPrefix>lib/</classpathPrefix> | |
<mainClass>com.junjun.util.spring.Launcher</mainClass> | |
</manifest> | |
<manifestEntries> | |
<Class-Path>conf/</Class-Path> | |
</manifestEntries> | |
</archive> | |
<includes> | |
<include>**/*.class</include> | |
<include>*.properties.not.configurable</include> | |
</includes> | |
</configuration> | |
</plugin> | |
<plugin> | |
<artifactId>maven-assembly-plugin</artifactId> | |
<executions> | |
<execution> | |
<id>build-distribution</id> | |
<phase>verify</phase> | |
<goals> | |
<goal>attached</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<descriptors> | |
<descriptor>src/assembly/dist-build.xml</descriptor> | |
</descriptors> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<assembly> | |
<id></id> | |
<formats> | |
<format>tar.gz</format> | |
</formats> | |
<includeBaseDirectory>false</includeBaseDirectory> | |
<dependencySets> | |
<dependencySet> | |
<unpack>false</unpack> | |
<outputDirectory>lib</outputDirectory> | |
<excludes> | |
<exclude>com.junjun.util.spring:spring-util-parent</exclude> | |
</excludes> | |
</dependencySet> | |
</dependencySets> | |
<fileSets> | |
<fileSet> | |
<directory>${project.build.directory}</directory> | |
<outputDirectory>/</outputDirectory> | |
<includes> | |
<include>${project.artifactId}-${project.version}.jar</include> | |
</includes> | |
</fileSet> | |
<fileSet> | |
<directory>src/main/resources</directory> | |
<outputDirectory>conf</outputDirectory> | |
<includes> | |
<include>log4j.xml</include> | |
<include>*.properties</include> | |
</includes> | |
</fileSet> | |
</fileSets> | |
</assembly> |
STEP 2 : Create a class which has a "main" method so that you can run the jar with this class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junjun.util.spring; | |
import org.apache.log4j.Logger; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
import com.junjun.util.spring.config.AppConfig; | |
import com.junjun.util.spring.service.TestService; | |
public class Launcher { | |
private final static Logger logger = Logger.getLogger(Launcher.class); | |
public static void main(String[] args) { | |
// use ClassPathXmlApplicationContext if your configuration is XML | |
// instead of annotation based. | |
final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); | |
ctx.register(AppConfig.class); | |
ctx.refresh(); | |
ctx.start(); | |
TestService service = ctx.getBean(TestService.class); | |
logger.info(service.echo("standalone")); | |
ctx.close(); | |
System.exit(0); | |
} | |
} |
STEP 3 : Define a class "AppConfig" for your application configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junjun.bi.config; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
@ComponentScan(basePackages = { "com.junjun.bi" }) | |
@Configuration | |
public class AppConfig { | |
} |
STEP 4 : Define your service class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junjun.util.spring.service; | |
public interface TestService { | |
String echo(String message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junjun.util.spring.service; | |
import org.springframework.stereotype.Service; | |
@Service | |
public class TestServiceImpl implements TestService { | |
@Override | |
public String echo(String message) { | |
return "echo by test service : " + message; | |
} | |
} |
STEP 5 : run "mvn clean install" and unzip the tar.gz file
STEP 6 : run the standalone java application with below
Option 1 (OS independent) : java -jar spring-way-standalone-1.0.0.jar
Option 2 (UNIX) : nohup java -jar spring-way-standalone-1.0.0.jar > /dev/null 2>&1 &
You may refer to the project source code here : https://github.com/junjun-dachi/spring-util
JOB DONE.
No comments:
Post a Comment