In this post, I am going to explain how to create simple application that perform Arithmetic operations.
Step 1: Create new class and annotate it with @ShellComponent annotation.
Step 2: Create methods that take some arguments and return some value. Annotate the methods with @ShellMethod annotation.
@ShellComponent
public class ArithmeticCommands {
@ShellMethod("Add two integers together.")
public int add(int a, int b) {
return a + b;
}
......
......
}
That’s it you are done.
Find the below working application.
Step 1: Create new maven project ‘hello-world’ in eclipse.
Step 2: Update pom.xml with maven dependencies.
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>hello-world</artifactId>
<packaging>jar</packaging>
<version>1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
<version>2.0.1.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<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}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
com.sample.app.App
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 3: Create new package ‘com.sample.app.commmands’ and define ArithmeticCommands.java.
ArithmeticCommands.java
package com.sample.app.commmands;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellComponent;
@ShellComponent
public class ArithmeticCommands {
@ShellMethod("Add two integers together.")
public int add(int a, int b) {
return a + b;
}
@ShellMethod("Subtract two integers together.")
public int sub(int a, int b) {
return a - b;
}
@ShellMethod("Multiply two integers together.")
public int mul(int a, int b) {
return a * b;
}
@ShellMethod("Devide two integers together.")
public int div(int a, int b) {
return a / b;
}
}
Step 4: Define App.java.
App.java
package com.sample.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String args[]) {
SpringApplication.run(App.class, args);
}
}
Run App.java.
You will see a yellow shell:> prompt that invites you to type commands.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.8.RELEASE)
2020-06-01 09:40:29.496 INFO 93580 --- [ main] com.sample.app.App : Starting App on m-c02z323dlvcg with PID 93580 (/Users/krishna/eclipse-workspace/hello-world/target/classes started by krishna in /Users/krishna/eclipse-workspace/hello-world)
2020-06-01 09:40:29.497 INFO 93580 --- [ main] com.sample.app.App : No active profile set, falling back to default profiles: default
2020-06-01 09:40:29.536 INFO 93580 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@530612ba: startup date [Mon Jun 01 09:40:29 IST 2020]; root of context hierarchy
2020-06-01 09:40:30.108 WARN 93580 --- [ main] org.jline : Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
2020-06-01 09:40:30.234 INFO 93580 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
shell:>
Execute the built in command ‘help’ to list all the available commands.
shell:>help AVAILABLE COMMANDS Arithmetic Commands add: Add two integers together. div: Devide two integers together. mul: Multiply two integers together. sub: Subtract two integers together. Built-In Commands clear: Clear the shell screen. exit, quit: Exit the shell. help: Display help about available commands. history: Display or save the history of previously run commands script: Read and execute commands from a file. stacktrace: Display the full stacktrace of the last error.
Execute below commands to perform Arithmetic operations.
add 10 20
sub 10 20
mul 10 20
div 10 20
shell:>add 10 20 30 shell:>sub 10 20 -10 shell:>mul 10 20 200 shell:>div 10 20 0
Once you are done with the application, execute the command ‘exit’ to terminate the application.
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240) none repeat scroll 0% 0%; color: rgb(68, 68, 68);">shell:>exit
2020-06-01 09:43:43.900 INFO 93580 --- [ main] com.sample.app.App : Started App in 194.573 seconds (JVM running for 194.835)
2020-06-01 09:43:43.900 INFO 93580 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@530612ba: startup date [Mon Jun 01 09:40:29 IST 2020]; root of context hierarchy
2020-06-01 09:43:43.902 INFO 93580 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
</pre>
You can download this application from this link.
https://github.com/harikrishna553/springboot/tree/master/shell/hello-world
Note
a. ShellComponent is a stereo type annotation, itself annotated with @Component annotation.
b. Name of the created bean is customized using value attribute of the annotation.
No comments:
Post a Comment