郑州定制网站,游戏软件网站开发,wap端是电脑还是手机,深圳优化公司找高粱seo服务目录 一#xff0c;SpringBoot中读取配置文件的常用方法1.1#xff0c;使用Value读取1.2#xff0c;使用ConfigurationProperties1.3#xff0c;使用Environment1.4#xff0c;自定义配置文件读取 二#xff0c;SpringBoot部署war项目到tomcat9和启动原理 一#xff0c;… 目录 一SpringBoot中读取配置文件的常用方法1.1使用Value读取1.2使用ConfigurationProperties1.3使用Environment1.4自定义配置文件读取 二SpringBoot部署war项目到tomcat9和启动原理 一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 {Autowiredprivate 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 {Autowiredprivate 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 {Autowiredprivate BootProperties bootProperties;RequestMapping(/test5)public Object test5(){return bootProperties.path ------ bootProperties.port;}}1.3使用Environment Environment是SpringCore中的一个用于读取配置文件的类将此类使用Autowired注入到类中就可以使用它的getProperty方法来获取某个配置项的值 RestController
public class HelloController {Autowiredprivate 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 {Autowiredprivate SysProperties sysProperties;RequestMapping(/test6)public Object test6(){return sysProperties.getParam1()sysProperties.getParam2();}}二SpringBoot部署war项目到tomcat9和启动原理
创建一个新项目 在添加个模块 然后在pom中添加依赖 packagingwar/packagingparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.0.1.RELEASE/version/parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependenciesbuildfinalNamepasserby-war/finalNamepluginsplugin!-- 打包插件 --groupIdorg.springframework.boot/groupIdartifactIdspring-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包粘贴的那个文件夹 现在就好了打开浏览器试试 有什么不理解的可以私信