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


    SpringBoot Java工程

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

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

    1.1 引入依赖

    pom.xml

    <dependencies>
      
       <dependency>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-starterartifactId>
       dependency>
       
       <dependency>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-starter-testartifactId>
           <scope>testscope>
           <exclusions>
               <exclusion>
                   <groupId>org.junit.vintagegroupId>
                   <artifactId>junit-vintage-engineartifactId>
               exclusion>
           exclusions>
       dependency>
    dependencies>
    
    <build>
       <plugins>
           
           <plugin>
               <groupId>org.apache.maven.pluginsgroupId>
               <artifactId>maven-compiler-pluginartifactId>
               <version>3.8.1version>
               <configuration>
                   <source>1.8source>
                   <target>1.8target>
                   <encoding>UTF-8encoding>
               configuration>
           plugin>
       plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    1.2 创建接口

    StudentService.java

    package com.guo.springboot.service;
    public interface StudentService {
        String say();
    }
    
    • 1
    • 2
    • 3
    • 4

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    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);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    2.5 运行结果

    在这里插入图片描述

  • 相关阅读:
    vue+elementUI el-table实现单选
    【Halcon边界轮廓提取与圆弧段拟合】
    Scratch软件编程等级考试一级——20220320
    全文检索&ElasticSearch简介
    使用Qt Designer为您的Qt for Python项目创建基于Qt Widgets的图形界面的两种方法
    微信小程序毕业设计题目计算机维修服务+后台管理系统|前后分离VUE.js
    【图像处理-计算机视觉学习路线】个人记录
    latex近日问题集锦
    数据结构-链表(java)
    Golang 中的 String、rune 和 byte
  • 原文地址:https://blog.csdn.net/qq_45896330/article/details/126069265