基于spring boot编写控制台命令行程序

spring boot除了可以开发web程序以外,还可以开发控制台程序。对于不需要提供web服务,只需要提供控制台命令行服务的程序,可以实现一个命令行接口的方式,提供控制台程序。

可以减少不必要的包依赖,减小最终的jar大小,也降低内存的占用。

1. pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.3.11.RELEASE</version>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-web</artifactId>-->
<!--            <version>2.3.11.RELEASE</version>-->
<!--        </dependency>-->


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.1.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

这里需要增加一个execution配置,不然打包不完整。 

2. 主程序代码

package com.platform.console;

import com.platform.console.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class ConsoleApplication implements CommandLineRunner {

    @Autowired
    DemoService demoService;

    public static void main(String[] args) {
        SpringApplication.run(ConsoleApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {

        demoService.hello();

        System.out.println("程序实际上的入口在这里。");

    }
}

可以看到,主程序实现了一个控制台接口。 

3. 服务代码

package com.platform.console.service;

import org.springframework.stereotype.Service;

@Service
public class DemoService {

    public void hello() throws InterruptedException {

        while (true)
        {
            System.out.println("hello");

            Thread.sleep(3000);
        }

    }

}

这里定义的服务,是 

 4. 打包后运行

java -jar spring-boot-console-1.0.jar

hello
hello
hello
hello

可以看到,启动运行方法,和web程序的启动运行方法一样。

示例代码下载

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Framework 并不仅仅适用于 Web 应用程序,它也可以用于控制台应用程序。下面是一个简单的示例,演示如何使用 Spring Framework 编写控制台应用程序。 1. 首先,您需要创建一个新的 Maven 项目,并添加 Spring Framework 的相关依赖。在 `pom.xml` 文件中添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> </dependencies> ``` 2. 创建一个简单的 Java 类,这个类将是您的控制台应用程序的入口点。在这个类中,您需要创建一个 Spring 应用程序上下文,加载您的配置文件,并获取您需要的 bean 实例。 ```java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ConsoleApplication { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); MyService service = context.getBean(MyService.class); service.doSomething(); } } ``` 3. 创建一个 Spring 配置文件,这个文件将描述您的应用程序的组件和依赖关系。在这个文件中,您需要定义您的 bean 实例。 ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myService" class="com.example.MyService"> <!-- 添加其他属性依赖关系 --> </bean> </beans> ``` 在上面的示例中,我们定义了一个名为 `myService` 的 bean,它是一个 `MyService` 类型的实例。您可以为这个 bean 添加其他属性依赖关系,例如其他的 bean 引用和值类型属性。 4. 创建您的服务类。这是一个简单的示例: ```java public class MyService { public void doSomething() { System.out.println("Hello, world!"); } } ``` 在上面的示例中,我们定义了一个 `MyService` 类,它有一个方法 `doSomething()`,它将输出一条简单的消息。 5. 最后,您可以在控制台中运行您的应用程序。如果一切正常,应该会输出 "Hello, world!"。 这是一个简单的示例,演示如何使用 Spring Framework 编写控制台应用程序。当然,实际应用程序可能更加复杂,但基本的原理是相同的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值