一,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;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/02c8f5f3104b4d11b9af3ce41073ed69.png)
使用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;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/18c1e3321cec4cf8a868389011208f06.png)
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;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/b74b13ab909746fcbf18fe7d79d669c5.png)
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");
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/d5e72812a80f4334b131fbaa09ad7bf6.png)
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();
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/6402159094ae41198bb3fb1cf2b4ef38.png)
二,SpringBoot部署war项目到tomcat9和启动原理
创建一个新项目
![在这里插入图片描述](https://img-blog.csdnimg.cn/7022bfe6e99640368747abb4bf2d7562.png)
在添加个模块
![在这里插入图片描述](https://img-blog.csdnimg.cn/b87bb1907f624878918c187eab24aab6.png)
然后在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类和一个启动类
![在这里插入图片描述](https://img-blog.csdnimg.cn/6a83470a1c1544ae93cab0ac7c935dde.png)
然后随便在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";
}
}
开始打包
![在这里插入图片描述](https://img-blog.csdnimg.cn/68766f7388474b70ab276a8b9482c6e0.png)
在文件夹中找到这个位置
![在这里插入图片描述](https://img-blog.csdnimg.cn/d1185e4560ad465481cc61f70ae10874.png)
找到后把刚打的war包复制下来
![在这里插入图片描述](https://img-blog.csdnimg.cn/34f415f6bc7140e2880bb1ce744f6818.png)
然后在到你Tomcat的位置
![在这里插入图片描述](https://img-blog.csdnimg.cn/22e36e4d54a7451daa0dfd1c41bef708.png)
把war包复制到里面
![在这里插入图片描述](https://img-blog.csdnimg.cn/73320d14f05d47d7a6ec2d8168272d8f.png)
然后打开bin目录
![在这里插入图片描述](https://img-blog.csdnimg.cn/53549a24a49442b7af1735a98c273e9c.png)
在里面找到startup.bat这个
![在这里插入图片描述](https://img-blog.csdnimg.cn/ab0b7a99bab14ccbb311d9fbd3f6ca66.png)
打开等他运行完
![在这里插入图片描述](https://img-blog.csdnimg.cn/5f4826a791bc47d49ba951365cbb82e8.png)
然后在打开你刚才把war包粘贴的那个文件夹
![在这里插入图片描述](https://img-blog.csdnimg.cn/a39099e03d66491893458aa6dccabac1.png)
现在就好了,打开浏览器试试
![在这里插入图片描述](https://img-blog.csdnimg.cn/9adebce1619a4cddaf4b28eeb16fe863.png)
有什么不理解的可以私信!!!