您现在的位置是:主页 > news > 徐州哪家做网站好/河南网站建设哪个公司做得好

徐州哪家做网站好/河南网站建设哪个公司做得好

admin2025/5/22 9:53:08news

简介徐州哪家做网站好,河南网站建设哪个公司做得好,bootstrap 购物网站 导航菜单,网站中的打赏怎么做的SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程 通过前面的了解&a…

徐州哪家做网站好,河南网站建设哪个公司做得好,bootstrap 购物网站 导航菜单,网站中的打赏怎么做的SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程 通过前面的了解&a…

SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程

 通过前面的了解,SpringBoot 虽然干掉了 XML 但未做到 零配置,它体现出了一种 约定优于配置,也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。 一般情况下默认的配置足够满足日常开发所需,但在特殊的情况下,我们往往需要用到自定义属性配置、自定义文件配置、多环境配置、外部命令引导等一系列功能。不用担心,这些 SpringBoot 都替我们考虑好了,我们只需要遵循它的规则配置即可

为了让 Spring Boot 更好的生成配置元数据文件,我们需要添加如下依赖(该依赖可以不添加,但是在 IDEA 和 STS 中不会有属性提示,没有提示的配置就跟你用记事本写代码一样苦逼,出个问题弄哭你去),该依赖只会在编译时调用,所以不用担心会对生产造成影响… 

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>

Springboot 中配置文件的优先级和加载顺序

1. 若application.yml 和bootStrap.yml 在同一目录下,则bootStrap.yml 的加载顺序要高于application.yml,即bootStrap.yml  会优先被加载。

   原理:bootstrap.yml 用于应用程序上下文的引导阶段。

              bootstrap.yml 由父Spring ApplicationContext加载。

            •bootstrap.yml 可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。
            •application.yml 可以用来定义应用级别的,如果搭配 spring-cloud-config 使用 application.yml 里面定义的文件可以实现动态替换。

2. 不同位置的配置文件的加载顺序:

   在不指定要被加载文件时,默认的加载顺序:由里向外加载,所以最外层的最后被加载,会覆盖里层的属性(参考官网介绍)

  •    file:./config/
  •    file:./
  •    classpath:/config/
  •    classpath:/ 

 最下层的优先加载,所以最上层的属性会覆盖下层的属性;

3. 可以通过属性指定加载某一文件:

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
当通过spring.config.location 指定一个配置文件时,配置文件的搜索顺序如下:

  • file:./custom-config/
  • classpath:custom-config/
  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/

最下层的优先加载,所以最上层的属性会覆盖下层的属性;

4. 如果使用spring-cloud-config时,项目内部的resource下有bootstrap.yml文件,并且在bootstrap.yml 里配置spring.application.name, git.url,spring.active.profies. 将项目打成jar包,放到服务器上,与jar包并列的位置,有start.sh脚本, 

a. 在start 脚本里指定了配置文件:spring.config.location=./bootstrap.yml, 则配置文件的加载顺序将为:

  • 1. cloud-config 仓库里指定的yml 配置;
  • 2. ./bootstrap.yml
  • 3. classpath:/bootstrap.yml
  • 4. 外部application.yml
  • 5. 内部application.yml

b. 在start 脚本里指定了配置文件:spring.config.location=./application.yml, 则配置文件的加载顺序将为:

  • 1. cloud-config 仓库里指定的yml 配置;
  • 2. ./application.yml
  • 3.  classpath:/application.yml
  • 4. ./bootstrap.yml
  • 5. classpath:/bootstrap.yml

所以,不管是jar包内还是jar运行的同级目录下,只要包含bootstrap.yml ,且为云配置,则云配置文件会覆盖其他配置文件;

 

自定义属性配置

在 application.properties 写入如下配置内容

my1.age=22
my1.name=battcn

其次定义 MyProperties1.java 文件,用来映射我们在 application.properties 中的内容,这样一来我们就可以通过操作对象的方式来获得配置文件的内容了 

