Springboot项目启动时自动打印访问地址(超简单)

本文介绍了一种在SpringBoot项目启动时自动打印访问地址的方法,通过在启动类中添加特定代码,可以在控制台直接获取项目的本地及外部访问路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot项目启动时自动打印访问地址

在Springboot项目中每次启动完项目,手动在浏览器输入访问地址太麻烦了。在启动类中加入下方代码,就可高效地在控制台中单击URL访问项目了~

在这里插入图片描述
在这里插入图片描述

示例代码:

@SpringBootApplication
@Slf4j
public class WebApplication {
    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(WebApplication.class, args);
        log.info("..######..##.....##..######...######..########..######...######.\n" +
                ".##....##.##.....##.##....##.##....##.##.......##....##.##....##\n" +
                ".##.......##.....##.##.......##.......##.......##.......##......\n" +
                "..######..##.....##.##.......##.......######....######...######.\n" +
                ".......##.##.....##.##.......##.......##.............##.......##\n" +
                ".##....##.##.....##.##....##.##....##.##.......##....##.##....##\n" +
                "..######...#######...######...######..########..######...######.");
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        if (StringUtils.isEmpty(path)) {
            path = "";
        }
        log.info("\n----------------------------------------------------------\n\t" +
                "Application  is running! Access URLs:\n\t" +
                "Local访问网址: \t\thttp://localhost:" + port + path + "\n\t" +
                "External访问网址: \thttp://" + ip + ":" + port + path + "\n\t" +
                "----------------------------------------------------------");
    }

}

关注下方,发送"01"获取更多相关学习教程。

在这里插入图片描述

lombok依赖包:

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>
好的,下面是一个简单的示例代码,用于实现Spring Boot中的日志切面: 首先,我们需要定义一个切面类,并在其中编写切面逻辑: ```java @Aspect @Component public class LoggingAspect { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Pointcut("execution(* com.example.controller..*.*(..))") public void controllerMethod() {} @Around("controllerMethod()") public Object logControllerMethodAccess(ProceedingJoinPoint joinPoint) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String url = request.getRequestURL().toString(); String httpMethod = request.getMethod(); String remoteAddr = request.getRemoteAddr(); Object[] args = joinPoint.getArgs(); StringBuilder params = new StringBuilder(); for (Object arg : args) { params.append(arg.toString()).append(";"); } long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long endTime = System.currentTimeMillis(); logger.info("Request URL: {}, HTTP Method: {}, Remote Address: {}, Parameters: {}, Response Time: {} ms", url, httpMethod, remoteAddr, params.toString(), endTime - startTime); return result; } } ``` 上述代码中,我们使用了`@Aspect`注解来标识这是一个切面类,并使用`@Component`注解将其声明为Spring Bean。接着,我们定义了一个切点`controllerMethod()`,用于匹配所有Controller类中的方法。最后,我们在`controllerMethod()`切点上使用了`@Around`注解,表示这是一个环绕通知,即在Controller方法执行前后都会执行。 在`@Around`注解中,我们首先获取了请求的URL、HTTP Method和远程地址等信息,然后将请求参数转换为字符串,并在执行Controller方法前记录当前时间。在Controller方法执行完成后,我们再记录当前时间并计算出响应时间,最后将所有信息以日志的形式输出到控制台中。 最后,我们需要在Spring Boot的配置文件中开启AOP: ```yaml spring: application: name: demo aop: auto: true ``` 这样,当我们启动Spring Boot应用时,就能够看到所有Controller方法的访问信息被打印在控制台中了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI知识分享

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

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

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

打赏作者

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

抵扣说明:

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

余额充值