主要记录项目中用到的 Spring的一些我不熟悉的知识点进行整理。
BeanUtilsPropertyUtils的copyProperties()的区别
链接:
http://jingyan.baidu.com/article/215817f7d55b871edb14235b.html
http://blog.sina.com.cn/s/blog_89ca421401015x2q.html
1 对比
1、 通过反射将一个对象的值赋值个另外一个对象(前提是对象中属性的名字相同)。
2、 BeanUtils.copyProperties(obj1,obj2); 经常闹混不知道是谁给谁赋值,“先付前(钱)”,或者直接看一下源码就知道了
3、 如果2中实例obj1为空对象,即值new了他的实例并没有赋值的话obj2对应的属性值也会被设置为空置。
4、BeanUtils与PropertyUtils对比(这里对比copyProperties方法)
PropertyUtils的copyProperties()方法几乎与BeanUtils.copyProperties()相同,主要的区别在于后者提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,在支持的数据类型范围内进行转换,BeanUtils 不支持这个功能,但是BeanUtils速度会更快一些。
主要支持转换类型如下:
- java.lang.BigDecimal
- java.lang.BigInteger
- boolean and java.lang.Boolean
- byte and java.lang.Byte
- char and java.lang.Character
- java.lang.Class
- double and java.lang.Double
- float and java.lang.Float
- int and java.lang.Integer
- long and java.lang.Long
- short and java.lang.Short
- java.lang.String
- java.sql.Date
- java.sql.Time
- java.sql.Timestamp
不支持java.util.Date转换,但支持java.sql.Date。如果开发中Date类型采用util而非sql.Date程序会抛出argument mistype异常。
2 扩展BeanUtils支持时间类型转换
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
/**
* 重写BeanUtils.copyProperties
*
* @author monkey
*/
public class BeanUtilsExtends extends BeanUtils {
static {
ConvertUtils.register(new DateConvert(), java.util.Date.class);
ConvertUtils.register(new DateConvert(), java.sql.Date.class);
}
public static void copyProperties(Object dest, Object orig) {
try {
BeanUtils.copyProperties(dest, orig);
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
}
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.commons.beanutils.Converter;
/**
* 重写日期转换
*
* @author houzhiqing
*/
public class DateConvert implements Converter {
public Object convert(Class arg0, Object arg1) {
String p = (String) arg1;
if (p == null || p.trim().length() == ) {
return null;
}
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.parse(p.trim());
} catch (Exception e) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.parse(p.trim());
} catch (ParseException ex) {
return null;
}
}
}
}
3 Spring 源码学习
1 http://blog.csdn.net/uestcyao/article/details/8243769
2 源码下载地址:http://pan.baidu.com/s/1o8F5oH8
java深入理解->多态的深入理解->设计模式理解->Spring能熟练使用->看spring源码基本没问题.另外可以看《SPRING技术内幕(第二版)》
首先要了解设计模式,这个是阅读大师源码的一个桥梁,很多代码看上去很多,其实就是一种模式。了解了模式后,整个关系图就清楚了。
还有善于分片阅读,找简单的读,可以先读spring jdbc,这部分的模板跟回调看起来会简单点。
然后再看IOC,看IOC之前必须对spring IOC原理掌握的很透彻,怎么扩展,怎么写胶水代码整合其他框架。建议熟读Spring-Reference。
然后再读源码,先学习怎么写出漂亮的代码,再学习怎么设计出漂亮的模式。
4 spring mvc Controller中使用@Value无法获取属性值
在使用spring mvc时,实际上是两个spring容器:
1,dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件
org.springframework.web.servlet.DispatcherServlet这里最终是使用WebApplicationContext parent =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 创建spring容器,代码在FrameworkServlet中
2,applicationContext.xml 是另外一个,也需要注入属性文件org.springframework.web.context.ContextLoaderListener在我们的service中可以拿到@Value注入的值,那是因为我们通常都会把获取属性文件定义在applicationContext.xml中,这样在 Controller中是取不到的,必须在dispatcher-servlet.xml 中把获取属性文件再定义一下
5 Spring 注解
1 @Resource
@Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象。 Spring确实支持使用@Resource通过JNDI lookup来解析对象,默认地,拥有与@Resource注解所提供名字相匹配的“bean name(bean名字)”的Spring管理对象会被注入
[email protected] 详解
Spring MVC之@RequestMapping 详解
引言:
前段时间项目中用到了REST风格来开发程序,但是当用POST、PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/json, 而且服务器端通过request.getReader() 打出的数据里确实存在浏览器提交的数据。为了找出原因,便对参数绑定(@RequestParam、 @RequestBody、 @RequestHeader 、 @PathVariable)进行了研究,同时也看了一下HttpMessageConverter的相关内容,在此一并总结。
简介:
@RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
RequestMapping注解有六个属性,下面我们把她分成三类进行说明。
1、 value, method;
value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
3、 params,headers;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
3 其他
Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解
Spring MVC之@RequestBody, @ResponseBody 详解
6 Spring 事务配置
待续。。。。
7: SpEL语法
SpEL语法