.NET CORE與Spring Boot編寫控制檯程式應有的優雅姿勢

夢在旅途發表於2019-07-02

本文分別說明.NET CORE與Spring Boot 編寫控制檯程式應有的“正確”方法,以便.NET程式設計師、JAVA程式設計師可以相互學習與加深瞭解,注意本文只介紹用法,不會刻意強調哪種語言或哪種框架寫的控制檯程式要好。

本文所說的編寫控制檯程式應有的“正確”方法,我把正確二字加上引號,因為沒有絕對的正確,因人而異,因系統設計需求而異,我這裡所謂的正確方法是指使用物件導向,依賴注入IOC,切面控制AOP等編碼規範來提升程式的效能、整潔度、可讀性、可維護性等,最終達到讓人感覺有點高大上,有點優雅的樣子

先來說說.NET CORE編寫控制檯程式,目前網路上大把的講解ASP.NET CORE的編寫規範,反而對於.NET CORE控制檯程式編寫規範介紹比較少,大多停留在Hello Word 程式中,而本文則來講講.NET CORE控制檯的編寫規範(應有的優雅姿勢)^ v ^

 如果說不講什麼IOC,DI,AOP等,不講擴充套件性,規範性,全部程式導向(方法)程式設計,那估計沒什麼好講的,因為無非就是定義一個class,然後在class中定義一堆的method(方法),如果在方法中需要使用到其它第三方元件,則直接單獨引用,引用後進行簡單封裝util工具類的靜態方法,甚至也不用封裝,直接使用原生的方法,總之全部都是方法調方法。而這裡所演示的編寫控制檯方法均是儘可能的使用.NET CORE所具有的特性,只有這樣才能體現出.NET CORE框架的優勢,否則普通控制檯程式與.NET CORE控制檯程式有什麼區別。

編寫.NET CORE控制檯程式優雅姿勢一:(直接使用.NET CORE的 IOC、Logging、Config元件)

程式碼如下:

//Program.cs

using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace NetCoreConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //設定config檔案
            var config = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("appSettings.json", optional: true, reloadOnChange: true).Build();

            //設定依賴注入
            var provider = new ServiceCollection()
                                    .AddLogging(configLogging => //設定日誌元件
                                    {
                                        configLogging.SetMinimumLevel(LogLevel.Information);
                                        configLogging.AddConsole();
                                    })
                                   .AddScoped<IConfiguration>(p => config)
                                   .AddScoped<HostService>()
                                   .BuildServiceProvider();

            var hostService = provider.GetService<HostService>();

            hostService.RunAsync();//統一入口服務

            Console.WriteLine("提示:程式已正常啟動執行,按任意鍵停止執行並關閉程式...");
            Console.ReadLine();

        }
    }
}