package com.battcn.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** @author Levin* @since 2018/4/23 0023*/
@Component
@ConfigurationProperties(prefix = "my1")
public class MyProperties1 {private int age;private String name;// 省略 get set@Overridepublic String toString() {return "MyProperties1{" +"age=" + age +", name='" + name + '\'' +'}';}
}

 接下来就是定义我们的 PropertiesController 用来注入 MyProperties1 测试我们编写的代码,值得注意的是 Spring4.x 以后,推荐使用构造函数的形式注入属性…

import com.battcn.properties.MyProperties1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;/*** @author Levin* @since 2018/4/23 0023*/
@RequestMapping("/properties")
@RestController
public class PropertiesController {private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);private final MyProperties1 myProperties1;@Autowiredpublic PropertiesController(MyProperties1 myProperties1) {this.myProperties1 = myProperties1;}@GetMapping("/1")public MyProperties1 myProperties1() {log.info("=================================================================================================");log.info(myProperties1.toString());log.info("=================================================================================================");return myProperties1;}
}

 打开浏览器,输入如下地址: http://localhost:8080/properties/1,观察控制台,监听到如下内容则表示程序正确

2018-04-23 15:51:43.145  INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController      : =================================================================================================
2018-04-23 15:51:43.145  INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController      : MyProperties1{age=22, name='battcn'}
2018-04-23 15:51:43.145  INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController      : =================================================================================================

 自定义文件配置

定义一个名为 my2.properties 的资源文件,自定义配置文件的命名不强制 application 开头

my2.age=22
my2.name=Levin
my2.email=1837307557@qq.com

其次定义 MyProperties2.java 文件,用来映射我们在 my2.properties 中的内容。 

package com.battcn.properties;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;/*** @author Levin* @since 2018/4/23 0023*/
@Component
@PropertySource("classpath:my2.properties")
@ConfigurationProperties(prefix = "my2")
public class MyProperties2 {private int age;private String name;private String email;// 省略 get set @Overridepublic String toString() {return "MyProperties2{" +"age=" + age +", name='" + name + '\'' +", email='" + email + '\'' +'}';}
}

接下来在 PropertiesController 用来注入 MyProperties2 测试我们编写的代码 

@GetMapping("/2")
public MyProperties2 myProperties2() {log.info("=================================================================================================");log.info(myProperties2.toString());log.info("=================================================================================================");return myProperties2;
}

打开浏览器,输入如下地址: http://localhost:8080/properties/2,观察控制台,监听到如下内容则表示程序正确 

2018-04-23 15:59:45.395  INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController      : =================================================================================================
2018-04-23 15:59:45.395  INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController      : MyProperties2{age=22, name='Levin', email='1837307557@qq.com'}
2018-04-23 15:59:45.395  INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController      : =================================================================================================

 

多环境化配置

在真实的应用中,常常会有多个环境(如:开发,测试,生产等),不同的环境数据库连接都不一样,这个时候就需要用到spring.profile.active 的强大功能了,它的格式为 application-{profile}.properties,这里的 application 为前缀不能改,{profile} 是我们自己定义的。

创建 application-dev.propertiesapplication-test.propertiesapplication-prod.properties,内容分别如下

  • application-dev.properties 

 server.servlet.context-path=/dev

  • application-test.properties 

server.servlet.context-path=/test 

  • application-prod.properties 

 server.servlet.context-path=/prod

在 application.properties 配置文件中写入 spring.profiles.active=dev,这个时候我们在次访问 http://localhost:8080/properties/1 就没用处了,因为我们设置了它的context-path=/dev,所以新的路径就是 http://localhost:8080/dev/properties/1 ,由此可以看出来我们激活不同的配置读取的属性值是不一样的 

外部命令引导

前面三种方式都是基于配置文件层面的,那么有没有办法外部引导呢,假设这样的场景,我们对已经开发完成的代码打包发布,期间在测试环境测试通过了,那么即可发布上生产,这个时候是修改application.properties的配置方便还是直接在命令参数配置方便呢,毫无疑问是后者更有说服力。默认情况下,SpringApplication 会将命令行选项参数(即:–property,如–server.port=9000)添加到Environment,命令行属性始终优先于其他属性源。

