码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SpringBoot 创建非web工程——2种实现方法


    SpringBoot 创建非web工程

    • SpringBoot Java工程
      • 1. 第一种实现——在启动类中获取SpringBoot容器ConfigurableApplicationContext
        • 1.1 引入依赖
        • 1.2 创建接口
        • 1.3 实现接口
        • 1.4 核心启动类
        • 1.5 运行结果
      • 2. 第二种实现——在启动类实现CommandLineRunner接口,重写run()方法
        • 2.1引入依赖
        • 2.2 创建接口
        • 2.3 实现接口
        • 2.4 核心启动类
        • 2.5 运行结果

    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 运行结果

    在这里插入图片描述

  • 相关阅读:
    重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
    华为新品Mate50将搭载北三短报文通信功能?这项技术是何方神圣
    【黑马程序员pinik名师讲html】HTML很容易忘记?有它我不慌的
    卷积神经网络信号处理,卷积神经网络应用领域
    内网渗透神器CobaltStrike之会话管理(五)
    备战数学建模39-粒子群算法pso进阶应用番外篇2(攻坚战3)
    一文说尽零售数据分析指标体系
    Apollo 应用与源码分析:CyberRT-工具与命令
    【Vue五分钟】 Vue Cli脚手架安装配置
    计算机毕业设计Java家乡旅游文化推广网站(源码+系统+mysql数据库+lw文档)
  • 原文地址:https://blog.csdn.net/qq_45896330/article/details/126069265
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号