//HostService.cs
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace NetCoreConsoleApp { public class HostService { private readonly IConfiguration config; private readonly ILogger<HostService> logger; public HostService(IConfiguration config, ILogger<HostService> logger) { this.config = config; this.logger = logger; } public void RunAsync() { Task.Run((Action)Execute); } /// <summary> /// 控制檯核心執行入口方法 /// </summary> private void Execute() { //TODO 業務邏輯程式碼,如下模擬 Stopwatch stopwatch = Stopwatch.StartNew(); for (int i = 1; i <= 100; i++) { Console.WriteLine("test WriteLine:" + i); Thread.Sleep(100); } stopwatch.Stop(); logger.LogInformation("Logging - Execute Elapsed Times:{}ms", stopwatch.ElapsedMilliseconds); } } }

因為要使用.NET CORE相關核心元件,故需要引用相關的NuGet包(引用包的方式有多種方式),而且預設的.NET CORE控制檯只會生成DLL並不會生成EXE啟動程式,故如果僅在WIN系統下使用,還需要設定生成方式等,詳細配置屬性如下:(專案檔案csproj)

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
    <SelfContained>false</SelfContained>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
  </ItemGroup>

</Project>
View Code

 如上程式碼雖簡單但程式碼編寫順序很關鍵,這裡進行說明一下:

1.因為一般應用程式都會有config檔案,故我們需要先通過new ConfigurationBuilder來設定config檔案的方式及路徑;

2.因為要使用.NET CORE預設的IOC框架,故new ServiceCollection,然後將相關的依賴服務元件註冊到IOC容器中;

3.config、logging 均是一個程式最基本的依賴元件,故將其註冊到IOC容器中,註冊logging有專門的擴充套件方法(AddLogging),而config沒有則直接使用通過的註冊方法(當然也可以基於ServiceCollection寫一個AddConfiguration擴充套件方法)

4.控制檯需要一個核心的入口方法,用於處理核心業務,不要直接在Program中寫方法,這樣就不能使用IOC,同時也沒有做到職責分明,Program僅是程式啟動入口,業務處理應該有專門的入口,故上述程式碼中有HostService類(即:核心宿主服務類, 意為存在於控制檯中的服務處理類,在這個類的構造涵數中列出所需依賴的服務元件,以便例項化時IOC可以自動注入這個引數),並註冊到IOC容器中,當然也可以先定義一個IHostService介面然後實現這個介面。(如果有多個HostService類例項,建議定義一個IHostService介面,介面中只需要入口方法定義即可,如:RunAsync)

5.當各元件初始化設定OK、IOC註冊到位後,就應該通過IOC解析獲得HostService類例項,並執行入口方法:RunAsync,該方法為非同步後臺執行,即呼叫該方法後,會在單獨的後臺執行緒處理核心業務,然後主執行緒繼續往下面走,輸出關閉提示資訊,最後的Console.ReadLine();很關鍵,這個是等待輸入流並掛起當前主執行緒,目的大家都知道,不要讓控制檯程式關閉。

 通過上述的講解及原始碼展示,有沒有感覺優雅呢?如果覺得這樣還算優雅,那下面展示的第二種更優雅的姿勢

編寫.NET CORE控制檯程式優雅姿勢二:(使用通用主機也稱泛型主機HostBuilder)

程式碼如下:Program.cs

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using System.IO;
using Polly;
using System;

namespace NetCoreConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureHostConfiguration(configHost =>
                {
                    configHost.SetBasePath(Directory.GetCurrentDirectory());
                })
                .ConfigureAppConfiguration(configApp =>
                {
                    configApp.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                })
                .ConfigureServices((context, services) =>
                {
                    //新增資料訪問元件示例:services.AddTransient<IDbAccesser>(provider =>
                    //{
                    //    string connStr = context.Configuration.GetConnectionString("ConnDbStr");
                    //    return new SqlDapperEasyUtil(connStr);
                    //});

                    //新增HttpClient封裝類示例:services.AddHttpClient<GitHubApiClient>()
                    //.AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(3, t => TimeSpan.FromMilliseconds(800)));

                    services.AddHostedService<DemoHostedService>();
                })
                .ConfigureLogging((context, configLogging) =>
                {
                    configLogging.ClearProviders();
                    configLogging.SetMinimumLevel(LogLevel.Trace);
                    configLogging.AddNLog(context.Configuration);
                })
                .UseConsoleLifetime()
                .Build();

            host.Run();
        }
    }
}

DemoHostedService類程式碼:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace NetCoreConsoleApp
{
    public class DemoHostedService : IHostedService
    {
        private readonly IConfiguration config;
        private readonly ILogger logger;

        public DemoHostedService(IConfiguration config, ILogger<DemoHostedService> logger)
        {
            this.config = config;
            this.logger = logger;
        }

        public Task StartAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine(nameof(DemoHostedService) + "已開始執行...");

            //TODO 業務邏輯程式碼,如下模擬
            Stopwatch stopwatch = Stopwatch.StartNew();
            for (int i = 1; i <= 100; i++)
            {
                Console.WriteLine("test WriteLine:" + i);
                Thread.Sleep(100);
            }

            stopwatch.Stop();

            logger.LogInformation("Logging - Execute Elapsed Times:{}ms", stopwatch.ElapsedMilliseconds);

            return Task.FromResult(0);
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine(nameof(DemoHostedService) + "已被停止");
            return Task.FromResult(0);
        }

    }
}

 

