`
dbp_cn
  • 浏览: 85004 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

spring web.xml中设置变量

    博客分类:
  • java
 
阅读更多

1. spring web.xml注入系统配置属性(如app.properties).

因为web.xml是web系统的配置入库,代码一经编译之后web.xml里面的值就不可改变,不能再程序运行时替换。所以如果需要在web.xml配置属性变量,必须在编译的时候就将属性变量替换。

具体需要在项目新建不同环境的属性文件

 

如(开发文件application-development.properties,测试application-integratetest.properties,生产application-production.properties)。然后在pom文件里面利用mavn profile 指定针对不同环境替换对于的属性文件里的变量,

如下例中有下面变量需要

<filter>
    <filter-name>CAS Authentication Filter</filter-name>
    <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
    <init-param>
      <param-name>casServerLoginUrl</param-name>
      <param-value>${uuc.host}/login</param-value>
    </init-param>
    <init-param>
      <param-name>serverName</param-name>
      <param-value>${server.name}</param-value>
    </init-param>
    <init-param>
      <param-name>gateway</param-name>
      <param-value>false</param-value>
    </init-param>

  

 1. 在application-development.properties等属性文件中配置

uuc.host=http://172.31.52.12:2280/uuc
conf.dir=classpath:
server.name=http://172.31.66.161:4000

 2.在项目的pom.xml 中配置如下build 参数

<build>
	<finalName>ROOT</finalName>
	<defaultGoal>compile</defaultGoal>
	<filters>
		<filter>${filter.file}</filter>
	</filters>
        	<resources>
		<resource>
			<directory>src/main/resources</directory>
			<includes>
				<include>**/*</include>
			</includes>
			<filtering>true</filtering>
		</resource>
		<resource>
			<directory>src/main/resources/properties</directory>
			<includes>
				<include>**/*</include>
			</includes>
			
			<filtering>true</filtering>
		</resource>
	</resources>
</build>
<profiles>
    <!-- 开发 -->
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <filter.file>src/main/resources/properties/application-development.properties</filter.file>
      </properties>
    </profile>
    <!-- 测试 -->
    <profile>
      <id>test</id>
      <properties>
        <filter.file>src/main/resources/properties/application-integratetest.properties</filter.file>
      </properties>
    </profile>
    <!-- 生产 -->
    <profile>
      <id>prod</id>
      <properties>
        <filter.file>src/main/resources/properties/application-production.properties</filter.file>
      </properties>
    </profile>
  </profiles>

 3. maven打包命令使用如下参数即可:

mvn clean install -DskipTests -Pdev (开发)
mvn clean install -DskipTests -Ptest (测试)
mvn clean install -DskipTests -Pprod (生产)

 

分享到:
评论

相关推荐

    org.springframework.web.servlet-3.1.0.RELEASE.jar.zip

    在实际开发中,开发者还需要了解如何配置Spring Web MVC,包括web.xml中的DispatcherServlet配置,以及Spring MVC配置文件中的bean定义。此外,对于大型项目,合理的Controller组织、Service层的设计以及DAO层的实现...

    Spring 3.x 中文开发手册.pdf

    除了上述改进之外,Spring 3.x 还对 MVC 架构进行了大量改进,提高了 Web 开发的效率和灵活性: - **基于注解的控制器映射**:现在可以在控制器中使用注解来指定特定的 Mapping 或 ExceptionHandler,使得控制器...

    在logback.xml中自定义动态属性的方法

    这样,你可以在logback.xml中通过${变量名}获取到这些动态设置的属性值。 总结来说,通过上述方法可以有效地在logback.xml中自定义动态属性,从而解决多实例部署时日志文件冲突的问题。无论是通过简单的...

    Spring配置Freemarker视图解析器,配置多个视图解析器

    在Spring的配置文件(如`applicationContext.xml`或`dispatcher-servlet.xml`)中,我们需要添加以下XML配置: ```xml &lt;bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker....

    Maven 搭建Spring + SpringMVC.rar

    接着,我们需要在项目的`pom.xml`文件中添加Spring和SpringMVC的依赖。Spring的核心库包括`spring-context`、`spring-beans`、`spring-aop`等,而SpringMVC则需要`spring-webmvc`。这些依赖应该按照Maven的坐标系统...

    spring 3.x source code

    在Spring 3.x中,SpEL得到了增强,支持更多的运算符和上下文变量。 3. **Improved Asynchronous Support**:Spring 3.x增强了异步处理能力,支持非阻塞I/O和多线程处理,提升了Web应用的性能。 4. **RESTful Web ...

    spring4.x 集成 jersey2.x 实现对外提供接口服务

    在开始集成之前,确保你已经安装了Java Development Kit (JDK) 并设置了相应的环境变量。同时,你需要在项目中引入Spring 4.x和Jersey 2.x的依赖。如果是Maven项目,可以在pom.xml文件中添加如下依赖: ```xml ...

    手动整合struts2+spring3.

    在软件开发领域,尤其是Java Web开发中,Struts2与Spring是两个非常重要的框架。Struts2是一个用于构建动态Web应用的MVC(Model-View-Controller)框架,而Spring则是一个提供全面基础设施支持的轻量级容器。本文将...

    spring-framework-3.2.x.zip

    Spring MVC是Spring Framework中的Web开发模块,3.2.x版本引入了更多增强,如类型安全的路径变量、自定义异常处理、改进的视图解析等。`@RequestMapping`、`@Controller`、`@ResponseBody`等注解帮助构建RESTful服务...

    使用XFire+Spring构建Web Service

    接着,在`web.xml`配置文件中设置XFireSpringServlet,以便对外提供Web Service服务。这一步骤确保了Spring与XFire的协作。 为了创建Web Service,你需要定义业务接口和其实现类。例如,你可以创建一个名为`Hello...

    spring web flow reference 2.4.0

    对于使用Ivy作为构建工具的项目,可以通过在ivy.xml文件中添加以下依赖来使用Spring Web Flow: ```xml &lt;dependency org="org.springframework.webflow" name="spring-webflow" rev="2.4.0"/&gt; ``` ##### 1.7 夜间...

    spring webflow升级-从1.0 到 2.0

    在 SWF 1 中,可以通过标准的 Spring Bean XML 配置或使用 `webflow-config-1.0` Schema 进行配置。而在 SWF 2 中,Schema 配置成为唯一的选择。这意味着需要使用特定的 Namespace 来配置 Web Flow 相关的组件。 ##...

    activiti 基础 web项目 spring mvc.zip

    在Spring MVC环境中,可以通过调用Activiti提供的API来启动流程实例,更新变量,或者完成任务。这些操作通常在控制器类中进行,根据用户的交互触发。 **6. 视图与任务交互** 在Spring MVC项目中,视图层负责展示...

    cxf-2.7.3与spring3整合开发步骤.

    【正文】 在本文中,我们将深入探讨如何将Apache CXF 2.7.3与Spring 3.0.7框架整合进行开发。...在整个过程中,Spring的角色主要是管理服务组件和提供依赖注入,而CXF负责Web服务的生成、部署和调用。

    Spring MVC 入门实例

    因为我的 jsp 和 html 文件都是 UTF-8 编码的, 所以我在 param-value 标签中设置了 UTF-8. 估计你使用的是 GB2312 或者 GBK, 立即转到 UTF-8 上来吧. 分解配置文件. context-param 标签指明我们的配置文件还有 /...

    spring-webflow

    ### Spring Web Flow 2 参考指南核心知识点详解 #### 一、引言与环境配置 **1.1 引言** Spring Web Flow (SWF) 是一款强大的工作流框架,专为构建复杂的多步骤 Web 应用而设计。本指南涵盖了 Spring Web Flow 2 ...

    maven 搭建spring mvc环境

    在`pom.xml`中,我们需要添加Spring MVC和相关依赖。例如,添加Spring Web和Spring MVC的依赖: ```xml &lt;groupId&gt;org.springframework &lt;artifactId&gt;spring-webmvc &lt;version&gt;3.2.2.RELEASE &lt;!-- 其他依赖,...

    maven中pom.xml基本配置

    6. **属性(Properties)**:定义可重用的变量,可以在整个pom.xml文件中引用。例如: - `&lt;properties&gt;` - `&lt;java.version&gt;1.8&lt;/java.version&gt;`:定义一个属性,方便在其他地方引用。 - `&lt;/properties&gt;` 7. **...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....

    java spring4.1.x源码

    在4.1.x版本中,Spring MVC引入了路径变量的支持,增强了RESTful服务的实现,并且对模板引擎如FreeMarker和Thymeleaf的集成进行了优化。 Spring的数据访问层提供了对JDBC、ORM(Object-Relational Mapping)框架如...

Global site tag (gtag.js) - Google Analytics