前言
之前的章节我们讲解了Spring的两种启动方式,分别是web.xml方式,java编程方式。如同我们讲过的那样,启动Spring,实际上是启动一个容器,创建一组应用上下文。既然需要创建应用上下文,就必须配置应用上下文,指导应用上下文如何工作。如同启动Spring一样,配置Spring应用上下文也有三种方式,分别是xml配置,混合配置,java编程配置。
bean
使用Spring,我们会不断谈到bean,那么bean到底是什么东西?从狭义上讲,凡是实现类(区别于接口)都可作为bean。在Spring看来,凡是有注解@Component、@Service、@Controller、@Repository等的类都是bean。@Service、@Controller、@Repository是@Component更为具体的注解,@Service表示业务类的bean,@Controller表示控制类的bean,@Repository表示持久化的bean,而@Component是一个通用的bean。就语法上讲,@Service、@Controller、@Repository都可以被@Component替换,但是,前三者是更为具体的bean,拥有更为丰富的功能,而且看名字就能知道bean的类型,不像@Component这么笼统。实际编程中,我们建议使用@Service、@Controller、@Repository注解不同的bean。由此看来,bean就是一个可以实现业务逻辑、控制流程或是持久化的实现类。
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-4.0.xsd">
<bean name="studentImp" class="com.gxz.StudentImp" />
</beans>
以上是文件WEB-INF\rootContext.xml的内容,它是根应用上下文的配置。这些配置告诉Spring管理bean——com.gxz.StudentImp,在适当的时候实例化它,取名为studentImp。以下代码是bean studentImp的定义。
package com.gxz;
public class StudentImp implements Student {
@Override
public String sayHi(String name) {
return "Hi," + name;
}
}
这个bean studentImp实现了接口Student,接口代码如下所示。
package com.gxz;
public interface Student {
String sayHi(String name);
}
DispatcherServlet应用上下文的配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven />
<bean name="studentController" class="com.gxz.StudentController">
<property name="student" ref="studentImp" />
</bean>
</beans>
以上是文件WEB-INF\servletContext.xml的内容,它是DispatcherServlet应用上下文的配置。 这些配置告诉Spring管理bean——com.gxz.StudentController,在适当的时候实例化它,取名为studentController,并注入bean studentImp 。<mvc:annotation-driven />这个配置表示注解驱动,有了这项配置,Spring就可以在控制器(bean studentController就是一个控制器)上使用注解@RequestMapping、@RequestBody、@RequestParam、@PathParam、@ResponseBody等注解。关于控制器、这些注解,后续章节我们再详细讲解。以下是bean studentController的定义。
package com.gxz;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentController {
private Student student;
@ResponseBody
@RequestMapping("/")
public String sayHi() {
return "Hi!";
}
@ResponseBody
@RequestMapping(value="/say", params="name")
public String say(@RequestParam("name") String name) {
return student.sayHi(name);
}
public void setStudent(Student student) {
this.student = student;
}
}
我们注意到,bean studentController有一个注解@Controller,表示该bean是一个控制器。我们知道DispatcherServlet的映射模式/,它将会映射所有的url请求,并把请求映射到控制器的方法上。所以,我们在DispatcherServlet应用上下文管理控制器bean studentController。至于注解@ResponseBody、@RequestMapping的含义,我们后续章节会进行讲解。现在可以简单的这么理解,@ResponseBody表示该方法可以接收映射,@RequestMapping是一种映射模式,相对于DispatcherServlet的映射模式。