因為要使用HostBuilder類及相關的.NET CORE元件(如上程式碼主要使用到了:Host、Dapper、Nlog、Polly等),故仍需引用相關的NuGet包,詳細配置屬性如下:(專案檔案csproj)

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
    <SelfContained>false</SelfContained>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Dapper" Version="1.60.6" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="2.2.0" />
    <PackageReference Include="NLog.Extensions.Logging" Version="1.5.1" />
    <PackageReference Include="System.Collections.Concurrent" Version="4.3.0" />
  </ItemGroup>

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="nlog.config">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>
View Code

 如上程式碼所示,寫過ASP.NET CORE程式的人可能比較眼熟,這與ASP.NET CORE的寫法很類似,是的,你沒有看錯,HostBuilder是通用主機,是可以廣泛應用於非HTTP的環境下,而ASP.NET CORE中的WebHostBuilder 主要用於HTTP WEB環境,使用方式基本類似,都是先定義HostBuilder,然後利用擴充套件方法註冊、配置各種元件(中介軟體),最後呼叫Host的Run方法,開啟後臺服務執行,不同的是WebHostBuilder多了屬於HTTP專有的一些屬性及方法及其適用的中介軟體。

由於這種寫法比較通用,適用於已熟悉.NET CORE或ASP.NET CORE的人群,上手也較簡單,故建議採取這種方式來寫.NET CORE控制檯程式。需要注意的是HostBuilder中最重要的是:註冊HostedService 服務,如上程式碼中的DemoHostedService即是實現了IHostedService介面的宿主後臺服務類,可以定義多個,然後都註冊到IOC中,最後Host會按註冊先後順序執行多個HostedService服務的StartAsync方法,當停止時同樣會執行多個HostedService服務的StopAsync方法

下面再來看看使用Spring&Spring Boot框架來優雅的編寫控制檯程式 

編寫Spring控制檯程式優雅姿勢一:(只引用所必需的spring jar包、logger jar包,追求極簡風)

使用IDEA +MAVEN 建立一個quickstart 控制檯專案,在maven POM XML中先引用所必需的spring jar包、logger jar包等,配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<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>

    <artifactId>spring-console</artifactId>

    <name>spring-console</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.zuowenjun.cn</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.1.8.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
View Code

然後採取自定義註解類(SpringBeansConfig)的方式註冊相關Bean(包含配置對映類Bean:AppProperties),程式碼如下:

//app.java

package cn.zuowenjun.spring;

import cn.zuowenjun.spring.cn.zuowenjun.spring.services.HostService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringBeansConfig.class);
        HostService hostService = applicationContext.getBean(HostService.class);

        hostService.run();

        applicationContext.registerShutdownHook();

        try {
            System.in.read();
        } catch (IOException e) {
            System.out.println("等待讀取輸入資料包錯:" + e.getMessage() + ",將直接退出程式!");
        }
    }
}


//AppProperties.java

package cn.zuowenjun.spring;

import org.springframework.beans.factory.annotation.Value;


public class AppProperties {

    @Value("${app.name}")
    private String appName;

    @Value("${app.author}")
    private String appAuthor;

    @Value("${app.test.msg}")
    private String testMsg;

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getAppAuthor() {
        return appAuthor;
    }

    public void setAppAuthor(String appAuthor) {
        this.appAuthor = appAuthor;
    }

    public String getTestMsg() {
        return testMsg;
    }

    public void setTestMsg(String testMsg) {
        this.testMsg = testMsg;
    }
}


//SpringBeansConfig.java

package cn.zuowenjun.spring;

import cn.zuowenjun.spring.cn.zuowenjun.spring.services.HostService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;

@Configuration
@PropertySource(value = "classpath:app.properties", ignoreResourceNotFound = false)
public class SpringBeansConfig {

    @Bean
    @Order(1)
    public HostService hostService() {
        return new HostService();
    }

    @Bean
    @Order(0)
    @Scope("singleton")
    public AppProperties appProperties() {
        return new AppProperties();
    }

    //註冊其它所需Bean...
}


//HostService.java

package cn.zuowenjun.spring.cn.zuowenjun.spring.services;

import cn.zuowenjun.spring.AppProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;

import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class HostService {

    private static final Logger LOGGER = LoggerFactory.getLogger(HostService.class);

    @Autowired
    private AppProperties appProperties;

    //可以新增其它屬性注入

    public void run() {
//        ExecutorService pool = Executors.newSingleThreadExecutor();
//        pool.execute(() -> execute());

        new Thread(this::execute).start();
    }

    /// <summary>
    /// 控制檯核心執行入口方法
    /// </summary>
    private void execute() {
        //TODO 業務邏輯程式碼,如下模擬
        StopWatch stopwatch = new StopWatch();
        stopwatch.start();
        for (int i = 1; i <= 100; i++) {
            System.out.println("test WriteLine:" + i);
            try {
                Thread.sleep(100);
            } catch (Exception e) {
            }
        }
        stopwatch.stop();

        System.out.println(String.join("", Collections.nCopies(30, "=")));

        System.out.printf("app name is:%s %n", appProperties.getAppName());
        System.out.printf("app author is:%s %n", appProperties.getAppAuthor());
        System.out.printf("app test msg:%s %n", appProperties.getTestMsg());

        LOGGER.info("Logging - Execute Elapsed Times:{}ms", stopwatch.getTotalTimeMillis());
    }
}

 app.properties配置檔案內容如下,注意應放在classpth目錄下(即:resources目錄下,沒有需自行建立並設為resources目錄):

app.name=demo spring console
app.author=zuowenjun
app.test.msg=hello java spring console app!

 如上即上實現一個spring的控制檯程式,當然由於是示例,故只引用了logger包,正常還需引用jdbc或ORM框架的相關jar包, 上述程式碼關鍵邏輯說明(同樣要注意順序):

1.new AnnotationConfigApplicationContext類(spring IOC容器),建立一個IOC容器,類似.NET CORE中的ServiceProvider類;

2.定義 SpringBeansConfig bean註冊配置類(註冊相關依賴),這個類中依次注入相關的bean,如果bean之間有依賴順序關係,建議新增@Order並指明序號;該類作為AnnotationConfigApplicationContext的建構函式引數傳入,以便IOC自動解析並完成實際註冊;

3.同樣是定義一個HostService 宿主服務類,並實現run方法邏輯,一般採取後臺執行緒非同步執行,為了演示效果與.NET CORE的HostService 類相同,示例邏輯基本相同。另外還定義了AppProperties配置對映類,便於直接讀取配置,.NET CORE同樣也有類似註冊bind到配置類中,然後在服務類中使用:IOptions<配置類>作為建構函式引數實現建構函式注入。只是由於篇幅有限故.NET CORE部份直接採取了注入IConfiguration,大家有興趣可以檢視網上相關資料。

4.IOC容器初始化並註冊成功後,即可解析HostService 類獲得例項,執行run方法,run方法會開啟執行緒在後臺處理,並返回到主執行緒,直至in.read()阻塞掛起主執行緒,防止程式自動關閉。

編寫Spring boot控制檯程式優雅姿勢二:(引用spring boot jar包)

 使用IDEA+Spring Initializr來建立一個spring boot專案,建立過程中按需選擇依賴的框架,我這裡是示例,故除了預設spring-boot-starter依賴外,其餘什麼依賴都不新增,建立後Maven POM XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.zuowenjun.spring</groupId>
    <artifactId>springboot-console</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-console</name>
    <description>Demo project for Spring Boot</description>
    <url>http://www.zuowenjun.cn</url>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
View Code

然後建立相關的Bean類:HostService(宿主服務類,這個與前文定義類均相同)、AppProperties(配置對映類,這個是對映預設的application.properties配置檔案,注意這裡的對映方式與前文所描述稍有不周,採用:@ConfigurationProperties+屬性對映,無需加@Value註解,對映屬性時如果有-則應寫成駝峰式,如果有.則應定義內部靜態類,呈現層級屬性完成對映,具體的用法可以參見我之前的文章):

//HostService.java

package cn.zuowenjun.spring.services;

import cn.zuowenjun.spring.AppProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.util.Collections;

@Component
public class HostService {

    private static final Logger LOGGER = LoggerFactory.getLogger(HostService.class);

    @Autowired
    private AppProperties appProperties;

    //可以新增其它屬性注入

    public void run() {
//        ExecutorService pool = Executors.newSingleThreadExecutor();
//        pool.execute(() -> execute());

        new Thread(this::execute).start();
    }

    /// <summary>
    /// 控制檯核心執行入口方法
    /// </summary>
    private void execute() {
        //TODO 業務邏輯程式碼,如下模擬
        StopWatch stopwatch = new StopWatch();
        stopwatch.start();
        for (int i = 1; i <= 100; i++) {
            System.out.println("test WriteLine:" + i);
            try {
                Thread.sleep(100);
            } catch (Exception e) {
            }
        }
        stopwatch.stop();

        System.out.println(String.join("", Collections.nCopies(30, "=")));

        System.out.printf("app name is:%s %n", appProperties.getName());
        System.out.printf("app author is:%s %n", appProperties.getAuthor());
        System.out.printf("app test msg:%s %n", appProperties.getTestMsg());

        LOGGER.info("Logging - Execute Elapsed Times:{}ms", stopwatch.getTotalTimeMillis());
    }
}


//AppProperties.java

package cn.zuowenjun.spring;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {

    private String name;

    private String author;

    private String testMsg;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTestMsg() {
        return testMsg;
    }

    public void setTestMsg(String testMsg) {
        this.testMsg = testMsg;
    }
}

application.properties配置檔案:(注意app.test.msg此處改為了app.test-msg,因為這樣就可以直接對映到類的屬性中,否則得定義內部類有點麻煩)

app.name=demo spring console
app.author=zuowenjun
app.test-msg=hello java spring console app!

 最後改造spring boot application類,讓SpringbootConsoleApplication類實現ApplicationRunner介面,並在run方法中編寫通過屬性依賴注入獲得HostService類的例項,最後執行HostService的run方法即可,程式碼如下:

package cn.zuowenjun.spring;

import cn.zuowenjun.spring.services.HostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootConsoleApplication implements ApplicationRunner {

    @Autowired
    private HostService hostService;

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

    @Override
    public void run(ApplicationArguments args) throws Exception {
        hostService.run();
    }
}

 如上步驟即完成了優雅編寫spring boot控制檯程式,關鍵點是ApplicationRunner,這個是給spring boot執行的入口,另一種思路,我們其實還可以把HostService類改造一下,讓其實現ApplicationRunner介面,那麼run方法即為spring boot的啟動入口。

總結一下:.

NET CORE控制檯程式優雅姿勢一與Spring控制檯優雅姿勢一核心思想是一樣的,都是手動建立各個依賴元件及IOC容器的例項,都是通過IOC容器顯式的解析獲得HostService類的例項,最後執行HostService#run方法。

NET CORE控制檯程式優雅姿勢二與Spring控制檯優雅姿勢二核心思想也是一樣的,都是利用IOC容器來直接管理註冊的各個依賴元件,並由.NET CORE、Spring boot框架自行排程HostService#run方法。

我個人更傾向優雅姿勢二的方法來編寫.NET CORE或Spring Boot的控制檯程式,因為寫得更少,做得更多。

 

 

 

相關文章