您的位置:首页 > 编程语言 > Java开发

Java后台框架篇--Spring应用上下文配置:Xml方式

2017-09-29 19:14 573 查看


前言

        之前的章节我们讲解了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配置


根应用上下文的配置

[html] view
plain copy

<?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的定义。

[java] view
plain copy

package com.gxz;  

  

public class StudentImp implements Student {  

    @Override  

    public String sayHi(String name) {  

        return "Hi," + name;  

    }  

}  

        这个bean studentImp实现了接口Student,接口代码如下所示。

[java] view
plain copy

package com.gxz;  

  

public interface Student {  

    String sayHi(String name);  

}  

DispatcherServlet应用上下文的配置

[html] view
plain copy

<?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的定义。

[java] view
plain copy

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的映射模式。
        比如说,DispatcherServlet的映射模式是/,方法sayHi的@RequestMapping的映射模式是/,那么请求为/,将会映射到该方法,比如url为http://localhost:8080/Test/。
        比如说,DispatcherServlet的映射模式是/,方法say的@RequestMapping的映射模式是/say,那么请求为/say、/say/,将会映射到该方法,比如url为http://localhost:8080/Test/say、http://localhost:8080/Test/say/。
        比如说,DispatcherServlet的映射模式是/do/*,方法sayHi的@RequestMapping的映射模式是/,那么请求为/do/(注意不是/do),将会映射到该方法,比如url为http://localhost:8080/Test/do/。注意,url为http://localhost:8080/Test/do不会映射到该方法。
        DispatcherServlet的映射模式是/do/*,方法say的@RequestMapping的映射模式是/say,那么请求为/do/say、/do/say/,将会映射到该方法,比如url为http://localhost:8080/Test/do/say、http://localhost:8080/Test/do/say/。
        DispatcherServlet的映射模式是/do/*,方法say的@RequestMapping的映射模式是/say/,那么请求为/do/say/(不是/do/say),将会映射到该方法,比如url为http://localhost:8080/Test/do/say/。注意,url为http://localhost:8080/Test/do/say,不会映射到该方法。

        以上代码@RequestMapping(value="/say", params="name"),那么什么样的请求才会映射到该方法呢?params="name"表示请求一定要有参数name。而根据前面章节对DispatcherServlet的配置,DispatcherServlet的映射模式是/,所以请求/say?name=xxx、/say/?name=xxx,就会映射到该方法,比如url为http://localhost:8080/Test/say?name=xxx,http://localhost:8080/Test/say/?name=xxx。注意,以下请求不会映射到这个方法。http://localhost:8080/testSpring/say/,http://localhost:8080/testSpring/say。

        bean studentController需要注入一个Student类型(接口)的bean,而bean studentImp 正是Student的实现,所以我们有了这样的配置。<property name="student" ref="studentImp" />。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: