首页 > 其他分享 >开发小技巧 - springboot项目启动控制台输出访问地址

开发小技巧 - springboot项目启动控制台输出访问地址

时间:2024-02-04 18:23:45浏览次数:25  
标签:springboot boot springframework 地址 context org import 控制台 String

在Spring Boot项目中,有时我们需要记录或输出访问的地址和IP,以便进行调试、监控或日志记录。以下是如何在Spring Boot中实现这一需求的方法:
  1、编写获取所有访问地址工具类
package com.example.utils;
import cn.hutool.core.util.StrUtil;
import org.springframework.boot.web.context.WebServerApplicationContext;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
/**
 * IP地址工具类
 * @author 李东阳
 */
public class IpAddressUtils {
    /**
     * 获取项目启动的IP地址
     * 注: 仅限springboot项目
     **/
    public static List < String > getIpAddressOfStartUp(WebServerApplicationContext context) {
        List < String > addressList = new ArrayList < > ();
        try {
            Enumeration < NetworkInterface > interfaces = NetworkInterface.getNetworkInterfaces();
            for(NetworkInterface networkInterface: Collections.list(interfaces)) {
                Enumeration < InetAddress > inetAddresses = networkInterface.getInetAddresses();
                for(InetAddress inetAddress: Collections.list(inetAddresses)) {
                    if(!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
                        String address = StrUtil.utf8Str(inetAddress.getHostAddress());
                        int port = context.getWebServer().getPort();
                        String ipAddress = StrUtil.format("http://{}:{}", address, port);
                        addressList.add(ipAddress);
                    }
                }
            }
            return addressList;
        } catch (SocketException e) {
            e.printStackTrace();
            return addressList;
        }
    }
}

2、在启动类中使用

package com.example;
import cn.hutool.core.lang.Console;
import com.example.utils.IpAddressUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.context.annotation.Bean;
import java.util.List;
/**
 * 项目启动器
 * @author lidy
 */
@SpringBootApplication
public class BlogApplication {
    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
    }
    /**
     * 项目启动输出访问地址
     */
    @Bean
    public ApplicationRunner applicationRunner(WebServerApplicationContext context) {
        return (ApplicationArguments args) - > {
            try {
                Console.log("===============项目启动成功 start===============");
                List < String > ipAddressList = IpAddressUtils.getIpAddressOfStartUp(context);
                ipAddressList.forEach(Console::log);
                Console.log("================项目启动成功 end================");
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    }
}
注:本案例中使用到了hutool工具来做控制台输出,需要在pom.xml引入如下如下坐标。
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.22</version>
</dependency>
如果不想引入相关工具,可以使用其他控制台输出方案即可。
3、效果展示:

标签:springboot,boot,springframework,地址,context,org,import,控制台,String
From: https://www.cnblogs.com/ldy731729142/p/18006749

相关文章

  • 不同品牌交换机mac地址与ip地址绑定命令
    华为交换机:使用user-bindstatic命令可以在全局或接口模式下配置ip地址、mac地址和接口的绑定。例如,如果要将192.168.1.100的ip地址和00e0-fc00-0001的mac地址绑定到GigabitEthernet0/0/1接口上,可以使用以下命令:[Switch]user-bindstaticip-address192.168.1.100mac-addres......
  • 虚拟化中的虚拟地址与物理地址的映射——EPT机制
    虚拟化中的虚拟地址与物理地址的映射——EPT机制​ 当secondaryprocessor-basedVM-executioncontrol字段“enableEPT”为1时,启用EPT(ExtendedPageTable,扩展页表)机制​ 开启EPT机制后VMM需要建立EPT页表结构,通过在EPTP(ExtendePageTablePointer)中......
  • SpringBoot连接MySQL
    一、文件结构: 二、实体类packagecom.example.demo.domain;importjava.io.Serializable;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importjavax.persistence.Table;......
  • SpringBoot 整合 RabbitMQ
    Docker搭建RabbitMQ拉取RabbitMQ的镜像执行命令dockerpullrabbitmq:3.7-management执行运行命令dockerrun-d--hostnamerabbit--namerabbit-eRABBITMQ_DEFAULT_USER=admin-eRABBITMQ_DEFAULT_PASS=admin-p15672:15672rabbitmq:3-management打开浏览器访问......
  • Springboot项目发布war到tomcat
    springboot项目有些日子没有开发了,新做一个minspringboot项目,复习下项目开发及发布流程。1.新建项目: 2.新建一个业务controllercontroller名称及方法,名称随意,项目结构如下: testcontroller代码文件的内容如下:packagecom.*****.Controller;importorg.springframewor......
  • SpringBoot-热部署插件添加
      在开发中修改代码避免反复重启编译   <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency> 使用idea为2023.2.3 ......
  • SpringBoot的约定是什么?
    springboot项目中必须在src/main/resources中放入application.yml(yaml,properties)配置文件,名字为applicationspringboot项目中必须在src/main/java中只能有一个启动类......
  • SpringBoot简单集成JWT
    1.JWT入门1.1JWT概念官方网站:https://jwt.io/introduction/JSONWebToken(JWT)是一个定义在RFC7519开放标准下的技术,提供了一种紧凑且自包含的方式用于在各方之间安全地传输信息。JWT使用JSON对象作为载体,同时通过数字签名来验证和确保信息的可信度。数字签名可以通过......
  • SpringBoot + LiteFlow:轻松应对复杂业务逻辑,简直不要太香!
    LiteFlow简介LiteFlow是什么?LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑。通过支持热加载规则配置,开发者能够即时调整流程步骤,将复杂的业务如价格计算、下单流程等拆分为独立且可复用的组件,从而实现系统的高度......
  • C#控制台在同一行显示进度条
    代码如下usingSystem;usingSystem.Threading;classProgram{staticvoidMain(){Console.Write("Progress:");//循环迭代,模拟进度显示for(inti=0;i<=100;i++){//更新控制台中的进度显示......