天天看点

spring框架学习过程的小知识

[email protected]的区别

@RequestMapping(value = "/index") 
    @ResponseBody
    public String helloworld(String json) {  
        String result = null;
        return "index";  
    }  
           

浏览器输入:http://localhost:8080/SpringTest/user/index

返回结果: index (字符串)

@RequestMapping(value = "/index") 
    public String helloworld(String json) {  
        String result = null;
        return "index";  
    }  
           

浏览器输入:http://localhost:8080/SpringTest/user/index

返回结果: index.jsp界面的内容

2.spring xml头文件xmlns和xsi的意思

<?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-2.0.xsd">

... ...

</beans>



说明如下:

beans —— xml文件的根节点

xmlns ——是XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上一个namespace来区分这个xml文件和其他的xml文件,类似于java中的package。

xmlns:xsi ——是指xml文件遵守xml规范,xsi全名:xml schema instance,是指具体用到的schema资源文件里定义的元素所准守的规范。即/spring-beans-.xsd这个文件里定义的元素遵守什么标准 
xsi:schemaLocation——是指,本文档里的xml元素所遵守的规范
           

即是说:我如果一个spring.xml需要用到标签 < mvc >,那么我需要先引入命名空间:

xmlns:mvc=”http://www.springframework.org/schema/mvc”

而后我需要规定遵循的规范:

xsi:schemaLocation=”

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd”

也就是两个都给上.

3.使spring识别注解的三种方法

  • A.

    <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>

  • B.

    <context:annotation-config/>

  • C.
<context:component-scan base-package="com.csdn.myspring">
        <context:include-filter type="annotation"               
        expression="org.springframework.stereotype.Controller"/>
       </context:component-scan>
           

由于现在多用context:scan扫描包的形式,所以其他两种可以不用了!

转载:

如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 
一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。
   不过,呵呵,我们使用注解一般都会配置扫描包路径选项
<context:component-scan base-package=”XX.XX”/> 
    该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
           

4.为什么有多个contextConfigLocation,上下文

答:一个项目可能面向多个服务类型,那么所处理返回的一些数据也不一样,通过多个DipatcherServlet,可以灵活的管理不同的上下文。

一个项目只能有一个全局的上下文:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring/spring.xml</param-value>
    </context-param>

    全局的上下文被所有成员共享,都可以访问。
    (这也解释了为什么之前我的Bean被创建了两次!因为我全局的和子DispatcherServlet都读取的是同一个Srping.xml)
           

但是能有多个DispatcherServlet。互相用不同的spring.xml定义不同的 处理方式/过滤方式 即可。

5.使用 redirect: 重定向

/**
     * 新注册一个用户,成功后返回登录页面
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String registerUser(@RequestBody User user) {
        System.out.println(JSONObject.toJSONString(user));
        userService.registerUser(user);

        // "redirect:/user/test?username=222222"; 都可以
        // 同级目录"redirect:test?username=222222";
        // "redirect:http://www.baidu.com";
        return "redirect:http://localhost:8080/MySpring/index.jsp";
    }
           

继续阅读