Spring boot 非web版(jar)入门配置程序-maven工程源码

摘要: 前面尝试了用spring boot 搭建入门的web, war 包工程,内置了一个tomcat。今天尝试直接用spring boot 实现一个jar工程,可以直接执行的 jar 工程,注意是非 web 工程,也就是普通的可以执行的jar ,后面可以跟参数,说白了就是通常用得很多的命令行模式,spring boot 对这个也有很好的支持, 废话就不多说了,码农就直接上代码了

前面尝试了用spring boot 搭建入门的web, war 包工程,内置了一个tomcat。今天尝试直接用spring boot 实现一个jar工程,可以直接执行的 jar 工程,注意是非 web 工程,也就是普通的可以执行的jar ,后面可以跟参数,说白了就是通常用得很多的命令行模式,spring boot 对这个也有很好的支持, 废话就不多说了,码农就直接上代码了。这个代码是基于上一个程序拷贝过来改动的。可以参考原来的web版:http://www.yihaomen.com/article/java/649.htm


1. Maven配置文件变化


  4.0.0
  com.yihaomen
  SpringBoot-002
  jar
  0.0.1-SNAPSHOT
  SpringBoot-002 Non-Web Appliation
  http://maven.apache.org
  
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
    

    
        1.8
    

    

        
            org.springframework.boot
            spring-boot-starter
        

    

    
        
            
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
  



2. 实现CommandLineRunner接口并实现run方法, 这是重点。
package com.yihaomen.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.yihaomen.springboot.service.MyCommandService;

@SpringBootApplication
public class Application implements CommandLineRunner {

	@Autowired
    private MyCommandService commandService;

    public static void main(String[] args) throws Exception {

        //不想看见 spring的logo
        SpringApplication app = new SpringApplication(Application.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }

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

        if (args.length > 0 ) {
            System.out.println(commandService.getMessage(args[0].toString()));
        }else{
            System.out.println(commandService.getMessage());
        }

        System.exit(0);
    }

}


3. 自己随便写个service , 表示来处理各种命令。
package com.yihaomen.springboot.service;

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


@Service
public class MyCommandService {
	
	@Value("${name:unknown}")
    private String name;

    public String getMessage() {
        return getMessage(name);
    }

    public String getMessage(String name) {
        return "Hello " + name;
    }

}



4. 还想强调一点的是,如果在cmd命令下执行时,如果想去掉一些多余的日志,那就一定要配置logback了,这里用了logback, log4j的姊妹。 配置后放在 resource 目录下,logback.xml.



    

    
        
            
                %d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
            
        
    

    
        
    

    
        
    

    
        
    




5. 打包,直接用maven , 到工程目录下执行 mvn clean package. 打包之后,可以直接执行.



6. 源码下载 maven 版本.

spring boot non web application,jar application

上一篇: Spring boot jsp版入门配置程序-maven工程源码
下一篇: JS拖动选择 table 里的单元格
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号