1.新建一个web项目
参考之前的博文使用Maven构建web项目这一章节
2.修改pom.xml,添加Spring依赖
<span style="font-size:12px;"><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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.carson.demo</groupId>
<artifactId>spring3</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring3 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<!-- 添加Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<!-- 添加Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>spring3</finalName>
<outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory>
</build>
</project>
</span>
注意:之前我们在构建web项目时,修改了Maven的编译目录,我这里修改为“src/webapp/WEB-INF/classes”,之后新建了HelloWorld的测试类,报错无法找到或者加载主类,这里只需要在pom.xml文件Maven的build标签下加入<outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory>,问题解决。
3.添加Spring配置文件,applicationContext.xml
Spring的配置文件applicationContext.xml默认是放在WEB-INF/目录下,Maven构建的web项目资源文件统一放在src/main/resources下。这里给出了两种方式注入bean,我选择了注解方式,代码如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 方法一:使用xml -->
<!-- <bean id="studentService" class="com.carson.spring.service.StudentServiceImpl"></bean> -->
<!-- 方法二:使用注解,告诉spring容器到base-package路径下去扫描所有的类,从而找到被注解的类。-->
<context:component-scan base-package="com.carson.spring.service"/>
</beans>
4.测试
写了简单的测试类,测试Spring项目是否构建成功,这里贴出测试代码,供参考。
student.java
package com.carson.spring.model;
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
StudentService.java
package com.carson.spring.service;
import com.carson.spring.model.Student;
public interface StudentService {
public void save(Student student);
}
StudentServiceImpl.java
package com.carson.spring.service;
import org.springframework.stereotype.Service;
import com.carson.spring.model.Student;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Override
public void save(Student student) {
System.out.println("------>>>>>>>Student name:"+student.getName());
}
}
StudentBeanTest.java
package com.carson.spring.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.carson.spring.model.Student;
import com.carson.spring.service.StudentService;
public class StudentBeanTest {
public static void main(String[] args) {
// 使用ApplicationContext来初始化系统
BeanFactory factory = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println(factory);
//通过spring获取实例对象
StudentService studentService = (StudentService) factory.getBean("studentService");
Student student = new Student();
student.setName("carson");
studentService.save(student);
}
}
执行测试类,控制台打印------>>>>>>>Student name:carson,到此为止,Spring项目构建成功。
另外说下,在这里我们没有用到web.xml.后续章节会讲到怎么在web.xml添加spring。