1. SpringBoot配置文件
1.1. 配置端口号和路径
(1)在application.properties文件下配置端口号和路径。
server.port: 8081
server.servlet.context-path=/demo
(2)运行访问。
1.2. 自定义配置
1.2.1. 方式一
(1)在application.properties文件下添加配置。
wechat.appid: wx7874d0c23d07e2aa
wechat.token: education
wechat.appSecret: e03f5ab03906d4d943675c233d4c3cb3
(2)创建config文件夹和WeChat实体类
package com.zzs.szyj.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@ConfigurationProperties("wechat")
@Component
public class WeChat {private String appId;private String token;private String appSecret;private String port;public String getPort() {return port;}public void setPort(String port) {this.port = port;}public String getAppId() {return appId;}public void setAppId(String appId) {this.appId = appId;}public String getToken() {return token;}public void setToken(String token) {this.token = token;}public String getAppSecret() {return appSecret;}public void setAppSecret(String appSecret) {this.appSecret = appSecret;}
}
(3)在HelloController编写新接口
package com.zzs.szyj;import com.zzs.szyj.config.WeChat;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
// @Controller + @ResponseBody
public class HelloController {@Resource//@Autowiredprivate WeChat weChat;@RequestMapping("/hello")public String hello() {return "hello Spring Boot";}@GetMapping("/getWeChat")public WeChat getWeChat() {return weChat;}
}
(4)重启服务
(5)访问接口
1.2.2. 方式二
(1)创建config文件夹和WeChatConfig类
@Configuration // 标识这是一个配置类
public class WeChatConfig {@Bean@ConfigurationProperties("wechat")public WeChat weChat() {return new WeChat();}}
(2)重启服务,访问接口
1.2.3. 方式三
(1)修改HelloController类
package com.zzs.szyj;
import com.zzs.szyj.config.WeChat;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
// @Controller + @ResponseBody
public class HelloController {// @Resource
// //@Autowired
// private WeChat weChat;@Value("${wechat.appId}")private String appId;@Value("${wechat.token}")private String token;@Value("${wechat.appSecret}")private String appSecret;@GetMapping("/getWeChat1")public WeChat getWeChat() {WeChat weChat = new WeChat();weChat.setAppId(appId);weChat.setAppSecret(appSecret);weChat.setToken(token);return weChat;}@RequestMapping("/hello")public String hello() {return "hello Spring Boot";}// @GetMapping("/getWeChat")
// public WeChat getWeChat() {
// return weChat;
// }}
(2)重启服务,访问接口
1.2.1. 方式四
@PropertySource
(1)新建my.properties
wechat1.appId=wx7874d0c23d07e2aa
wechat1.token=51zxw
wechat1.appSecret=e03f5ab03906d4d943675c233d4c3cb3
(2)新建MyWeChat类
package com.zzs.szyj.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@PropertySource("my.properties")
@ConfigurationProperties("wechat1")
@Component
public class MyWeChat {private String appId;private String token;private String appSecret;public String getAppId() {return appId;}public void setAppId(String appId) {this.appId = appId;}public String getToken() {return token;}public void setToken(String token) {this.token = token;}public String getAppSecret() {return appSecret;}public void setAppSecret(String appSecret) {this.appSecret = appSecret;}
}
(3)修改HelloController类
package com.zzs.szyj;
import com.zzs.szyj.config.WeChat;
import jakarta.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
// @Controller + @ResponseBody
public class HelloController {// @Resource
// //@Autowired
// private WeChat weChat;@Value("${wechat.appId}")private String appId;@Value("${wechat.token}")private String token;@Value("${wechat.appSecret}")private String appSecret;@Value("${wechat.port}")private String wechatPost;@Resourceprivate ApplicationContext applicationContext;@GetMapping("/getWeChat2")public WeChat getWeChat2() {Environment environment = applicationContext.getEnvironment();String appId = environment.getProperty("wechat.appId");String token = environment.getProperty("wechat.token");String appSecret = environment.getProperty("wechat.appSecret");WeChat weChat = new WeChat();weChat.setAppId(appId);weChat.setPort(wechatPost);weChat.setToken(token);weChat.setAppSecret(appSecret);return weChat;}@GetMapping("/getWeChat1")public WeChat getWeChat() {WeChat weChat = new WeChat();weChat.setAppId(appId);weChat.setAppSecret(appSecret);weChat.setToken(token);return weChat;}@RequestMapping("/hello")public String hello() {return "hello Spring Boot";}// @GetMapping("/getWeChat")
// public WeChat getWeChat() {
// return weChat;
// }}
(2)重启服务,访问接口
1.3. application.yml
(1)新建application.yml
server:port: 8082
wechat:appId: wx7874d0c23d07e2aatoken: 51zxwappSecret: e03f5ab03906d4d943675c233d4c3cb3
(2)重启服务,访问接口
1.4. SpringBoot多环境配置
1.4.1. 修改application.yml
server:port: 8082
wechat:appId: wx7874d0c23d07e2aatoken: 51zxwappSecret: e03f5ab03906d4d943675c233d4c3cb3port: ${server.port}
1.4.2. 多环境配置
(1)新建application-dev.yml、application-local.yml、application-pre.yml、application-prod.yml、application-test.yml
(2) 修改application.yml
wechat:appId: wx7874d0c23d07e2aatoken: 51zxwappSecret: e03f5ab03906d4d943675c233d4c3cb3port: ${server.port}
spring:profiles:active: local
(3) 运行
1.4.3. 多环境配置注入url
(1)多环境配置文件注入url
(2)添加链接
(2)运行
SpringBoot配置文件、配置示例下载