前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring shell 的HelloWorld

Spring shell 的HelloWorld

作者头像
克虏伯
发布2019-04-15 14:51:35
6340
发布2019-04-15 14:51:35
举报

Spring官网看到SpringShell,自己最近探索了一下,写个简单的HelloWorld

注:我参考的是官方的sample,地址:https://github.com/spring-projects/spring-shell/tree/master/samples/helloworld,可以clone到本地自己运行一下它的HelloWorld。

我的开发环境: windows7 64bit

开发工具: eclipse luna、maven

废话不多说:亮代码,如下:

整体结构:

pom.xml:

代码语言:javascript
复制
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mjduan</groupId>
  <artifactId>LearnSpringShell2</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>LearnSpringShell2 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
	<properties>
		<spring.shell.version>1.2.0.RELEASE</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>
    <finalName>LearnSpringShell2</finalName>
    <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>
</project>

log4j.properties:

代码语言:javascript
复制
#让log4j不输出日志,不然屏幕上全是日志信息
log4j.rootCategory=OFF

spring-shell-plugin.xml:

代码语言:javascript
复制
<?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.mjduan" />

</beans>

Main.java:

代码语言:javascript
复制
package com.mjduan.learnSpringShell;

import java.io.IOException;

import org.springframework.shell.Bootstrap;

public class Main {
	
	public static void main(String[] args) throws IOException {
		Bootstrap.main(args);
	}

}

MyPromptProvider.java:

代码语言:javascript
复制
package com.mjduan.learnSpringShell.commands;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.shell.plugin.support.DefaultPromptProvider;
import org.springframework.stereotype.Component;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MyPromptProvider extends DefaultPromptProvider{

	@Override
	public String getPrompt() {
		return "mingJun@duan>";
	}
	
	@Override
	public String getProviderName() {
		return "my prompt provider";
	}
	
}

MyHistoryFileNameProvider.java:

代码语言:javascript
复制
package com.mjduan.learnSpringShell.commands;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.shell.plugin.support.DefaultHistoryFileNameProvider;
import org.springframework.stereotype.Component;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MyHistoryFileNameProvider extends DefaultHistoryFileNameProvider{

	//会在当面目录下新建日志文件
	@Override
	public String getHistoryFileName() {
		return "myLearnSpringShell.log";
	}
	
	@Override
	public String getProviderName() {
		return "My History file name provider";
	}
}

MyBannerProvider.java:

代码语言:javascript
复制
package com.mjduan.learnSpringShell.commands;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.shell.plugin.support.DefaultBannerProvider;
import org.springframework.shell.support.util.OsUtils;
import org.springframework.stereotype.Component;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MyBannerProvider extends DefaultBannerProvider{

	@Override
	public String getBanner() {
		StringBuffer buf = new StringBuffer();
		buf.append("=======================================" + OsUtils.LINE_SEPARATOR);
		buf.append("*                                     *"+ OsUtils.LINE_SEPARATOR);
		buf.append("*               mjDuan                *" +OsUtils.LINE_SEPARATOR);
		buf.append("*                                     *"+ OsUtils.LINE_SEPARATOR);
		buf.append("=======================================" + OsUtils.LINE_SEPARATOR);
		buf.append("Version:" + this.getVersion());
		return buf.toString();
	}
	
	@Override
	public String getVersion() {
		return "1.1.0";
	}
	
	@Override
	public String getWelcomeMessage() {
		return "Welcome to mjDuan CLI";
	}
	
	@Override
	public String getProviderName() {
		return "My promt provider";
	}
}

HelloWorldCommands.java:

代码语言:javascript
复制
package com.mjduan.learnSpringShell.commands;

import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;

@Component
public class HelloWorldCommands implements CommandMarker{

	private boolean simpleCommandExecuted = false;
	
	@CliAvailabilityIndicator({"hw simple"})
	public boolean isSimpleCommandExecuted() {
		return true;
	}

	//返回为true时才可以执行hw complex和hw enum命令
	@CliAvailabilityIndicator({"hw complex","hw enum"})
	private boolean isComplexAvailable() {
		if(simpleCommandExecuted){
			return true;
		}else{
			return false;
		}
	}
	@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 + "]";
	}
	
	@CliCommand(value = "hw complex", help = "Print a complex hello world message (run 'hw simple' once first)")
	public String hello(
		@CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
		@CliOption(key = { "name1"}, mandatory = true, help = "Say hello to the first name") final String name1,
		@CliOption(key = { "name2" }, mandatory = true, help = "Say hello to a second name") final String name2,
		@CliOption(key = { "time" }, mandatory = false, specifiedDefaultValue="now", help = "When you are saying hello") final String time,
		@CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello") final String location) {		
		return "Hello " + name1 + " and " + name2 + ". Your special message is "  + message + ". time=[" + time + "] location=[" + location + "]";
	}
	
	@CliCommand(value = "hw enum", help = "Print a simple hello world message from an enumerated value (run 'hw simple' once first)")
	public String eenum(
		@CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final MessageType message){		
		return "Hello.  Your special enumerated message is " + message;
	}
	
	enum MessageType {		
		Type1("type1"),
		Type2("type2"),
		Type3("type3");
		
		private String type;
		
		private MessageType(String type){
			this.type = type;
		}
		
		public String getType(){
			return type;
		}
	}
	
}

打jar包:

到这,可以打jar包了(不是war),可以用命令行执行mvn package或者在eclipse中Run as -> maven build...之后打jar包。

运行:

用windows的cmd命令行切换到项目的源码目录下,执行java -jar ?target/LearnSpringShell2.jar,如下图所示:

注:在下也只是自己探索了一下,没有深入的研究。

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016/09/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com