一、创建maven项目
点击Next,输入GroupId(组织)和 ArtifactId(项目)
点击Next
项目目录结构
src main -- 主要目录 java -- 源码 resources -- 资源文件,如properties
webapp -- Web项目有的,存放web页面和组件,暂时不管 test -- 测试目录 java
pom配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.codedot</groupId> <artifactId>SpringDemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- JDK版本 --> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- Spring 5.1.6 要求JDK必须是1.8以上--> <spring.version>5.1.16.RELEASE</spring.version> </properties> <dependencies> <!-- 核心容器 之 spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- 核心容器 之 spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!-- 核心容器 之 spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- 核心容器 之 spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- 核心容器 之 spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring Test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> </plugins> </build> </project>
二、配置文件
1. 创建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"> </beans>
要查看Spring可配置的命名空间,可以下载Spring的dist查看。
下载地址:http://repo.springsource.org/libs-release-local/ , 这是一个树结构。
直接地址:https://repo.spring.io/libs-release-local/org/springframework/spring/5.1.16.RELEASE/
下面添加了context的命名空间
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
三、创建bean
接口
package com.codedot.service; public interface HelloService { void saveInfo(String userName, String password); }
接口实现类
package com.codedot.service.impl; import com.codedot.service.HelloService; public class HelloServiceImpl implements HelloService { @Override public void saveInfo(String userName, String password){ System.out.println(String.format("用户名: %s, 密码:%s", userName, password)); } }
bean配置
在applicationContext.xml中配置
<!-- 这里的class必须是实现类 --> <bean id="helloService" class="com.codedot.service.impl.HelloServiceImpl"></bean>
四、Spring单元测试
在src/test/java下创建测试类。
main方法测试
import com.codedot.service.HelloService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringMainTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = ac.getBean(HelloService.class); helloService.saveInfo("codedot", "123456"); } }
spring-test集成Junit单元框架
需要引入以下依赖:
<!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version><!--此处需要注意的是,spring5 及以上版本要求 junit 的版本必须是 4.12 及以上,否则用不了--> <scope>test</scope> </dependency>
测试类
package com.codedot.test; import com.codedot.service.HelloService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //指定在单元测试启动的时候创建spring的工厂类对象 @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) //RunWith的value属性指定以spring test的SpringJUnit4ClassRunner作为启动类 //如果不指定启动类,默认启用的junit中的默认启动类 @RunWith(value = SpringJUnit4ClassRunner.class) public class SpringTest { @Autowired private HelloService helloService; @Test public void testSpring(){ helloService.saveInfo("codedot", "123456"); } }