spring框架学习过程的小知识
2015-12-09 14:57
387 查看
1.@ResponseBody的区别
浏览器输入:http://localhost:8080/SpringTest/user/index
返回结果: index (字符串)
浏览器输入:http://localhost:8080/SpringTest/user/index
返回结果: index.jsp界面的内容
2.spring xml头文件xmlns和xsi的意思
即是说:我如果一个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.
B.
C.
由于现在多用context:scan扫描包的形式,所以其他两种可以不用了!
转载:
4.为什么有多个contextConfigLocation,上下文
答:一个项目可能面向多个服务类型,那么所处理返回的一些数据也不一样,通过多个DipatcherServlet,可以灵活的管理不同的上下文。
一个项目只能有一个全局的上下文:
但是能有多个DispatcherServlet。互相用不同的spring.xml定义不同的 处理方式/过滤方式 即可。
5.使用 redirect: 重定向
[code] @RequestMapping(value = "/index") @ResponseBody public String helloworld(String json) { String result = null; return "index"; }
浏览器输入:http://localhost:8080/SpringTest/user/index
返回结果: index (字符串)
[code] @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的意思
[code]<?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-2.0.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.
[code] <context:component-scan base-package="com.csdn.myspring"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
由于现在多用context:scan扫描包的形式,所以其他两种可以不用了!
转载:
[code]如果想使用 @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,可以灵活的管理不同的上下文。
一个项目只能有一个全局的上下文:
[code] <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: 重定向
[code] /** * 新注册一个用户,成功后返回登录页面 * * @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"; }
相关文章推荐
- Java常见内存溢出异常分析(OutOfMemoryError)
- Java观察者模式例子
- java方法调用绑定
- java对象拷贝之BeanUtils.copyProperties() 用法
- spring中的内部bean
- 百度地图在混淆打包时的注意事项
- [转]Eclipse工具使用技巧总结
- java.lang.reflect.MalformedParameterizedTypeException
- Java观察者设计模式(Observable和Observer)
- java 调用webservice的各种方法总结
- java二进制,运算符算法说明
- 转 详解Java解析XML的四种方法
- Java学习笔记----异常
- java中静态代码块的用法 static用法详解
- java开发之IO流
- Java FX中TreeView节点选中和双击事件监听
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- java异常——捕获异常+再次抛出异常与异常链
- [javase学习笔记]-1.4 Java程序开发之初体验--Hello World
- 使用Java驱动操作Cassandra