Setting variables in spring web.xml

1. Spring web.xml injects system configuration properties (such as app.properties).

Because web.xml is the configuration storage of the web system, the value in web.xml cannot be changed after the code is compiled, and cannot be replaced when the program is running. So if you need to configure property variables in web.xml, you must replace the property variables at compile time.

Specifically, you need to create a new property file for different environments in the project

 

Such as (development file application-development.properties, test application-integratetest.properties, production application-production.properties). Then use the mavn profile in the pom file to specify the variables in the property file to be replaced for different environments,

In the following example, the following variables are required

<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. Configure in property files such as application-development.properties

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

 2. Configure the following build parameters in the project's pom.xml

<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>
    <!-- development-->
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <filter.file>src/main/resources/properties/application-development.properties</filter.file>
      </properties>
    </profile>
    <!-- test-->
    <profile>
      <id>test</id>
      <properties>
        <filter.file>src/main/resources/properties/application-integratetest.properties</filter.file>
      </properties>
    </profile>
    <!-- production-->
    <profile>
      <id>prod</id>
      <properties>
        <filter.file>src/main/resources/properties/application-production.properties</filter.file>
      </properties>
    </profile>
  </profiles>

 3. The maven packaging command can use the following parameters:

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326046651&siteId=291194637