`
tmj_159
  • 浏览: 700283 次
  • 性别: Icon_minigender_1
  • 来自: 永州
社区版块
存档分类
最新评论

Spring shell的使用

 
阅读更多

工作这么多年了,权且大胆的预测,现在绝大部分java项目都在用Spring,大家对Spring 的理解还一直是AOP和IOC和Spring MVC的知识吧,其实Spring早就做了重构,在IOC的基础(Spring Context)上做了很多有用的东西,Spring Shell就是这样一个东西。

 

Spring Shell可以让你的项目很轻松的支持命令行的功能,可能有些人还觉得比较抽象,这么说假如你想让你的项目支持动态启动和停止,即java -jar xxx.jar start 就启动某个服务,然后输入stop就停止,最简单的方式就是解析main方法中的args ,然后调用不同的服务。

其实Spring Shell做的也是类似的东西,只是可以通过一些注解让你很轻松的把注解和方法之间建立联系。

看看下面官网给出的例子

@CliCommand(value = "hw simple", help = "Print a simple hello world message")
    public String simple(
            @CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
            @CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello", specifiedDefaultValue="At work") final String location) {
        simpleCommandExecuted = true;
        return "Message = [" + message + "] Location = [" + location + "]";
    }

程序启动后,输入

hw simple --message hell

其实就调用到上面的方法中了。

 

我跑起来了一个最简单的例子,现在贴出来供大家理解

package com.tang.test.sprintShell;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.springframework.shell.Bootstrap;
import org.springframework.shell.core.CommandResult;
import org.springframework.shell.core.JLineShellComponent;

public class HelloWorldCommandTest {

    @Test
    public void testSimple() {
        Bootstrap bootstrap = new Bootstrap();

        JLineShellComponent shell = bootstrap.getJLineShellComponent();

        CommandResult cr = shell.executeCommand("hw simple --message hello");
        assertEquals(true, cr.isSuccess());
        assertEquals("Message = [hello] Location = [null]", cr.getResult());
    }
}

 我们使用Spring Shell主要写的东西。

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.tang.test.springShell" />
</beans>

配置文件

 

package com.tang.test.sprintShell;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.springframework.shell.Bootstrap;
import org.springframework.shell.core.CommandResult;
import org.springframework.shell.core.JLineShellComponent;

public class HelloWorldCommandTest {

    @Test
    public void testSimple() {
        Bootstrap bootstrap = new Bootstrap();

        JLineShellComponent shell = bootstrap.getJLineShellComponent();

        CommandResult cr = shell.executeCommand("hw simple --message hello");
        assertEquals(true, cr.isSuccess());
        assertEquals("Message = [hello] Location = [null]", cr.getResult());
    }
}

测试类

 

ps ,注意配置文件需要放在  resources/META-INF下,至于为什么我们看看Bootstrap源码就知道了

public class Bootstrap {
    private static final String[] CONTEXT_PATH = new String[]{"classpath*:/META-INF/spring/spring-shell-plugin.xml"};
//后面略

 

最后附上pom文件

<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>org.springframework.shell.samples</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.1.0.RELEASE</version>
    <packaging>jar</packaging>

    <name>helloworld</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.shell.version>1.2.0.RC1</spring.shell.version>
        <jar.mainclass>org.springframework.shell.Bootstrap</jar.mainclass>
        <log4j.version>1.2.17</log4j.version>
        <junit.version>4.10</junit.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.shell</groupId>
            <artifactId>spring-shell</artifactId>
            <version>${spring.shell.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit-dep</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>true</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <useUniqueVersions>false</useUniqueVersions>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>${jar.mainclass}</mainClass>
                        </manifest>
                        <manifestEntries>
                            <version>${project.version}</version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>

    </build>


    <repositories>
        <repository>
            <id>libs-milestone</id>
            <url>http://repo.spring.io/libs-milestone/</url>
        </repository>
        <repository>
            <id>libs-release</id>
            <url>http://repo.spring.io/libs-release/</url>
        </repository>
    </repositories>
</project>

  

希望对你有所帮助,谢谢大家。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics