浅谈xml配置spring profiles的几个注意点

先贴正确配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  <task:annotation-driven/>

  <import resource="spring-datasource.xml"/>
  <import resource="spring-hessian-server.xml"/>
  <import resource="spring-remoting-dis.xml"/>
  <import resource="spring-remoting-worldeye.xml"/>
  <import resource="spring-activemq.xml"/>
  <import resource="spring-cxf-client.xml"/>

  <!-- 开发配置 -->
  <beans profile="dev">
    <context:property-placeholder location="classpath:config/application.properties, classpath:config/application-dev.properties"/>
    <import resource="spring-hadoop-dev.xml"/>
  </beans>

  <!-- 测试配置 -->
  <beans profile="test">
    <context:property-placeholder location="classpath:config/application.properties, classpath:config/application-prd.properties, classpath:config/application-test.properties"/>
    <import resource="spring-hadoop-test.xml"/>
  </beans>

  <!-- 线上配置 -->
  <beans profile="prd">
    <context:property-placeholder location="classpath:config/application.properties, classpath:config/application-prd.properties"/>
    <import resource="spring-hadoop.xml"/>
  </beans>
</beans>

一. xml标签的xsd版本

spring-beans.xsd 文件不要指定版本,也可以使用高版本(起码是3.1),原因是 spring profile 是3.1版本开始的。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

    ......

    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

二. dispatcherServlet文件配置

web.xml中配置了 DispatcherServlet 的 contextConfigLocation,需要在 spring-dispatch.xml 添加 spring profile 的配置,配置项同上。

  <!-- profile配置 -->
  <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>prd</param-value>
  </context-param>

  <!-- Spring配置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:config/spring/spring-context.xml
      classpath:config/spring/spring-security.xml
    </param-value>
  </context-param>

  ......

  <!-- Spring Dispatcher配置 -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        classpath:config/spring/spring-hessian-server.xml
        classpath:config/spring/spring-dispatch.xml
        classpath:config/spring/spring-security.xml
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 使用spring.profiles.active来分区配置的方法示例

    很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境下配置用不同的配置文件或者不同的配置 spring boot允许你通过命名约定按照一定的格式(application-{profile}.properties)来定义多个配置文件,然后通过在application.properyies通过spring.profiles.active来具体激活一个或者

  • Spring Boot配置特定属性spring.profiles的方法

    Spring Boot配置特定属性spring.profiles SpringBoot能使用application- {你的自定义profile名称myProfileName} .properties模式添加任何你指定配置文件到其属性文件. 要加载特定的配置文件属性文件,我们可以使用命令行选项-Dspring.profiles.active = myProfileName. 缺省默认SpringBoot是加载application.properties,无需任何-Dspring.profile.

  • 详解Spring Boot Profiles 配置和使用

    介绍 Spring Profiles 提供了一套隔离应用配置的方式,不同的 profiles 提供不同组合的配置,在不同的环境中,应用在启动时通过选择激活某些特定的 profiles 来适应运行时环境,以达到在不同的环境可以使用相同的一套程序代码. 环境 JDK 8 Maven 3 IntelliJ IDEA 2016 Spring Boot 1.5.2.RELEASE @Profiles 你可以在任何 @Component(@Service,@Repository) 或 @Configurat

  • 浅谈xml配置spring profiles的几个注意点

    先贴正确配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/

  • 浅谈XML Schema中的elementFormDefault属性

    elementFormDefault属性与命名空间相关,其值可设置为qualified或unqualified 如果设置为qualified: 在XML文档中使用局部元素时,必须使用限定短名作为前缀 sean.xsd: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sean=&

  • 浅谈Linux配置定时,使用crontab -e与直接编辑/etc/crontab的区别

    Linux配置定时任务,大家都知道使用crontab这个系统功能,但有时候我们需要区分用户执行,下面就直接说一下2种方法的区别: 方法1: 使用命令 crontab -e 然后直接编辑定时脚本. 这样执行以后,属于用户自定义的,会被写到 /var/spool/cron 目录下,生成一个和用户名一致的文件,文件内容就是我们编辑的定时脚本. 如: [root@localhost cron.d]# cd /var/spool/cron [root@localhost cron]# ll 总用量 4 -

  • 浅谈CMake配置OpenCV 时静态链接与动态链接的选择

    方法: 添加OpenCV_STATIC 选项,设置为不勾选,在cmake配置的时候就会选择动态库 否则,cmake 配置的时候会设置为静态库 以上这篇浅谈CMake配置OpenCV 时静态链接与动态链接的选择就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • 浅谈python配置与使用OpenCV踩的一些坑

    下载opencv2.4.9(python2.7匹配)后 (1)运行OpenCV 2.4.9.exe: (2)配置Python:将\opencv\build\python\2.7\x64 这个目录下:cv2.pyd 复制到:Python27\Lib\site-packages\目录下: (3)测试:输入import cv2,如报错,说明未安装成功 1.opencv的版本一定要与python的版本匹配,否则是python是无法调用cv2这个模块的. 错误信息:ImportError DLL load

  • Django 浅谈根据配置生成SQL语句的问题

    想要根据django中的模型和配置生成SQL语句,需要先进行一定的设置: 首先需要在你的app文件夹中进入setting.py文件,里面有一个DATABASES,进行设置数据库的配置信息: DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'ENGINE': 'django.db.backends.mysql',

  • 浅谈Java中spring 线程异步执行

    多线程并发处理起来通常比较麻烦,如果你使用spring容器来管理业务bean,事情就好办了多了.spring封装了Java的多线程的实现,你只需要关注于并发事物的流程以及一些并发负载量等特性,具体来说如何使用spring来处理并发事务: 1.了解 TaskExecutor接口 Spring的TaskExecutor接口等同于java.util.concurrent.Executor接口. 实际上,它存在的主要原因是为了在使用线程池的时候,将对Java5的依赖抽象出来. 这个接口只有一个方法exe

  • 浅谈Java中Spring Boot的优势

    Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. Spring Boot 简化了基于 Spring 的应用开发,通过少量的代码就能创建一个独立的.产品级别的 Spring 应用. 作为一名 Java 程序员如果你已经厌恶了传统的开发模式,希望有一个全新的框架可以提供快速开发,简单集成的编程体验,强烈建议你学习了解 Spring Boot .

  • 基于XML配置Spring的自动装配过程解析

    一.了解Spring自动装配的方式 采用传统的XML方式配置Bean组件的关键代码如下所示 <bean id="userMapper" class="edu.cn.dao.UserMapperImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <bean id="userSer

  • 浅谈一个基础的SpringBoot项目该包含哪些

    前言   建立一个全新的项目,或者把旧的庞大的项目,进行拆分成多个项目.在建立新的项目中,经常需要做一些重复的工作,比如说拷贝一下常用的工具类,通用代码等等.所以就可以做一个基础的项目方便使用,在经历新项目的时候,直接在基础项目上进行简单配置就可以开发业务代码了. 基础项目该包含哪些东西. Swagger在线接口文档. CodeGenerator 代码生成器. 统一返回. 通用的分页对象. 常用工具类. 全局异常拦截. 错误枚举. 自定义异常. 多环境配置文件. Maven多环境配置. 日志配置

随机推荐