当前位置: 首页 > news >正文

电子商城网站开发价格/google 官网入口

电子商城网站开发价格,google 官网入口,安庆网页设计培训,关于建设网站安全性合同在Maven和Spring中,都有profile这个概念。profile是用于区分各种环境的,例如开发环境、测试环境、正式环境等。Maven的profile用于在打包时根据指定环境替换不同环境的配置文件配置,如数据库配置。Spring的Profile可以用于在不同的环境下加载…

在Maven和Spring中,都有profile这个概念。profile是用于区分各种环境的,例如开发环境、测试环境、正式环境等。Maven的profile用于在打包时根据指定环境替换不同环境的配置文件配置,如数据库配置。Spring的Profile可以用于在不同的环境下加载不同的bean,例如@Profile注解。两者一个是Maven编译和打包时生效,另一个是运行时生效,默认是没有关联的,本文会分别介绍非Spring Boot项目和Spring Boot项目整合Maven profile。

Maven profile配置

pom.xml中,可以配置testproduct两个profile,分别对应测试环境和正式环境。这里也可以根据具体情况自定义。

<profiles><profile><id>test</id>...</profile><profile><id>product</id>...</profile>
</profiles>

此时,运行mvn package -Ptest就会使用id为test的profile内的配置打包,mvn package -Pproduct就是用来打正式环境包的命令。

Spring Framework(非Spring Boot)整合Maven profile

Spring Framework如何启用一个profile

Spring启用某个profile有多种方式(摘自官方文档:https://docs.spring.io/spring... ):

Activating a profile can be done in several ways, but the most straightforward is to do it programmatically against the Environment API which is available through an ApplicationContext.
In addition, you can also declaratively activate profiles through the spring.profiles.active property, which may be specified through system environment variables, JVM system properties, servlet context parameters in web.xml, or even as an entry in JNDI.

总结一下有以下几种方式:

  • 通过代码设置:ApplicationContext.getEnvironment().setActiveProfiles("yourProfile")
  • 通过系统环境变量spring.profiles.active值来设置
  • 通过JVM系统属性spring.profiles.active值来设置
  • 通过web.xml中的context-param来设置

为了便于跟Maven整合,我们使用web.xml来设置Spring profile,如下:

<context-param><param-name>spring.profiles.active</param-name><param-value>product</param-value>
</context-param>

以上配置会启用Spring的product profile,即正式环境。

Spring Framework profile整合Maven profile

如果想要整合Maven profile和Spring Framework profile,需要在Maven打包时对web.xml中的spring.profiles.active值进行替换,可以在web.xml中配置一个占位符${activeProfile}

<context-param><param-name>spring.profiles.active</param-name><param-value>${activeProfile}</param-value>
</context-param>

pom.xml配置maven-war-plugin

<!-- 打war包时替换占位符 -->
<build><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version><configuration><filteringDeploymentDescriptors>true</filteringDeploymentDescriptors></configuration></plugin>
</build><!-- 默认的maven profile -->
<properties><activeProfile>dev</activeProfile>
</properties><profiles><profile><id>test</id><properties><activeProfile>test</activeProfile></properties></profile><profile><id>product</id><properties><activeProfile>product</activeProfile></properties></profile>
</profiles>

<filteringDeploymentDescriptors>true表示过滤Deployment Descriptor并将文件中的占位符替换为pom.xml中对应的<properties>值,Deployment Descriptor即部署描述符,指的就是web.xml (参考维基百科:https://zh.wikipedia.org/wiki... )。

以上配置完成后,再通过mvn package -Ptestmvn package -Pproduct打包后,再解压war包,可以看到web.xml中原有的

<context-param><param-name>spring.profiles.active</param-name><param-value>${activeProfile}</param-value>
</context-param>

被替换为了Maven中对应的profile,例如mvn package -Pproduct打包后web.xml内容:

<context-param><param-name>spring.profiles.active</param-name><param-value>product</param-value>
</context-param>

以上就完成了Maven profile和Spring profile的整合。

兼容jetty-maven-plugin

如果恰好在项目中使用到jetty-maven-plugin用于开发环境调试,那么在web.xml配置占位符${activeProfile}后,通过mvn jetty:run启动应用时会Spring框架会报错:

Could not resolve placeholder 'activeProfile' in string value "${activeProfile}"

这是因为运行mvn jetty:run命令时插件并没有打war包,而是直接使用源码中的web.xml,此时占位符${activeProfile}未被maven-war-plugin替换,所以Spring框架会报错。

参考文档:https://www.eclipse.org/jetty...

解决方法一

使用mvn jetty:run-warmvn jetty:run-exploded命令替代mvn jetty:run,这两个命令会先用maven-war-plugin打好war包后再运行,此时占位符${activeProfile}已被替换为Maven的profile。

但是这种方案会带来一个问题:由于这种方式需要先打war包再运行,开发时项目中资源(例如html、jsp)修改后就不会实时生效,而是需要重新打包启动,不便于调试。

解决方法二(推荐)

这种方案还是使用mvn jetty:run命令,只需要给jetty-maven-plugin插件添加一个名为activeProfile的系统属性,让Spring框架来解析web.xml中的${activeProfile}

<plugin><groupId>org.eclipse.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>9.2.10.v20150310</version><configuration><webApp><contextPath>/</contextPath></webApp><systemProperties><systemProperty><name>activeProfile</name><value>${activeProfile}</value></systemProperty></systemProperties></configuration>
</plugin>

参考文档:https://www.eclipse.org/jetty...

Spring Boot整合Maven profile

如果项目采用的框架是Spring Boot而不是直接使用Spring Framework,那么Spring Boot的profile可以在resources目录下的application.propertiesapplication.yml文件中指定,以application.properties为例:

spring.profiles.active=product

要想整合Maven profile只需要改为@activeProfile@占位符即可:

spring.profiles.active=@activeProfile@

仅需要这一行配置就完成了Spring Boot profile整合Maven profile,非常方便。此时可以尝试mvn package -Ptestmvn package -Pproduct命令打包,安装包内的文件中@activeProfile@占位符已被替换。

Spring Boot整合Maven profile原理

Spring Boot项目中一般都会加上spring-boot-starter-parent

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>${spring.boot.version}</version>
</parent>

可以查看spring-boot-starter-parent的pom.xml文件,里面包含maven-resources-plugin

<plugin><artifactId>maven-resources-plugin</artifactId><configuration><delimiters><delimiter>${resource.delimiter}</delimiter></delimiters><useDefaultDelimiters>false</useDefaultDelimiters></configuration>
</plugin>

${resource.delimiter}定义的值是@

<resource.delimiter>@</resource.delimiter>

这样maven-resources-plugin插件会将application.propertiesapplication.yml文件中的@activeProfile@替换为pom.xml中对应profile的值。

至于为什么Spring Boot要使用@..@而不是Maven默认的${..}作为占位符的符号,官方文档也给出了解释,以下摘自:https://docs.spring.io/spring...

Note that, since the application.properties and application.yml files accept Spring style placeholders (${…​}), the Maven filtering is changed to use @..@ placeholders. (You can override that by setting a Maven property called resource.delimiter.)

因为Spring Boot框架本身也用${..}作为占位符,Maven插件maven-resources-plugin如果还使用相同的占位符,那么可能会导致一些冲突,所以spring-boot-starter-parentmaven-resources-plugin的占位符改为@..@

参考文档

  • Spring Framework: Activating a Profile
  • Spring Boot: Maven
  • Jetty Maven Plugin
  • 维基百科:部署描述符(Deployment Descriptor)
http://www.lbrq.cn/news/806563.html

相关文章:

  • 做网站绑定 对应的域名/渠道推广有哪些方式
  • 有创意做网站找投资/网络营销的五大特点
  • 百度做的网站 后台管理怎么进入/中央常委成员名单
  • 杭州建委网站首页/长春网站公司哪家好
  • 上海做网站哪家好/百度关键词推广公司哪家好
  • 嘉鱼网站建设哪家专业/电商代运营公司
  • 南部网站建设/西安网站建设公司排行榜
  • 做字体的网站/本地推广最好用的平台
  • 找公司做网站源代码给客户吗/济南seo官网优化
  • 做网站等保收费/汽车推广软文
  • 电视网站后台管理系统漏洞/seo优化一般多少钱
  • 查询网站服务商/百度站长平台工具
  • wordpress的wp后台css样式错乱/北京关键词优化平台
  • 上海高端网站定制建设公司/临沂seo排名外包
  • 手机直播app开发制作/关键词优化一般收费价格
  • 做网站使用字体图标/线上营销工具
  • 网络舆情现状分析/seo网站关键词
  • 贵州网站设计/山东搜索引擎优化
  • 泰康人寿保险官方网站/企业seo推广
  • 男人做鸭子的网站/十堰seo排名公司
  • 营销型网站建设合同/杭州关键词自动排名
  • 16年百度对泛解析网站的惩罚/上海seo外包公司
  • 也买酒技术网站建设/seo静态页源码
  • 网络销售怎么做网站/2023年重大时政热点
  • 男女做羞羞事的网站/站长工具如何使用
  • 成都网站制作怎么样/阿里云搜索引擎
  • flask做视频网站/如何做网销
  • 网站建设备案书模板/今日预测足球比分预测
  • 企业门户网站建设渠道/种子库
  • wordpress登录页美化/青岛seo网站管理
  • npm报错:npm install 出现“npm WARN old lockfile”
  • OPENGLPG第九版学习 - 纹理与帧缓存 part2
  • echarts一个图例控制多个图表
  • Windows11 WSL安装Ubntu22.04,交叉编译C语言应用程序
  • RabbitMQ 消费者确认 (Ack/Nack) (With Spring Boot)
  • MacTex+Vscode数学建模排版