Java web项目创建笔记5 之《springmvc基础配置——多环境配置及读取属性文件》

1、在src/main/resources下建立env目录
建立属性文件
configurations-dev.properties
内容:flag=1
configurations-sit.properties
内容:flag=2
configurations-uat.properties
内容:flag=3
configurations-pit.properties
内容:flag=4

application-dev.properties
内容:appName=dev
application-sit.properties
内容:appName=sit
application-uat.properties
内容:appName=uat
application-pit.properties
内容:appName=pit

2、建立xml文件
在src/main/resources下创建spring-env.xml




	
		
		
			
			
			
				classpath:env/application-dev.properties
			
		
	

	
		
		
			
			
			
				classpath:env/application-sit.properties
			
		
	

	
		
		
			
			
			
				classpath:env/application-uat.properties
			
		
	

	
		
		
			
			
			
				classpath:env/application-pit.properties
			
		
	

说明:
1)context:property-placeholder可以在代码中使用@Value注解获取值

@Value("${flag}")
private String flag;

2)使用Bean方式定义,可以在代码中使用工具类获取

String appName = PropertiesUtil.getProperties("appName");

3)configurations-XXX.properties放置系统环境参数(数据库连接,调用组件参数),application-XXX.properties放置应用环境参数

3、applicationContext.xml导入该文件




	
	

	
		
	

	
	

	

4、web.xml中添加spring.profiles.default配置


	Archetype Created Web Application

	
	
		dispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
		1
	

	
		dispatcherServlet
		/
	

	
	
		contextConfigLocation
		classpath:applicationContext.xml
	

	
		org.springframework.web.context.ContextLoaderListener
	

	
	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
	

	
		encodingFilter
		/*
		REQUEST
		FORWARD
	

	
	
		spring.profiles.default
		${env.config}
	

5、pom.xml中添加环境切换参数


	4.0.0
	com.my.maven
	webapp
	war
	0.0.1-SNAPSHOT
	webapp Maven Webapp
	http://maven.apache.org

	
		UTF-8
		UTF-8
		UTF-8
		1.8
		1.8
		4.3.22.RELEASE
		1.8.13
		2.6
		1.12
		3.8.1
		2.6.1
		3.1.0
		1.7.26
		1.2.3
		0.1.5
		1.7.26
	

	
		
			junit
			junit
			4.12
			test
		
		
		
			org.springframework
			spring-core
			${springframework.version}
		
		
			org.springframework
			spring-beans
			${springframework.version}
		
		
			org.springframework
			spring-context
			${springframework.version}
		
		
			org.springframework
			spring-context-support
			${springframework.version}
		
		
			org.springframework
			spring-expression
			${springframework.version}
		
		
			org.springframework
			spring-aop
			${springframework.version}
		
		
			org.aspectj
			aspectjweaver
			${aspectj.version}
		
		
			org.springframework
			spring-jdbc
			${springframework.version}
		
		
			org.springframework
			spring-tx
			${springframework.version}
		
		
			org.springframework
			spring-jms
			${springframework.version}
		
		
			org.springframework
			spring-orm
			${springframework.version}
		
		
			org.springframework
			spring-oxm
			${springframework.version}
		
		
			org.springframework
			spring-web
			${springframework.version}
		
		
			org.springframework
			spring-webmvc
			${springframework.version}
		
		
			org.springframework
			spring-websocket
			${springframework.version}
		
		
			org.springframework
			spring-webmvc-portlet
			${springframework.version}
		
		
			org.springframework
			spring-test
			${springframework.version}
			test
		
		
		
		
			commons-codec
			commons-codec
			${commons-codec.version}
		
		
			commons-io
			commons-io
			${commons-io.version}
		
		
			org.apache.commons
			commons-lang3
			${commons-lang3.version}
		
		
			org.apache.commons
			commons-pool2
			${commons-pool2.version}
		
		
		
			javax.servlet
			javax.servlet-api
			${servlet.version}
			provided
		
		
		
			org.slf4j
			slf4j-api
			${slf4j.version}
		
		
			org.slf4j
			jcl-over-slf4j
			${jcl.version}
		
		
			ch.qos.logback
			logback-core
			${logback.version}
		
		
			ch.qos.logback
			logback-classic
			${logback.version}
		
		
			org.logback-extensions
			logback-ext-spring
			${logback-ext-spring.version}
		
	

	
	
		
			dev
			
				dev
			
			
				true
			
		
		
			sit
			
				sit
			
		
		
			uat
			
				uat
			
		
		
			pit
			
				pit
			
		
	

	
		webapp
		
		
			
				
				true
				src/main/resources
			
		
		
			
				org.apache.maven.plugins
				maven-war-plugin
				3.2.2
				
					UTF-8
					
						
							true
							
							src/main/webapp
							
								**/web.xml
							
						
					
				
			
		

	

说明:
1)编译时可以用install -Psit,带环境参数编译
2)注意maven-war-plugin版本要用高版本,用2.2会有web.xml中文注释乱码问题

6、测试下工具类功能(properties文件参数获取)
在src下建立测试实体类Student.java

package com.study.testBean;

public class Student {
	private String name;
	private int age;

	public void sayHello() {
		System.out.println("student say hello." + " name=" + name + " age=" + age);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

在src/test/java下建立MyTest.java

package com.study.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.study.util.ApplicationContextHolder;
import com.study.util.PropertiesUtil;
import com.study.util.ReflectInvokeUtil;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:applicationContext.xml", "classpath:spring-mvc.xml" })
@ActiveProfiles("sit")
public class MyTest {

	@Value("${flag}")
	private String flag;

	@Test
	public void testInvoke() throws Exception {
		System.out.println("第一个测试方法*******");
		ApplicationContext ac = ApplicationContextHolder.getApplicationContext();
		ReflectInvokeUtil.invokeMethod("com.study.testBean.Student", "sayHello", null);
	}
	
	@Test
	public void testById() {
		System.out.println("第二个测试方法*******");
		ApplicationContext ac = ApplicationContextHolder.getApplicationContext();
		System.out.println("**********");
	}
	
	@Test
	public void testProperties() {
		System.out.println("第三个测试方法*******");
		System.out.println("flag is:" + flag);
		System.out.println("appName is:" + PropertiesUtil.getProperties("appName"));
	}
}

 

你可能感兴趣的:(JAVA基础)