需求:Maven项目整合Spring
IDE工具:Eclipse
搭建环境:jdk1.8 tomcat8.5
目前的项目结构如下图:
开整。
1. 添加依赖
1.1 主要有Spring核心依赖,Spring dao依赖,Spring web依赖,Spring test依赖,如下:
<!-- Spring依赖 -->
<!-- Spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- Spring dao依赖 -->
<!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- Spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- Spring test依赖,集成junit方便做单元测试和集成测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
1.2 为了在实体类中不需要写Getter和Setter方法等,添加了lombok依赖(非必要环节),如下:
<!-- 添加lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
2. 添加依赖后,更新maven项目,创建一个实体类,方便之后的测试,实体类如下:
package com.yzpt.entity;
import lombok.Getter;
import lombok.Setter;
/**
* @author yzp
* 图书对象
*/
@Setter
@Getter
public class Book {
private String bookId; //书本id
private String bookName; //书本名称
private String writer; //作者
private String publishingHouse; //出版社
}
3. 编写Spring配置文件,Spring配置文件命名为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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 配置自动扫描的(Spring IOC 容器不扫描Controller) -->
<context:component-scan base-package="com.yzpt">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
</beans>
4. 为了能让Spring配置文件在项目启动的时候生效,在web.xml中添加ContextLoader监听器以及Spring配置文件的路径,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>SSMTest1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 指定Spring配置文件的路径,添加ContextLoader监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
5.接下来进行测试
5.1 在applicationContext.xml中添加一个Book的实例,如下:
<!-- 测试类 -->
<bean id="book" class="com.yzpt.entity.Book">
<property name="bookId" value="10086"></property>
<property name="bookName" value="论客户的重要性"></property>
<property name="writer" value="test"></property>
<property name="publishingHouse" value="HaoYiTec"></property>
</bean>
5.2 编写测试类
在test文件夹下创建如下类,如图所示:
测试类内容如下:
package com.yzpt.Test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yzpt.entity.Book;
public class TestAll {
@Test
public void testSpring() {
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Book book = (Book) ac.getBean("book");
System.out.println(book.getBookId());
System.out.println(book.getBookName());
System.out.println(book.getWriter());
System.out.println(book.getPublishingHouse());
}
}
5.3 在testSpring()方法名边上右击Debug as,选择Junit Test,结果如下:
5.4 测试成功