SpringBoot 创建非web工程——2种实现方法

SpringBoot Java工程

创建一个SpringBoot项目,但不是web工程,哦~ 好像有点多余,,,好奇心驱使着写完了,没有web怎么调用接口的实现类呢,一起期待一波~

1. 第一种实现——在启动类中获取SpringBoot容器ConfigurableApplicationContext

1.1 引入依赖

pom.xml

<dependencies>
  <!--SpringBoot框架非web应用 起步依赖-->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter</artifactId>
   </dependency>
   <!--SpringBoot框架测试起步依赖-->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
       <exclusions>
           <exclusion>
               <groupId>org.junit.vintage</groupId>
               <artifactId>junit-vintage-engine</artifactId>
           </exclusion>
       </exclusions>
   </dependency>
</dependencies>

<build>
   <plugins>
       <!--SpringBoot框架编译打包插件-->
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>3.8.1</version>
           <configuration>
               <source>1.8</source>
               <target>1.8</target>
               <encoding>UTF-8</encoding>
           </configuration>
       </plugin>
   </plugins>
</build>

1.2 创建接口

StudentService.java

package com.guo.springboot.service;
public interface StudentService {
    String say();
}

1.3 实现接口

StudentServiceImpl.java

package com.guo.springboot.service.impl;
import com.guo.springboot.service.StudentService;
import org.springframework.stereotype.Service;
@Service  //创建对象交给Spring容器管理
public class StudentServiceImpl implements StudentService {
    @Override
    public String say() {
        return "HelloWord!";
    }
}

1.4 核心启动类

Application.java

package com.guo.springboot;

import com.guo.springboot.service.StudentService;
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
         */
        //获取SpringBoot容器
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
        //从spring容器中获取指定bean对象
        StudentService studentService = (StudentService) applicationContext.getBean("studentServiceImpl");
        //调用业务方法
        String say = studentService.say();
        System.out.println(say);
    }
}

1.5 运行结果

在这里插入图片描述

2. 第二种实现——在启动类实现CommandLineRunner接口,重写run()方法

2.1引入依赖

同第一种

2.2 创建接口

同第一种

2.3 实现接口

同第一种

2.4 核心启动类

核心启动类实现 CommandLineRunner接口 重写run()方法

package com.guo.springboot;

import com.guo.springboot.service.StudentService;
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 Application implements CommandLineRunner {
    @Autowired
    private StudentService studentService;

    public static void main(String[] args) {
        //SpringBoot启动程序,会初始化Spring容器
        SpringApplication.run(Application.class, args);
    }
    //重写CommandLineRunner类中的run方法
    @Override
    public void run(String... args) throws Exception {
        //调用业务方法
        String say = studentService.say();
        System.out.println(say);
    }
}

2.5 运行结果

在这里插入图片描述

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

848698119

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值