代码改变世界

【接口自动化框架实践】3、applicationContext.xml及其相关配置(重点哦)

2019-11-21 20:28  昆山玉乐  阅读(419)  评论(0编辑  收藏  举报
1、src/main/resources下新建applicationContext.xml文件
resources右键--new--xml configuration file--Spring config,命名为applicationContext.xml

 

默认生成如下文件

 

我们添加一些配置信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
<!--注入配置信息,如环境配置-->
<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--以上是配置注入类-->
<property name="fileEncoding" value="utf-8"/>
<property name="locations">
<list>
<value>file:${basedir}/conf/${environment}.properties</value>
<!--basedir表示工程所在目录的通配,environment环境信息的通配,为pom中配置环境信息的变量,此2处通配在maven中install后会在target中转换为具体路径-->
<!--如转换失败,请查看pom中bulid里的resources配置是否正确-->
</list>
</property>
</bean>
 
</beans>

 

2、maven--clean、maven--install,去target中查看,你会发现applicationContext.xml中的路径还未正确转换
这是因为我们没有指定文件的读取路径
需要使用到pom.xml中的build标签的resources标签:resources标签是指定读取的配置文件或文件夹中的文件
resources标签内容需配置在<build></build>中
我们先使用,关于resources标签中的具体含义说明,我们在3.1中详解
<!--下面配置作用:构建过程往往会将资源文件从源路径复制到指定的目标路径,resources配置即给出各个资源在Maven项目中的具体路径-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>

 

3、配置后resources后,我们再次执行maven--clean、maven--install,去target中查看,你会发现applicationContext.xml中的路径已经正确转换
 
4、pom中配置plugins标签
plugins标签内容需配置在<build></build>中,是放各种maven的插件的
maven-compiler-plugin:项目编译时,指定编译器并设定参数
maven-surefire-plugin:maven里执行测试用例的插件,详见https://www.cnblogs.com/qyf404/p/5013694.html
 
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!--项目编译时,指定编译器并设定参数,厦门1.8表示jdk版本1.8-->
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!--maven里执行测试用例的插件-->
<version>2.19.1</version>
</plugin>
</plugins>