Spring框架学习之--搭建spring框架
Posted 爱穿新衣服的姑凉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring框架学习之--搭建spring框架相关的知识,希望对你有一定的参考价值。
此文介绍搭建一个最最简单的spring框架的步骤
一、创建一个maven项目
二、在pom.xml文件中添加依赖导入spring框架运行需要的相关jar包
注意:在引入jar包之后会出现org.junit里面的包无法使用,参考https://blog.csdn.net/he99774/article/details/78254262
-
<dependencies> <!-- 框架运行时的基本依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.2.RELEASE</version> </dependency> <!-- 数据库相关依赖 --> <dependency> <groupId>org.unitils</groupId> <artifactId>unitils-core</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.dbunit</groupId> <artifactId>dbunit</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.greenline.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <dependency> <groupId>io.codis.jodis</groupId> <artifactId>jodis</artifactId> <version>0.3.3-GH</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> <type>jar</type> </dependency> <!-- http协议测试时的依赖 --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> <type>jar</type> </dependency> <!--java对象和json相互转化--> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib-ext-spring</artifactId> <version>1.0.2</version> </dependency> <!-- log4j日志的依赖 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <!--其他一些通用的依赖--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>2.1_3</version> <type>jar</type> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.3</version> <type>jar</type> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> <type>jar</type> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.4</version> <type>jar</type> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> <type>jar</type> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20090211</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.32</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> </dependencies>
三、添加resources文件
- 添加配置文件:applicationContext.xml文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 6 7 <!-- 加载资源文件,所有的资源文件都集中加载,不要分散到其他spring配置文件中,否则会找不到 --> 8 <bean id="propertyConfigurer" 9 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 10 <property name="ignoreUnresolvablePlaceholders" value="true" /> 11 <property name="locations"> 12 <list> 13 <value>classpath:jdbc.properties</value> 14 </list> 15 </property> 16 </bean> 17 18 <!--引入bean--> 19 <bean id="consultDataSource" 20 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 21 <property name="driverClassName" value="${consult.jdbc.driverClassName}" /> 22 <property name="url" value="${consult.jdbc.url}" /> 23 <property name="username" value="${consult.jdbc.username}" /> 24 <property name="password" value="${consult.jdbc.password}" /> 25 </bean> 26 27 <bean id="consultTemplate" 28 class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> 29 <constructor-arg ref="consultDataSource" /> 30 </bean> 31 32 </beans>
- 添加数据库配置文件:jdbc.properties 配置常用的数据库信息
1 consult.jdbc.driverClassName=com.mysql.jdbc.Driver 2 consult.jdbc.url=jdbc:mysql://xxx.xxx.xxxx.xxx:xxxx/consult?useUnicode=true&characterEncoding=utf-8&tinyInt1isBit=false 3 consult.jdbc.username=consult 4 consult.jdbc.password=123456
- 添加日志配置文件:log4j.properties
1 log4j.rootLogger=info,stdout,FILE 2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 4 log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p [%t] (%F:%L) - %m%n 5 log4j.appender.stdout.Encoding=UTF-8 6 log4j.appender.FILE=org.apache.log4j.FileAppender 7 log4j.appender.FILE.File=./logs/greentest.log 8 log4j.appender.FILE.Append=false 9 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 10 log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p [%t] (%F:%L) - %m%n
四、添加一个基类BaseCase 无需重复进行数据库等公共资源的初始化
五、将所有测试账号和数据等信息集中放在一个类BaseData 中,方便管理
以上是关于Spring框架学习之--搭建spring框架的主要内容,如果未能解决你的问题,请参考以下文章