如何测试?

  • 进入到项目目录,此处以我本地目录为主:F:/battcn-workspace/spring-boot2-learning/chapter2
  • 然后打开 cmd 程序,不会在当前目录打开 cmd 的请自行百度,输入:mvn package
  • 打包完毕后进入到:F:/battcn-workspace/spring-boot2-learning/chapter2/target 目录中去,我们可以发现一个名为chapter2-0.0.1-SNAPSHOT.jar 的包
  • 接着在打开 cmd 程序,输入:java -jar chapter2-0.0.1-SNAPSHOT.jar --spring.profiles.active=test --my1.age=32。仔细观察spring.profiles.active=test、my1.age=32 这俩配置的键值是不是似曾相识(不认识的请从开头认真阅读)
  • 最后输入测试地址:http://localhost:8080/test/properties/1 我们可以发现返回的JSON变成了 {"age":32,"name":"battcn"} 表示正确

总结

  • 掌握@ConfigurationProperties@PropertySource 等注解的用法及作用
  • 掌握编写自定义配置
  • 掌握外部命令引导配置的方式

目前很多大佬都写过关于 SpringBoot 的教程了,如有雷同,请多多包涵,本教程基于最新的 spring-boot-starter-parent:2.0.1.RELEASE编写,包括新版本的特性都会一起介绍…

以下应用程序starters是Spring Boot在org.springframework.boot组下提供的:

名称描述
spring-boot-starter核心Spring Boot starter,包括自动配置支持,日志和YAML
spring-boot-starter-actuator生产准备的特性,用于帮我们监控和管理应用
spring-boot-starter-amqp对”高级消息队列协议”的支持,通过spring-rabbit实现
spring-boot-starter-aop对面向切面编程的支持,包括spring-aop和AspectJ
spring-boot-starter-batch对Spring Batch的支持,包括HSQLDB数据库
spring-boot-starter-cloud-connectors对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接
spring-boot-starter-data-elasticsearch对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire对GemFire分布式数据存储的支持,包括spring-data-gemfire
spring-boot-starter-data-jpa对”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-data-mongodb对MongoDB NOSQL数据库的支持,包括spring-data-mongodb
spring-boot-starter-data-rest对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现
spring-boot-starter-data-solr对Apache Solr搜索平台的支持,包括spring-data-solr
spring-boot-starter-freemarker对FreeMarker模板引擎的支持
spring-boot-starter-groovy-templates对Groovy模板引擎的支持
spring-boot-starter-hateoas对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现
spring-boot-starter-hornetq对”Java消息服务API”的支持,通过HornetQ实现
spring-boot-starter-integration对普通spring-integration模块的支持
spring-boot-starter-jdbc对JDBC数据库的支持
spring-boot-starter-jersey对Jersey RESTful Web服务框架的支持
spring-boot-starter-jta-atomikos对JTA分布式事务的支持,通过Atomikos实现
spring-boot-starter-jta-bitronix对JTA分布式事务的支持,通过Bitronix实现
spring-boot-starter-mail对javax.mail的支持
spring-boot-starter-mobile对spring-mobile的支持
spring-boot-starter-mustache对Mustache模板引擎的支持
spring-boot-starter-redis对REDIS键值数据存储的支持,包括spring-redis
spring-boot-starter-security对spring-security的支持
spring-boot-starter-social-facebook对spring-social-facebook的支持
spring-boot-starter-social-linkedin对spring-social-linkedin的支持
spring-boot-starter-social-twitter对spring-social-twitter的支持
spring-boot-starter-test对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块
spring-boot-starter-thymeleaf对Thymeleaf模板引擎的支持,包括和Spring的集成
spring-boot-starter-velocity对Velocity模板引擎的支持
spring-boot-starter-web对全栈web开发的支持, 包括Tomcat和spring-webmvc
spring-boot-starter-websocket对WebSocket开发的支持
spring-boot-starter-ws对Spring Web服务的支持