SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动)

简介: SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动)

一,SpringBoot中读取配置文件的常用方法

1.1,使用@Value读取

在springBoot声明周期内,可以使用@Value注解从SpringBoot的默认配置文件中读取配置信息

例如在Controller中使用:

    // 在配置文件中读取属性名为web.images-path对应的值
    @Value("${web.images-path}")
    private String path;

@Value可以放到属性或方法上,能够正常使用的前提是所在类,必须在SpringBoot的生命周期内。

我们怎么把一个类放到Spring的生命周期中进行管理?使用的是@Component注解

因为@Controller和@Service本身就包含@Component。所以可以直接使用。

下面是单独使用@Component的例子

创建一个config包,然后创建一个BootProperties

package com.demo.config;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
@Component
public class BootProperties {
    @Value("${web.images-path}")
    public String path;
}

然后在controller中写

@RestController
public class HelloController {
    @Autowired
    private BootProperties bootProperties;
    @RequestMapping("/test5")
    public Object test5(){
        return bootProperties.path;
    }
}


使用ing类型写

package com.demo.config;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
@Component
public class BootProperties {
    @Value("${web.images-path}")
    public String path;
  @Value("${server.port}")
    public int port;
}
@RestController
public class HelloController {
    @Autowired
    private BootProperties bootProperties;
    @RequestMapping("/test5")
    public Object test5(){
      return bootProperties.path +  " ------ "+ bootProperties.port;
    }
}

1.2,使用@ConfigurationProperties

BootProperties类

package com.demo.config;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
@Setter  // lombok,生成set方法
@ConfigurationProperties(prefix = "server") // 配置属性类,并定制前缀
@Component  // 因为@ConfigurationProperties不能把此类放到boot容器中,所以要配合@Componpent使用
public class BootProperties {
    @Value("${web.images-path}")
    public String path;
    // 不使用@Value注解,需要保证:前缀+属性名=全路径。还需要此属性有对应的setter方法
    // @Value("${server.port}")
    public int port;
    // 使用@Value注解则需要写全路径
}

controller类

@RestController
public class HelloController {
    @Autowired
    private BootProperties bootProperties;
    @RequestMapping("/test5")
    public Object test5(){
      return bootProperties.path +  " ------ "+ bootProperties.port;
    }
}

1.3,使用Environment

Environment是SpringCore中的一个用于读取配置文件的类,将此类使用@Autowired注入到类中就可以使用它的getProperty方法来获取某个配置项的值

@RestController
public class HelloController {
    @Autowired
    private Environment environment;
    @RequestMapping("/test7")
    public Object test7(){
        return environment.getProperty("server.port");
    }
}

1.4,自定义配置文件读取

使用之前的知识来理解下面的代码。

主要添加新的注解@PropertySource

创建一个config包,然后创建一个SysProperties

package com.demo.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "sys")
@Component
@PropertySource("classpath:sys.properties")
@Getter
@Setter
public class SysProperties {
    private String param1;
    private String param2;
}

controller类

@RestController
public class HelloController {
    @Autowired
    private SysProperties sysProperties;
    @RequestMapping("/test6")
    public Object test6(){
        return sysProperties.getParam1()+sysProperties.getParam2();
    }
}

二,SpringBoot部署war项目到tomcat9和启动原理

创建一个新项目

在添加个模块

然后在pom中添加依赖

    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>passerby-war</finalName>
        <plugins>
            <plugin>
                <!-- 打包插件 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

然后添加controller类和一个启动类

然后随便在Controller类里面加个方法

package com.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
    @RequestMapping("/demo01")
    public Object demo01(){
        return "hello,war";
    }
}

开始打包

在文件夹中找到这个位置

找到后把刚打的war包复制下来

然后在到你Tomcat的位置

把war包复制到里面

然后打开bin目录

在里面找到startup.bat这个

打开等他运行完

然后在打开你刚才把war包粘贴的那个文件夹

现在就好了,打开浏览器试试


有什么不理解的可以私信!!!


相关文章
|
11天前
|
XML Java 应用服务中间件
SpringBoot项目打war包流程
本文介绍了将Spring Boot项目改造为WAR包并部署到外部Tomcat服务器的步骤。主要内容包括:1) 修改pom.xml中的打包方式为WAR;2) 排除Spring Boot内置的Tomcat依赖;3) 添加Servlet API依赖;4) 改造启动类以支持WAR部署;5) 打包和部署。通过这些步骤,可以轻松地将Spring Boot应用转换为适合外部Tomcat服务器的WAR包。
102 64
SpringBoot项目打war包流程
|
3月前
|
Java 应用服务中间件 Maven
如何将 Spring Boot 应用程序部署为 WAR?
如何将 Spring Boot 应用程序部署为 WAR?
184 1
|
6天前
|
监控 Java 应用服务中间件
SpringBoot是如何简化Spring开发的,以及SpringBoot的特性以及源码分析
Spring Boot 通过简化配置、自动配置和嵌入式服务器等特性,大大简化了 Spring 应用的开发过程。它通过提供一系列 `starter` 依赖和开箱即用的默认配置,使开发者能够更专注于业务逻辑而非繁琐的配置。Spring Boot 的自动配置机制和强大的 Actuator 功能进一步提升了开发效率和应用的可维护性。通过对其源码的分析,可以更深入地理解其内部工作机制,从而更好地利用其特性进行开发。
20 6
|
16天前
|
Java 测试技术 应用服务中间件
Spring Boot 配置文件总结
Spring Boot 提供全局配置文件 `application.properties` 和 `application.yml`,用于修改自动配置的默认值。前者使用键值对配置,后者使用缩进和冒号。不同环境(开发、测试、生产)可切换配置文件,通过 `spring.profiles.active` 指定。例如,开发环境端口为4790,测试环境为4791,生产环境为4792。配置示例展示了属性、List、Map定义及引用方法。
52 14
|
19天前
|
缓存 安全 Java
Spring Boot 3 集成 Spring Security + JWT
本文详细介绍了如何使用Spring Boot 3和Spring Security集成JWT,实现前后端分离的安全认证概述了从入门到引入数据库,再到使用JWT的完整流程。列举了项目中用到的关键依赖,如MyBatis-Plus、Hutool等。简要提及了系统配置表、部门表、字典表等表结构。使用Hutool-jwt工具类进行JWT校验。配置忽略路径、禁用CSRF、添加JWT校验过滤器等。实现登录接口,返回token等信息。
202 12
|
2月前
|
XML Java 数据格式
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
本文介绍了在使用Spring框架时,如何通过创建`applicationContext.xml`配置文件来管理对象。首先,在resources目录下新建XML配置文件,并通过IDEA自动生成部分配置。为完善配置,特别是添加AOP支持,可以通过IDEA的Live Templates功能自定义XML模板。具体步骤包括:连续按两次Shift搜索Live Templates,配置模板内容,输入特定前缀(如spring)并按Tab键即可快速生成完整的Spring配置文件。这样可以大大提高开发效率,减少重复工作。
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
|
25天前
|
存储 安全 Java
Spring Boot 3 集成Spring AOP实现系统日志记录
本文介绍了如何在Spring Boot 3中集成Spring AOP实现系统日志记录功能。通过定义`SysLog`注解和配置相应的AOP切面,可以在方法执行前后自动记录日志信息,包括操作的开始时间、结束时间、请求参数、返回结果、异常信息等,并将这些信息保存到数据库中。此外,还使用了`ThreadLocal`变量来存储每个线程独立的日志数据,确保线程安全。文中还展示了项目实战中的部分代码片段,以及基于Spring Boot 3 + Vue 3构建的快速开发框架的简介与内置功能列表。此框架结合了当前主流技术栈,提供了用户管理、权限控制、接口文档自动生成等多项实用特性。
72 8
|
2月前
|
缓存 前端开发 Java
【Spring】——SpringBoot项目创建
SpringBoot项目创建,SpringBootApplication启动类,target文件,web服务器,tomcat,访问服务器
|
4月前
|
Java API Spring
在 Spring 配置文件中配置 Filter 的步骤
【10月更文挑战第21天】在 Spring 配置文件中配置 Filter 是实现请求过滤的重要手段。通过合理的配置,可以灵活地对请求进行处理,满足各种应用需求。还可以根据具体的项目要求和实际情况,进一步深入研究和优化 Filter 的配置,以提高应用的性能和安全性。
|
3月前
|
监控 Java 数据库连接
详解Spring Batch:在Spring Boot中实现高效批处理
详解Spring Batch:在Spring Boot中实现高效批处理
397 12