天天看點

Spring 學習筆記 BeanUtilsPropertyUtils的copyProperties()的差別3 Spring 源碼學習4 spring mvc Controller中使用@Value無法擷取屬性值5 Spring 注解6 Spring 事務配置7: SpEL文法

主要記錄項目中用到的 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文法