从零开始创建Spring Web项目
通过Maven创建Java Web项目,主要流程
Jar包依赖
<!--Spring框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
web.xml添加Spring 配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring/*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
测试代码
/**
* 定义测试方法
*/
@Component
public class AsyncTask {
public void exec() throws Exception{
System.out.println("准备睡觉");
Thread.sleep(1000);
System.out.println("醒了");
}
}
/**
* JUnit测试
*/
public class AsyncTaskTest extends AbstractTest {
@Autowired
private AsyncTask asyncTask;
@Test
public void testExec() throws Exception {
asyncTask.exec();
}
}
测试结果
准备睡觉
醒了