SpringBoot非web应用程序 - 极悦

SpringBoot教程

SpringBoot非web应用程序

在Spring Boot框架中,要创建一个非Web应用程序(纯Java程序),有两种方式。

方式一:直接在main方法中,根据SpringApplication.run()方法获取返回的Spring容器对象,再获取业务bean进行调用

项目名称:025-springboot-java-01

1.创建一个SpringBoot Module

2.创建一个演示UserService接口及实现类

UserService.java接口


package com.abc.springboot.service;

/**
 * ClassName:UserService
 * Package:com.abc.springboot.service
 * Description:<br/>
 */
public interface UserService {

    String sayHello();
}

UserServiceImpl.java接口实现类


package com.abc.springboot.service.impl;

import com.abc.springboot.service.UserService;
import org.springframework.stereotype.Service;

/**
 * ClassName:UserServiceImpl
 * Package:com.abc.springboot.service.impl
 * Description:<br/>
 */
@Service
public class UserServiceImpl implements UserService {

    @Override
    public String sayHello() {
        return "Hello,SpringBoot Java!";
    }
}

3.在Application类的main方法中,获取容器,调用业务bean


package com.abc.springboot;

import com.abc.springboot.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        /**
         * SpringBoot程序启动后,返回值是ConfigurableApplicationContext,它也是一个Spring容器对象
         * 它其它相当于原来Spring中启动容器ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("");
         */

        //获取SpringBoot程序启动后的Spring容器
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

        //从Spring容器中获取指定bean的对象
        UserService userService = (UserService) context.getBean("userServiceImpl");

        //调用业务bean的方法
        String sayHello = userService.sayHello();

        System.out.println(sayHello);
    }
}

方式二:Spring boot 的入口类实现CommandLineRunner接口

项目名称:026-springboot-java-02

创建一个UserService接口及接口实现类

UserService.java接口类


package com.abc.springboot.service;

/**
 * ClassName:UserService
 * Package:com.abc.springboot.service
 * Description:

 */
public interface UserService {

    String sayHello();
}

UserServiceImpl.java


package com.abc.springboot.service.impl;

import com.abc.springboot.service.UserService;
import org.springframework.stereotype.Service;

/**
 * ClassName:UserServiceImpl
 * Package:com.abc.springboot.service.impl
 * Description:

 */
@Service
public class UserServiceImpl implements UserService {

    @Override
    public String sayHello() {
        return "Hello,SpringBoot Java!";
    }
}

将Application类的@SpringBootApplication注解注释掉,复制一个新的Application取名为Application2,实现CommandLineRunner接口


package com.abc.springboot;

import com.abc.springboot.service.UserService;
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 Application2 implements CommandLineRunner {


    //第二步:通过容器获取bean,并注入给userService
    @Autowired
    private UserService userService;


    public static void main(String[] args) {
        //第一步:SpringBoot的启动程序,会初始化spring容器
        SpringApplication.run(Application2.class,args);
    }

    //覆盖接口中的run方法
    @Override
    public void run(String... args) throws Exception {

        //第三步:容器启动后调用run方法,在该方法中调用业务方法
        String sayHello = userService.sayHello();

        System.out.println(sayHello);

    }
}

小Tip

1.关闭SpringBoot Logo图标及启动日志

项目名称:027-springboot-logo-01


package com.abc.springboot;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication springApplication = new SpringApplication(Application.class);

        //关闭启动logo和启动日志的输出
        springApplication.setBannerMode(Banner.Mode.OFF);
        springApplication.run(args);

    }

}

 

修改完毕后,运行程序查看效果

2.修改启动的logo图标

项目名称:028-springboot-logo-02

修改前LOGO

在src/main/resources放入banner.txt文件,该文件名字不能随意,文件中的内容就是要输出的logo;

可以利用网站生成图标:http://patorjk.com/software/taag/,将生成好的图标文字粘贴到banner.txt文件中,然后将关闭logo输出的语句注释,启动看效果。

修改后LOGO

技术文档推荐

更多>>

视频教程推荐

更多>>