一、首先,模塊的組織更加的細(xì)致,從那么多的jar分包就看的出來:
Spring的構(gòu)建系統(tǒng)以及依賴管理使用的是Apache Ivy,從源碼包看出,也使用了Maven。
Maven確實(shí)是個好東西,好處不再多言,以后希望能進(jìn)一步用好它。
二、新特性如下:
Spring Expression Language (Spring表達(dá)式語言)
IoC enhancements/Java based bean metadata (Ioc增強(qiáng)/基于Java的bean元數(shù)據(jù))
General-purpose type conversion system and UI field formatting system (通用類型轉(zhuǎn)換系統(tǒng)和UI字段格式化系統(tǒng))
Object to XML mapping functionality (OXM) moved from Spring Web Services project (對象到XML映射功能從Spring Web Services項目移出)
Comprehensive REST support (廣泛的REST支持)
@MVC additions (@MVC增強(qiáng))
Declarative model validation (聲明式模型驗證)
Early support for Java EE 6 (提前對Java EE6提供支持)
Embedded database support (嵌入式數(shù)據(jù)庫的支持)
三、針對Java 5的核心API升級
1、BeanFactory接口盡可能返回明確的bean實(shí)例,例如:
T getBean(String name, Class requiredType)
Map getBeansOfType(Class type)
Spring3對泛型的支持,又進(jìn)了一步。個人建議泛型應(yīng)該多用,有百利而無一害!
2、Spring的TaskExecutor接口現(xiàn)在繼承自java.util.concurrent.Executor:
擴(kuò)展的子接口AsyncTaskExecutor支持標(biāo)準(zhǔn)的具有返回結(jié)果Futures的Callables。
任務(wù)計劃,個人還是更喜歡Quartz。
3、新的基于Java5的API和SPI轉(zhuǎn)換器
無狀態(tài)的ConversionService 和 Converters
取代標(biāo)準(zhǔn)的JDK PropertyEditors
類型化的ApplicationListener,這是一個實(shí)現(xiàn)“觀察者設(shè)計模式”使用的事件監(jiān)聽器。
基于事件的編程模式,好處多多,在項目中應(yīng)該考慮使用,基于事件、狀態(tài)遷移的設(shè)計思路,有助于理清軟件流程,和減少項目的耦合度。
四、Spring表達(dá)式語言
Spring表達(dá)式語言是一種從語法上和統(tǒng)一表達(dá)式語言(Unified EL)相類似的語言,但提供更多的重要功能。它可以在基于XML配置文件和基于注解的bean配置中使用,并作為基礎(chǔ)為跨Spring portfolio平臺使用表達(dá)式語言提供支持。
接下來,是一個表達(dá)式語言如何用于配置一個數(shù)據(jù)庫安裝中的屬性的示例:
<bean class="mycompany.RewardsTestDatabase">
<property name="databaseName"
value="#{systemProperties.databaseName}"/>
<property name="keyGenerator"
value="#{strategyBean.databaseKeyGenerator}"/>
</bean>
如果你更愿意使用注解來配置你的組件,那么這種功能同樣可用:
@Repository public class RewardsTestDatabase {
@Value("#{systemProperties.databaseName}")
public void setDatabaseName(String dbName) { … }
@Value("#{strategyBean.databaseKeyGenerator}")
public voidsetKeyGenerator(KeyGenerator kg) { … }
}
又多一種表達(dá)式語言,造輪子的運(yùn)動還在繼續(xù)中!
五、基于Java的bean元數(shù)據(jù)
JavaConfig項目中的一些核心特性已經(jīng)集成到了Spring中來,這意味著如下這些特性現(xiàn)在已經(jīng)可用了:
@Configuration
@Bean
@DependsOn
@Primary
@Lazy
@Import
@Value
又來一堆的注解,無語了,感覺還是配置文件方便?。海?/font>
這兒有一個例子,關(guān)于一個Java類如何使用新的JavaConfig特性提供基礎(chǔ)的配置信息:
package org.example.config;
@Configuration
public class AppConfig {
private @Value("#{jdbcProperties.url}") String jdbcUrl;
private @Value("#{jdbcProperties.username}") String username;
private @Value("#{jdbcProperties.password}") String password;
@Bean
public FooService fooService() {
return new FooServiceImpl(fooRepository());
}
@Bean
public FooRepository fooRepository() {
return new HibernateFooRepository(sessionFactory());
}
@Bean
public SessionFactory sessionFactory() {
// wire up a session factory
AnnotationSessionFactoryBean asFactoryBean =
new AnnotationSessionFactoryBean();
asFactoryBean.setDataSource(dataSource());
// additional config
return asFactoryBean.getObject();
}
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource(jdbcUrl, username, password);
}
}為了讓這段代碼開始生效,我們需要添加如下組件掃描入口到最小化的應(yīng)用程序上下文配置文件中:
<context:component-scan base-package="org.example.config"/>
<util:properties id="jdbcProperties" location="classpath:org/example/config/jdbc.properties"/>
六、在組件中定義bean的元數(shù)據(jù)
感覺Spring提供了越來越多的注解、元數(shù)據(jù),復(fù)雜性已經(jīng)超出了當(dāng)初帶來的方便本身!
七、通用類型轉(zhuǎn)換系統(tǒng)和UI字段格式化系統(tǒng)
Spring3加入了一個通用的類型轉(zhuǎn)換系統(tǒng),目前它被SpEL用作類型轉(zhuǎn)換,并且可能被一個Spring容器使用,用于當(dāng)綁定bean的屬性值的時候進(jìn)行類型轉(zhuǎn)換。
另外,還增加了一個UI字段格式化系統(tǒng),它提供了更簡單的使用并且更強(qiáng)大的功能以替代UI環(huán)境下的JavaBean的PropertyEditors,例如在SpringMVC中。
這個特性要好好研究下,通用類型轉(zhuǎn)換系統(tǒng)如果果如所言的話,帶來的好處還是很多的。
八、數(shù)據(jù)層
對象到XML的映射功能已經(jīng)從Spring Web Services項目移到了Spring框架核心中。它位于org.springframework.oxm包中。
OXM?研究下!時間真不夠!
九、Web層
在Web層最激動人心的新特性莫過于新增的對構(gòu)件REST風(fēng)格的web服務(wù)和web應(yīng)用的支持!另外,還新增加了一些任何web應(yīng)用都可以使用的新的注解。
服務(wù)端對于REST風(fēng)格的支持,是通過擴(kuò)展既有的注解驅(qū)動的MVC web框架實(shí)現(xiàn)的。
客戶端的支持則是RestTemplate類提供的。
無論服務(wù)端還是客戶端REST功能,都是使用HttpConverter來簡化對HTTP請求和應(yīng)答過程中的對象到表現(xiàn)層的轉(zhuǎn)換過程。
MarshallingHttpMessageConverter使用了上面提到的“對象到XML的映射機(jī)制”。
十、@MVC增強(qiáng)
新增了諸如@CookieValue 和 @RequestHeaders這樣的注解等。
十一、聲明式模型驗證
支持JSR 303,使用Hibernate Validator作為實(shí)現(xiàn)。
十二、提前對Java EE6提供支持
提供了使用@Async注解對于異步方法調(diào)用的支持(或者EJB 3.1里的 @Asynchronous)
另外,新增對JSR 303, JSF 2.0, JPA 2.0等的支持。
十三、嵌入式數(shù)據(jù)庫的支持
對于嵌入式的Java數(shù)據(jù)庫引擎提供了廣泛而方便的支持,諸如HSQL, H2, 以及Derby等。
這是不是代表一種潮流呢?數(shù)據(jù)庫向越來越小型化發(fā)展,甚至小型化到嵌入式了,我認(rèn)為這在桌面級應(yīng)用上還是很有市場的。
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/abigfrog/archive/2009/10/30/4748685.aspx
請查看此blog: http://conkeyn.javaeye.com/category/35770
HQL: Hibernate查詢語言
Hibernate配備了一種非常強(qiáng)大的查詢語言,這種語言看上去很像SQL。但是不要被語法結(jié)構(gòu)上的相似所迷惑,HQL是非常有意識的被設(shè)計為完全面向?qū)ο蟮牟樵儯梢岳斫馊缋^承、多態(tài) 和關(guān)聯(lián)之類的概念。
除了Java類與屬性的名稱外,查詢語句對大小寫并不敏感。 所以 SeLeCT 與 sELEct 以及 SELECT 是相同的,但是 org.hibernate.eg.FOO 并不等價于 org.hibernate.eg.Foo 并且 foo.barSet 也不等價于 foo.BARSET 。
本手冊中的HQL關(guān)鍵字將使用小寫字母. 很多用戶發(fā)現(xiàn)使用完全大寫的關(guān)鍵字會使查詢語句 的可讀性更強(qiáng), 但我們發(fā)現(xiàn),當(dāng)把查詢語句嵌入到Java語句中的時候使用大寫關(guān)鍵字比較難看。
Hibernate中最簡單的查詢語句的形式如下:
from eg.Cat
該子句簡單的返回eg.Cat 類的所有實(shí)例。通常我們不需要使用類的全限定名, 因為 auto-import (自動引入) 是缺省的情況。所以我們幾乎只使用如下的簡單寫法:
from Cat
大多數(shù)情況下, 你需要指定一個別名 , 原因是你可能需要 在查詢語句的其它部分引用到Cat
from Cat as cat
這個語句把別名cat 指定給類Cat 的實(shí)例, 這樣我們就可以在隨后的查詢中使用此別名了。關(guān)鍵字as 是可選的,我們也可以這樣寫:
from Cat cat
子句中可以同時出現(xiàn)多個類, 其查詢結(jié)果是產(chǎn)生一個笛卡兒積或產(chǎn)生跨表的連接。
from Formula, Parameter
from Formula as form, Parameter as param
查詢語句中別名的開頭部分小寫被認(rèn)為是實(shí)踐中的好習(xí)慣, 這樣做與Java變量的命名標(biāo)準(zhǔn)保持了一致 (比如,domesticCat )。
3.關(guān)聯(lián)(Association)與連接(Join)
我們也可以為相關(guān)聯(lián)的實(shí)體甚至是對一個集合中的全部元素指定一個別名, 這時要使用關(guān)鍵字join 。
from Cat as cat
inner join cat.mate as mate
left outer join cat.kittens as kitten
from Cat as cat left join cat.mate.kittens as kittens
from Formula form full join form.parameter param
受支持的連接類型是從ANSI SQL中借鑒來的。
inner join (內(nèi)連接)
left outer join (左外連接)
right outer join (右外連接)
full join (全連接,并不常用)
語句inner join , left outer join 以及 right outer join 可以簡寫。
from Cat as cat
join cat.mate as mate
left join cat.kittens as kitten
還有,一個"fetch"連接允許僅僅使用一個選擇語句就將相關(guān)聯(lián)的對象或一組值的集合隨著他們的父對象的初始化而被初始化,這種方法在使用到集合的情況下尤其有用,對于關(guān)聯(lián)和集合來說,它有效的代替了映射文件中的外聯(lián)接與延遲聲明(lazy declarations). 查看 第20.1節(jié) “ 抓取策略(Fetching strategies) ” 以獲得等多的信息。
from Cat as cat
inner join fetch cat.mate
left join fetch cat.kittens
一個fetch連接通常不需要被指定別名, 因為相關(guān)聯(lián)的對象不應(yīng)當(dāng)被用在 where 子句 (或其它任何子句)中。同時,相關(guān)聯(lián)的對象并不在查詢的結(jié)果中直接返回,但可以通過他們的父對象來訪問到他們。
注意fetch 構(gòu)造變量在使用了scroll() 或 iterate() 函數(shù)的查詢中是不能使用的。最后注意,使用full join fetch 與 right join fetch 是沒有意義的。
如果你使用屬性級別的延遲獲?。╨azy fetching)(這是通過重新編寫字節(jié)碼實(shí)現(xiàn)的),可以使用 fetch all properties 來強(qiáng)制Hibernate立即取得那些原本需要延遲加載的屬性(在第一個查詢中)。
from Document fetch all properties order by name
from Document doc fetch all properties where lower(doc.name) like '%cats%'
select 子句選擇將哪些對象與屬性返回到查詢結(jié)果集中. 考慮如下情況:
select mate
from Cat as cat
inner join cat.mate as mate
該語句將選擇mate s of other Cat s。(其他貓的配偶) 實(shí)際上, 你可以更簡潔的用以下的查詢語句表達(dá)相同的含義:
select cat.mate from Cat cat
查詢語句可以返回值為任何類型的屬性,包括返回類型為某種組件(Component)的屬性:
select cat.name from DomesticCat cat
where cat.name like 'fri%'
select cust.name.firstName from Customer as cust
查詢語句可以返回多個對象和(或)屬性,存放在 Object[] 隊列中,
select mother, offspr, mate.name
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr
或存放在一個List 對象中,
select new list(mother, offspr, mate.name)
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr
也可能直接返回一個實(shí)際的類型安全的Java對象,
select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr
假設(shè)類Family 有一個合適的構(gòu)造函數(shù).
你可以使用關(guān)鍵字as 給“被選擇了的表達(dá)式”指派別名:
select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat
這種做法在與子句select new map 一起使用時最有用:
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
from Cat cat
該查詢返回了一個Map 的對象,內(nèi)容是別名與被選擇的值組成的名-值映射。
HQL查詢甚至可以返回作用于屬性之上的聚集函數(shù)的計算結(jié)果:
select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat)
from Cat cat
受支持的聚集函數(shù)如下:
avg(...), sum(...), min(...), max(...)
count(*)
count(...), count(distinct ...), count(all...)
你可以在選擇子句中使用數(shù)學(xué)操作符、連接以及經(jīng)過驗證的SQL函數(shù):
select cat.weight + sum(kitten.weight)
from Cat cat
join cat.kittens kitten
group by cat.id, cat.weight
select firstName||' '||initial||' '||upper(lastName) from Person
關(guān)鍵字distinct 與all 也可以使用,它們具有與SQL相同的語義.
select distinct cat.name from Cat cat
select count(distinct cat.name), count(cat) from Cat cat
一個如下的查詢語句:
from Cat as cat
不僅返回Cat 類的實(shí)例, 也同時返回子類 DomesticCat 的實(shí)例. Hibernate 可以在from 子句中指定任何 Java 類或接口. 查詢會返回繼承了該類的所有持久化子類的實(shí)例或返回聲明了該接口的所有持久化類的實(shí)例。下面的查詢語句返回所有的被持久化的對象:
from java.lang.Object o
接口Named 可能被各種各樣的持久化類聲明:
from Named n, Named m where n.name = m.name
注意,最后的兩個查詢將需要超過一個的SQL SELECT .這表明order by 子句 沒有對整個結(jié)果集進(jìn)行正確的排序. (這也說明你不能對這樣的查詢使用Query.scroll() 方法.)
where 子句允許你將返回的實(shí)例列表的范圍縮小. 如果沒有指定別名,你可以使用屬性名來直接引用屬性:
from Cat where name='Fritz'
如果指派了別名,需要使用完整的屬性名:
from Cat as cat where cat.name='Fritz'
返回名為(屬性name等于)'Fritz'的Cat 類的實(shí)例。
select foo
from Foo foo, Bar bar
where foo.startDate = bar.date
將返回所有滿足下面條件的Foo 類的實(shí)例:存在如下的bar 的一個實(shí)例,其date 屬性等于 Foo 的startDate 屬性。 復(fù)合路徑表達(dá)式使得where 子句非常的強(qiáng)大,考慮如下情況:
from Cat cat where cat.mate.name is not null
該查詢將被翻譯成為一個含有表連接(內(nèi)連接)的SQL查詢。如果你打算寫像這樣的查詢語句
from Foo foo
where foo.bar.baz.customer.address.city is not null
在SQL中,你為達(dá)此目的將需要進(jìn)行一個四表連接的查詢。
= 運(yùn)算符不僅可以被用來比較屬性的值,也可以用來比較實(shí)例:
from Cat cat, Cat rival where cat.mate = rival.mate
select cat, mate
from Cat cat, Cat mate
where cat.mate = mate
特殊屬性(小寫)id 可以用來表示一個對象的唯一的標(biāo)識符。(你也可以使用該對象的屬性名。)
from Cat as cat where cat.id = 123
from Cat as cat where cat.mate.id = 69
第二個查詢是有效的。此時不需要進(jìn)行表連接!
同樣也可以使用復(fù)合標(biāo)識符。比如Person 類有一個復(fù)合標(biāo)識符,它由country 屬性 與medicareNumber 屬性組成。
from bank.Person person
where person.id.country = 'AU'
and person.id.medicareNumber = 123456
from bank.Account account
where account.owner.id.country = 'AU'
and account.owner.id.medicareNumber = 123456
第二個查詢也不需要進(jìn)行表連接。
同樣的,特殊屬性class 在進(jìn)行多態(tài)持久化的情況下被用來存取一個實(shí)例的鑒別值(discriminator value)。 一個嵌入到where子句中的Java類的名字將被轉(zhuǎn)換為該類的鑒別值。
from Cat cat where cat.class = DomesticCat
你也可以聲明一個屬性的類型是組件或者復(fù)合用戶類型(以及由組件構(gòu)成的組件等等)。永遠(yuǎn)不要嘗試使用以組件類型來結(jié)尾的路徑表達(dá)式(path-expression_r)(與此相反,你應(yīng)當(dāng)使用組件的一個屬性來結(jié)尾)。 舉例來說,如果store.owner 含有一個包含了組件的實(shí)體address
store.owner.address.city // 正確
store.owner.address // 錯誤!
一個“任意”類型有兩個特殊的屬性id 和class , 來允許我們按照下面的方式表達(dá)一個連接(AuditLog.item 是一個屬性,該屬性被映射為<any> )。
from AuditLog log, Payment payment
where log.item.class = 'Payment' and log.item.id = payment.id
注意,在上面的查詢與句中,log.item.class 和 payment.class 將涉及到完全不同的數(shù)據(jù)庫中的列。
在where 子句中允許使用的表達(dá)式包括大多數(shù)你可以在SQL使用的表達(dá)式種類:
數(shù)學(xué)運(yùn)算符+, -, *, /
二進(jìn)制比較運(yùn)算符=, >=, <=, <>, !=, like
邏輯運(yùn)算符and, or, not
in , not in , between , is null , is not null , is empty , is not empty , member of and not member of
"簡單的" case, case ... when ... then ... else ... end ,和 "搜索" case, case when ... then ... else ... end
字符串連接符...||... or concat(...,...)
current_date() , current_time() , current_timestamp()
second(...) , minute(...) , hour(...) , day(...) , month(...) , year(...) ,
EJB-QL 3.0定義的任何函數(shù)或操作:substring(), trim(), lower(), upper(), length(), locate(), abs(), sqrt(), bit_length()
coalesce() 和 nullif()
cast(... as ...) , 其第二個參數(shù)是某Hibernate類型的名字,以及extract(... from ...) ,只要ANSI cast() 和 extract() 被底層數(shù)據(jù)庫支持
任何數(shù)據(jù)庫支持的SQL標(biāo)量函數(shù),比如sign() , trunc() , rtrim() , sin()
JDBC參數(shù)傳入 ?
命名參數(shù):name , :start_date , :x1
SQL 直接常量 'foo' , 69 , '1970-01-01 10:00:01.0'
Java public static final 類型的常量 eg.Color.TABBY
關(guān)鍵字in 與between 可按如下方法使用:
from DomesticCat cat where cat.name between 'A' and 'B'
from DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' )
而且否定的格式也可以如下書寫:
from DomesticCat cat where cat.name not between 'A' and 'B'
from DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )
同樣, 子句is null 與is not null 可以被用來測試空值(null).
在Hibernate配置文件中聲明HQL“查詢替代(query substitutions)”之后,布爾表達(dá)式(Booleans)可以在其他表達(dá)式中輕松的使用:
<property name="hibernate.query.substitutions">true 1, false 0</property>
系統(tǒng)將該HQL轉(zhuǎn)換為SQL語句時,該設(shè)置表明將用字符 1 和 0 來 取代關(guān)鍵字true 和 false :
from Cat cat where cat.alive = true
你可以用特殊屬性size , 或是特殊函數(shù)size() 測試一個集合的大小。
from Cat cat where cat.kittens.size > 0
from Cat cat where size(cat.kittens) > 0
對于索引了(有序)的集合,你可以使用minindex 與 maxindex 函數(shù)來引用到最小與最大的索引序數(shù)。同理,你可以使用minelement 與 maxelement 函數(shù)來引用到一個基本數(shù)據(jù)類型的集合中最小與最大的元素。
from Calendar cal where maxelement(cal.holidays) > current date
from Order order where maxindex(order.items) > 100
from Order order where minelement(order.items) > 10000
在傳遞一個集合的索引集或者是元素集(elements 與indices 函數(shù)) 或者傳遞一個子查詢的結(jié)果的時候,可以使用SQL函數(shù)any, some, all, exists, in
select mother from Cat as mother, Cat as kit
where kit in elements(foo.kittens)
select p from NameList list, Person p
where p.name = some elements(list.names)
from Cat cat where exists elements(cat.kittens)
from Player p where 3 > all elements(p.scores)
from Show show where 'fizard' in indices(show.acts)
注意,在Hibernate3種,這些結(jié)構(gòu)變量- size , elements , indices , minindex , maxindex , minelement , maxelement - 只能在where子句中使用。
一個被索引過的(有序的)集合的元素(arrays, lists, maps)可以在其他索引中被引用(只能在where子句中):
from Order order where order.items[0].id = 1234
select person from Person person, Calendar calendar
where calendar.holidays['national day'] = person.birthDay
and person.nationality.calendar = calendar
select item from Item item, Order order
where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11
select item from Item item, Order order
where order.items[ maxindex(order.items) ] = item and order.id = 11
在[] 中的表達(dá)式甚至可以是一個算數(shù)表達(dá)式。
select item from Item item, Order order
where order.items[ size(order.items) - 1 ] = item
對于一個一對多的關(guān)聯(lián)(one-to-many association)或是值的集合中的元素, HQL也提供內(nèi)建的index() 函數(shù),
select item, index(item) from Order order
join order.items item
where index(item) < 5
如果底層數(shù)據(jù)庫支持標(biāo)量的SQL函數(shù),它們也可以被使用
from DomesticCat cat where upper(cat.name) like 'FRI%'
如果你還不能對所有的這些深信不疑,想想下面的查詢。如果使用SQL,語句長度會增長多少,可讀性會下降多少:
select cust
from Product prod,
Store store
inner join store.customers cust
where prod.name = 'widget'
and store.location.name in ( 'Melbourne', 'Sydney' )
and prod = all elements(cust.currentOrder.lineItems)
提示: 會像如下的語句
SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_order
FROM customers cust,
stores store,
locations loc,
store_customers sc,
product prod
WHERE prod.name = 'widget'
AND store.loc_id = loc.id
AND loc.name IN ( 'Melbourne', 'Sydney' )
AND sc.store_id = store.id
AND sc.cust_id = cust.id
AND prod.id = ALL(
SELECT item.prod_id
FROM line_items item, orders o
WHERE item.order_id = o.id
AND cust.current_order = o.id
)
查詢返回的列表(list)可以按照一個返回的類或組件(components)中的任何屬性(property)進(jìn)行排序:
from DomesticCat cat
order by cat.name asc, cat.weight desc, cat.birthdate
可選的asc 或desc 關(guān)鍵字指明了按照升序或降序進(jìn)行排序.
一個返回聚集值(aggregate values)的查詢可以按照一個返回的類或組件(components)中的任何屬性(property)進(jìn)行分組:
select cat.color, sum(cat.weight), count(cat)
from Cat cat
group by cat.color
select foo.id, avg(name), max(name)
from Foo foo join foo.names name
group by foo.id
having 子句在這里也允許使用.
select cat.color, sum(cat.weight), count(cat)
from Cat cat
group by cat.color
having cat.color in (eg.Color.TABBY, eg.Color.BLACK)
如果底層的數(shù)據(jù)庫支持的話(例如不能在MySQL中使用),SQL的一般函數(shù)與聚集函數(shù)也可以出現(xiàn) 在having 與order by 子句中。
select cat
from Cat cat
join cat.kittens kitten
group by cat
having avg(kitten.weight) > 100
order by count(kitten) asc, sum(kitten.weight) desc
注意group by 子句與 order by 子句中都不能包含算術(shù)表達(dá)式(arithmetic expression_rs).
第一步:需要文件包,其實(shí)就是dwr 3.0中例子所需要的包, dwr.jar 、 commons-fileupload-1.2.jar 、 commons-io-1.3.1.jar 。
第二步:編輯web.xml,添加dwr-invoke
< servlet >
< display-name > DWR Sevlet </ display-name >
< servlet-name > dwr-invoker </ servlet-name >
< servlet-class > org.directwebremoting.servlet.DwrServlet </ servlet-class >
< init-param >
< description > 是否打開調(diào)試功能 </ description >
< param-name > debug </ param-name >
< param-value > true </ param-value >
</ init-param >
< init-param >
< description > 日志級別有效值為: FATAL, ERROR, WARN (the default), INFO and DEBUG. </ description >
< param-name > logLevel </ param-name >
< param-value > DEBUG </ param-value >
</ init-param >
< init-param >
< description > 是否激活反向Ajax </ description >
< param-name > activeReverseAjaxEnabled </ param-name >
< param-value > true </ param-value >
</ init-param >
< init-param >
< description > 在WEB啟動時是否創(chuàng)建范圍為application的creator </ description >
< param-name > initApplicationScopeCreatorsAtStartup </ param-name >
< param-value > true </ param-value >
</ init-param >
< init-param >
< description > 在WEB啟動時是否創(chuàng)建范圍為application的creator </ description >
< param-name > preferDataUrlSchema </ param-name >
< param-value > false </ param-value >
</ init-param >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > dwr-invoker </ servlet-name >
< url-pattern > /dwr/* </ url-pattern >
</ servlet-mapping >
<servlet>
<display-name>DWR Sevlet</display-name>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<description>是否打開調(diào)試功能</description>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>日志級別有效值為: FATAL, ERROR, WARN (the default), INFO and DEBUG.</description>
<param-name>logLevel</param-name>
<param-value>DEBUG</param-value>
</init-param>
<init-param>
<description>是否激活反向Ajax</description>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>在WEB啟動時是否創(chuàng)建范圍為application的creator</description>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>在WEB啟動時是否創(chuàng)建范圍為application的creator</description>
<param-name>preferDataUrlSchema</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
第三步:創(chuàng)建上傳類FileUpload.java,編輯代碼,內(nèi)容如下:
package learn.dwr.upload_download;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
public class FileUpload {
public BufferedImage uploadFiles(BufferedImage uploadImage,
String uploadFile, String color) {
return uploadImage;
}
public String uploadFile(InputStream uploadFile, String filename)
throws Exception {
WebContext webContext = WebContextFactory.get();
String realtivepath = webContext.getContextPath() + "/upload/" ;
String saveurl = webContext.getHttpServletRequest().getSession()
.getServletContext().getRealPath( "/upload" );
File file = new File(saveurl + "/" + filename);
int available = uploadFile.available();
byte [] b = new byte [available];
FileOutputStream foutput = new FileOutputStream(file);
uploadFile.read(b);
foutput.write(b);
foutput.flush();
foutput.close();
uploadFile.close();
return realtivepath + filename;
}
private BufferedImage scaleToSize(BufferedImage uploadImage) {
AffineTransform atx = new AffineTransform();
atx
.scale(200d / uploadImage.getWidth(), 200d / uploadImage
.getHeight());
AffineTransformOp atfOp = new AffineTransformOp(atx,
AffineTransformOp.TYPE_BILINEAR);
uploadImage = atfOp.filter(uploadImage, null );
return uploadImage;
}
private BufferedImage grafitiTextOnImage(BufferedImage uploadImage,
String uploadFile, String color) {
if (uploadFile.length() < 200 ) {
uploadFile += uploadFile + " " ;
}
Graphics2D g2d = uploadImage.createGraphics();
for ( int row = 0 ; row < 10 ; row++) {
String output = "" ;
if (uploadFile.length() > (row + 1 ) * 20 ) {
output += uploadFile.substring(row * 20 , (row + 1 ) * 20 );
} else {
output = uploadFile.substring(row * 20 );
}
g2d.setFont( new Font( "SansSerif" , Font.BOLD, 16 ));
g2d.setColor(Color.blue);
g2d.drawString(output, 5 , (row + 1 ) * 20 );
}
return uploadImage;
}
}
package learn.dwr.upload_download;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
/**
* title: 文件上傳
* @author Administrator
* @時間 2009-11-22:上午11:40:22
*/
public class FileUpload {
/**
* @param uploadImage 圖片文件流
* @param uploadFile 需要用簡單的文本文件,如:.txt文件,不然上傳會出亂碼
* @param color
* @return
*/
public BufferedImage uploadFiles(BufferedImage uploadImage,
String uploadFile, String color) {
// uploadImage = scaleToSize(uploadImage);
// uploadImage =grafitiTextOnImage(uploadImage, uploadFile, color);
return uploadImage;
}
/**
* 文件上傳時使用InputStream類進(jìn)行接收,在DWR官方例中是使用String類接收簡單內(nèi)容
*
* @param uploadFile
* @return
*/
public String uploadFile(InputStream uploadFile, String filename)
throws Exception {
WebContext webContext = WebContextFactory.get();
String realtivepath = webContext.getContextPath() + "/upload/";
String saveurl = webContext.getHttpServletRequest().getSession()
.getServletContext().getRealPath("/upload");
File file = new File(saveurl + "/" + filename);
// if (!file.exists()) {
// file.mkdirs();
// }
int available = uploadFile.available();
byte[] b = new byte[available];
FileOutputStream foutput = new FileOutputStream(file);
uploadFile.read(b);
foutput.write(b);
foutput.flush();
foutput.close();
uploadFile.close();
return realtivepath + filename;
}
private BufferedImage scaleToSize(BufferedImage uploadImage) {
AffineTransform atx = new AffineTransform();
atx
.scale(200d / uploadImage.getWidth(), 200d / uploadImage
.getHeight());
AffineTransformOp atfOp = new AffineTransformOp(atx,
AffineTransformOp.TYPE_BILINEAR);
uploadImage = atfOp.filter(uploadImage, null);
return uploadImage;
}
private BufferedImage grafitiTextOnImage(BufferedImage uploadImage,
String uploadFile, String color) {
if (uploadFile.length() < 200) {
uploadFile += uploadFile + " ";
}
Graphics2D g2d = uploadImage.createGraphics();
for (int row = 0; row < 10; row++) {
String output = "";
if (uploadFile.length() > (row + 1) * 20) {
output += uploadFile.substring(row * 20, (row + 1) * 20);
} else {
output = uploadFile.substring(row * 20);
}
g2d.setFont(new Font("SansSerif", Font.BOLD, 16));
g2d.setColor(Color.blue);
g2d.drawString(output, 5, (row + 1) * 20);
}
return uploadImage;
}
}
第四步:添加到dwr.xml
<create creator= "new" >
<param name= "class" value= "learn.dwr.upload_download.FileUpload" />
</create>
<create creator="new">
<param name="class" value="learn.dwr.upload_download.FileUpload" />
</create>
第五步:添加前臺html代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
< title > 二進(jìn)制文件處理,文件上傳 </ title >
< script type = 'text/javascript' src = '/learnajax/dwr/interface/FileUpload.js' > </ script >
< script type = 'text/javascript' src = '/learnajax/dwr/engine.js' > </ script >
< script type = 'text/javascript' src = '/learnajax/dwr/util.js' > </ script >
< script type = 'text/javascript' >
function uploadFiles(){
var uploadImage = dwr .util.getValue("uploadImage");
FileUpload.uploadFiles(uploadImage, "", "", function(imageURL) {
alert(imageURL);
dwr.util.setValue('image', imageURL);
});
}
function uploadFile(){
var uploadFile = dwr .util.getValue("uploadFile");
//var uploadFile = document .getElementById("uploadFile").value;
var uploadFile uploadFile_temp = uploadFile.value.replace("\\","/");
var filenames = uploadFile .value.split("/");
var filename = filenames [filenames.length-1];
//var e extension = e[e.length-1];
FileUpload.uploadFile(uploadFile,filename,function(data){
var file_a = document .getElementById("file_a");
file_a.href = data ;
file_a.innerHTML = data ;
document.getElementById("filediv") .style.display = "" ;
});
}
</ script >
</ head >
< body >
< table border = "1" cellpadding = "3" width = "50%" >
< tr >
< td > Image </ td >
< td > < input type = "file" id = "uploadImage" /> </ td >
< td > < input type = "button" onclick = "uploadFiles()" value = "upload" /> < div id = "image.container" > </ div > </ td >
</ tr >
< tr >
< td > File </ td >
< td > < input type = "file" id = "uploadFile" /> </ td >
< td > < input type = "button" onclick = "uploadFile()" value = "upload" /> < div id = "file.container" > </ div > </ td >
</ tr >
< tr >
< td colspan = "3" > </ td >
</ tr >
</ table >
< img id = "image" src = "javascript:void(0);" />
< div id = "filediv" style = "display:none;" >
< a href = "" id = "file_a" > 上傳的文件 </ a >
</ div >
</ body >
</ html >
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>二進(jìn)制文件處理,文件上傳</title>
<script type='text/javascript' src='/learnajax/dwr/interface/FileUpload.js'></script>
<script type='text/javascript' src='/learnajax/dwr/engine.js'></script>
<script type='text/javascript' src='/learnajax/dwr/util.js'></script>
<script type='text/javascript' >
function uploadFiles(){
var uploadImage = dwr.util.getValue("uploadImage");
FileUpload.uploadFiles(uploadImage, "", "", function(imageURL) {
alert(imageURL);
dwr.util.setValue('image', imageURL);
});
}
function uploadFile(){
var uploadFile = dwr.util.getValue("uploadFile");
//var uploadFile =document.getElementById("uploadFile").value;
var uploadFile_temp = uploadFile.value.replace("\\","/");
var filenames = uploadFile.value.split("/");
var filename = filenames[filenames.length-1];
//var extension = e[e.length-1];
FileUpload.uploadFile(uploadFile,filename,function(data){
var file_a= document.getElementById("file_a");
file_a.href=data;
file_a.innerHTML=data;
document.getElementById("filediv").style.display="";
});
}
</script>
</head>
<body>
<table border="1" cellpadding="3" width="50%">
<tr>
<td>Image</td>
<td><input type="file" id="uploadImage" /></td>
<td><input type="button" onclick="uploadFiles()" value="upload"/><div id="image.container"> </div></td>
</tr>
<tr>
<td>File</td>
<td><input type="file" id="uploadFile" /></td>
<td><input type="button" onclick="uploadFile()" value="upload"/><div id="file.container"> </div></td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
</table>
<img id="image" src="javascript:void(0);"/>
<div id="filediv" style="display:none;">
<a href="" id="file_a">上傳的文件</a>
</div>
</body>
</html>
添加進(jìn)度條么,就需要用reverse ajax 進(jìn)行配合使用了。
本文轉(zhuǎn)自:http://www.aygfsteel.com/xylz/
DWR 作為Ajax遠(yuǎn)程調(diào)用的服務(wù)端得到了很多程序員的追捧,在DWR的2.x版本中已經(jīng)集成了Guice的插件。
老套了,我們還是定義一個HelloWorld的服務(wù)吧,哎,就喜歡HelloWorld,不怕被別人罵!
1 public interface HelloWorld {
2
3 String sayHello();
4
5 Date getSystemDate();
6 }
7
然后寫一個簡單的實(shí)現(xiàn)吧。
1 public class HelloWorldImpl implements HelloWorld {
2
3 @Override
4 public Date getSystemDate() {
5 return new Date();
6 }
7
8 @Override
9 public String sayHello() {
10 return "Hello, guice";
11 }
12 }
13
然后是與dwr有關(guān)的東西了,我們寫一個dwr的listener來注入我們的模塊。
1 package cn.imxylz.study.guice.web.dwr;
2
3 import org.directwebremoting.guice.DwrGuiceServletContextListener;
4
5 /**
6 * @author xylz (www.imxylz.cn)
7 * @version $Rev: 105 $
8 */
9 public class MyDwrGuiceServletContextListener extends DwrGuiceServletContextListener{
10
11 @Override
12 protected void configure() {
13 bindRemotedAs("helloworld", HelloWorld. class ).to(HelloWorldImpl. class ).asEagerSingleton();
14 }
15 }
16
這里使用bindRemotedAs來將我們的服務(wù)開放出來供dwr遠(yuǎn)程調(diào)用。
剩下的就是修改web.xml,需要配置一個dwr的Servlet并且將我們的listener加入其中??纯慈康膬?nèi)容。
1 <? xml version="1.0" encoding="UTF-8" ?>
2 < web-app xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4 version ="2.5">
5
6 < display-name > guice-dwr </ display-name >
7 < description > xylz study project - guice </ description >
8
9 < listener >
10 < listener-class > cn.imxylz.study.guice.web.dwr.MyDwrGuiceServletContextListener
11 </ listener-class >
12 </ listener >
13 < servlet >
14 < servlet-name > dwr-invoker </ servlet-name >
15 < servlet-class > org.directwebremoting.guice.DwrGuiceServlet </ servlet-class >
16 < init-param >
17 < param-name > debug </ param-name >
18 < param-value > true </ param-value >
19 </ init-param >
20 </ servlet >
21 < servlet-mapping >
22 < servlet-name > dwr-invoker </ servlet-name >
23 < url-pattern > /dwr/* </ url-pattern >
24 </ servlet-mapping >
25
26 </ web-app >
27
非常簡單,也非常簡潔,其中DwrGuiceServlet的debug參數(shù)只是為了調(diào)試方便才開放的,實(shí)際中就不用寫了。
好了,看看我們的效果。
1 < html >
2 < head >< title > dwr - test (www.imxylz.cn) </ title >
3 < script type ='text/javascript' src ='/guice-dwr/dwr/interface/helloworld.js'></ script >
4 < script type ='text/javascript' src ='/guice-dwr/dwr/engine.js'></ script >
5 < script type ='text/javascript' src ='/guice-dwr/dwr/util.js'></ script >
6 < script type ='text/javascript'>
7 var showHello = function (data){
8 dwr.util.setValue('result',dwr.util.toDescriptiveString(data,1));
9 }
10 var getSystemDate = function (data){
11 dwr.util.setValue('systime',dwr.util.toDescriptiveString(data,2));
12 }
13 </ script >
14 < style type ='text/css'>
15 input.button { border : 1px outset ; margin : 0px ; padding : 0px ; }
16 span { background : #ffffdd ; white-space : pre ; padding-left : 20px ;}
17 </ style >
18 </ head >
19 < body onload ='dwr.util.useLoadingMessage()'>
20 < p >
21 < h2 > Guice and DWR </ h2 >
22 < input class ='button' type ='button' value ="Call HelloWorld 'sayHello' service" onclick ="helloworld.sayHello(showHello)" />
23 < span id ='result' ></ span >
24 </ p >
25 < p >
26 < input class ='button' type ='button' value ="Call HelloWorld 'getSystemDate' service" onclick ="helloworld.getSystemDate(getSystemDate)" />
27 < span id ='systime' ></ span >
28 </ P >
29 </ body >
30 </ html >
我們通過兩個按鈕來獲取我們的遠(yuǎn)程調(diào)用的結(jié)果。
摘要: Guice真的無法享受企業(yè)級組件嗎,JavaEye里炮轟Guice的占絕大多數(shù)。但如果Guice能整合Spring,那么我們似乎可以做很多有意義的事了。那么開始Spring整合之旅吧。不過crazybob在整合方面極不配合,就給了我們一個單元測試類,然后讓我們自力更生。好在Guice本身足夠簡單。
首先還是來一個最簡單無聊的HelloWorld整合吧。
Hell...
閱讀全文
The Example Document and Beans
In this example, we will unmarshall the same XML document that we used in the previous article:
<?xml version="1.0"?>
<catalog library="somewhere">
<book>
<author>Author 1</author>
<title>Title 1</title>
</book>
<book>
<author>Author 2</author>
<title>His One Book</title>
</book>
<magazine>
<name>Mag Title 1</name>
<article page="5">
<headline>Some Headline</headline>
</article>
<article page="9">
<headline>Another Headline</headline>
</article>
</magazine>
<book>
<author>Author 2</author>
<title>His Other Book</title>
</book>
<magazine>
<name>Mag Title 2</name>
<article page="17">
<headline>Second Headline</headline>
</article>
</magazine>
</catalog>
The bean classes are also the same, except for one important change: In the previous article, I had declared these classes to have package
scope -- primarily so that I could define all of them in the same source file! Using the Digester framework, this is no longer possible; the classes need to be declared as public
(as is required for classes conforming to the JavaBeans specification):
import java.util.Vector;
public class Catalog {
private Vector books;
private Vector magazines;
public Catalog() {
books = new Vector();
magazines = new Vector();
}
public void addBook( Book rhs ) {
books.addElement( rhs );
}
public void addMagazine( Magazine rhs ) {
magazines.addElement( rhs );
}
public String toString() {
String newline = System.getProperty( "line.separator" );
StringBuffer buf = new StringBuffer();
buf.append( "--- Books ---" ).append( newline );
for( int i=0; i<books.size(); i++ ){
buf.append( books.elementAt(i) ).append( newline );
}
buf.append( "--- Magazines ---" ).append( newline );
for( int i=0; i<magazines.size(); i++ ){
buf.append( magazines.elementAt(i) ).append( newline );
}
return buf.toString();
}
}
public class Book {
private String author;
private String title;
public Book() {}
public void setAuthor( String rhs ) { author = rhs; }
public void setTitle( String rhs ) { title = rhs; }
public String toString() {
return "Book: Author='" + author + "' Title='" + title + "'";
}
}
import java.util.Vector;
public class Magazine {
private String name;
private Vector articles;
public Magazine() {
articles = new Vector();
}
public void setName( String rhs ) { name = rhs; }
public void addArticle( Article a ) {
articles.addElement( a );
}
public String toString() {
StringBuffer buf = new StringBuffer( "Magazine: Name='" + name + "' ");
for( int i=0; i<articles.size(); i++ ){
buf.append( articles.elementAt(i).toString() );
}
return buf.toString();
}
}
public class Article {
private String headline;
private String page;
public Article() {}
public void setHeadline( String rhs ) { headline = rhs; }
public void setPage( String rhs ) { page = rhs; }
public String toString() {
return "Article: Headline='" + headline + "' on page='" + page + "' ";
}
}
import org.apache.commons.digester.*;
import java.io.*;
import java.util.*;
public class DigesterDriver {
public static void main( String[] args ) {
try {
Digester digester = new Digester();
digester.setValidating( false );
digester.addObjectCreate( "catalog", Catalog.class );
digester.addObjectCreate( "catalog/book", Book.class );
digester.addBeanPropertySetter( "catalog/book/author", "author" );
digester.addBeanPropertySetter( "catalog/book/title", "title" );
digester.addSetNext( "catalog/book", "addBook" );
digester.addObjectCreate( "catalog/magazine", Magazine.class );
digester.addBeanPropertySetter( "catalog/magazine/name", "name" );
digester.addObjectCreate( "catalog/magazine/article", Article.class );
digester.addSetProperties( "catalog/magazine/article", "page", "page" );
digester.addBeanPropertySetter( "catalog/magazine/article/headline" );
digester.addSetNext( "catalog/magazine/article", "addArticle" );
digester.addSetNext( "catalog/magazine", "addMagazine" );
File input = new File( args[0] );
Catalog c = (Catalog)digester.parse( input );
System.out.println( c.toString() );
} catch( Exception exc ) {
exc.printStackTrace();
}
}
}
After instantiating the Digester
, we specify that it should not validate the XML document against a DTD -- because we did not define one for our simple Catalog document. Then we specify the patterns and the associated rules: the ObjectCreateRule
creates an instance of the specified class and pushes it onto the parse stack. The SetPropertiesRule
sets a bean property to the value of an XML attribute of the current element -- the first argument to the rule is the name of the attribute, the second, the name of the property.
Whereas SetPropertiesRule
takes the value from an attribute, BeanPropertySetterRule
takes the value from the raw character data nested inside of the current element. It is not necessary to specify the name of the property to set when using BeanPropertySetterRule
: it defaults to the name of the current XML element. In the example above, this default is being used in the rule definition matching the catalog/magazine/article/headline
pattern. Finally, the SetNextRule
pops the object on top of the parse stack and passes it to the named method on the object below it -- it is commonly used to insert a finished bean into its parent.
Note that it is possible to register several rules for the same pattern. If this occurs, the rules are executed in the order in which they are added to the Digester -- for instance, to deal with the <article>
element, found at catalog/magazine/article
, we first create the appropriate article
bean, then set the page
property, and finally pop the completed article
bean and insert it into its magazine
parent.
Invoking Arbitrary Functions
It is not only possible to set bean properties, but to invoke arbitrary methods on objects in the stack. This is accomplished using the CallMethodRule
to specify the method name and, optionally, the number and type of arguments passed to it. Subsequent specifications of the CallParamRule
define the parameter values to be passed to the invoked functions. The values can be taken either from named attributes of the current XML element, or from the raw character data contained by the current element. For instance, rather than using the BeanPropertySetterRule
in the DigesterDriver
implementation above, we could have achieved the same effect by calling the property setter explicitly, and passing the data as parameter:
digester.addCallMethod( "catalog/book/author", "setAuthor", 1 );
digester.addCallParam( "catalog/book/author", 0 );
The first line gives the name of the method to call (setAuthor()
), and the expected number of parameters (1
). The second line says to take the value of the function parameter from the character data contained in the <author>
element and pass it as first element in the array of arguments (i.e., the array element with index 0
). Had we also specified an attribute name (e.g., digester.addCallParam( "catalog/book/author", 0, "author" );
), the value would have been taken from the respective attribute of the current element instead.
One important caveat: confusingly, digester.addCallMethod( "pattern", "methodName", 0 );
does not specify a call to a method taking no arguments -- instead, it specifies a call to a method taking one argument, the value of which is taken from the character data of the current XML element! We therefore have yet another way to implement a replacement for BeanPropertySetterRule
:
digester.addCallMethod( "catalog/book/author", "setAuthor", 0 );
To call a method that truly takes no parameters, use digester.addCallMethod( "pattern", "methodName" );
.
Summary of Standard Rules
Below are brief descriptions of all of the standard rules.
Creational
ObjectCreateRule
: Creates an object of the specified class using its default constructor and pushes it onto the stack; it is popped when the element completes. The class to instantiate can be given through a class
object or the fully-qualified class name.
FactoryCreateRule
: Creates an object using a specified factory class and pushes it onto the stack. This can be useful for classes that do not provide a default constructor. The factory class must implement the org.apache.commons.digester.ObjectCreationFactory
interface.
Property Setters
SetPropertiesRule
: Sets one or several named properties in the top-level bean using the values of named XML element attributes. Attribute names and property names are passed to this rule in String[]
arrays. (Typically used to handle XML constructs like <article page="10">
.)
BeanPropertySetterRule
: Sets a named property on the top-level bean to the character data enclosed by the current XML element. (Example: <page>10</page>
.)
SetPropertyRule
: Sets a property on the top-level bean. Both the property name, as well as the value to which this property will be set, are given as attributes to the current XML element. (Example: <article key="page" value="10" />
.)
Parent/Child Management
SetNextRule
: Pops the object on top of the stack and passes it to a named method on the object immediately below. Typically used to insert a completed bean into its parent.
SetTopRule
: Passes the second-to-top object on the stack to the top-level object. This is useful if the child object exposes a setParent
method, rather than the other way around.
SetRootRule
: Calls a method on the object at the bottom of the stack, passing the object on top of the stack as argument.
Arbitrary Method Calls
CallMethodRule
: Calls an arbitrary named method on the top-level bean. The method may take an arbitrary set of parameters. The values of the parameters are given by subsequent applications of the CallParamRule
.
CallParamRule
: Represents the value of a method parameter. The value of the parameter is either taken from a named XML element attribute, or from the raw character data enclosed by the current element. This rule requires that its position on the parameter list is specified by an integer index.
Specifying Rules in XML: Using the xmlrules Package
So far, we have specified the patterns and rules programmatically at compile time. While conceptually simple and straightforward, this feels a bit odd: the entire framework is about recognizing and handling structure and data at run time, but here we go fixing the behavior at compile time! Large numbers of fixed strings in source code typically indicate that something is being configured (rather than programmed), which could be (and probably should be) done at run time instead.
The org.apache.commons.digester.xmlrules
package addresses this issue. It provides the DigesterLoader
class, which reads the pattern/rule-pairs from an XML document and returns a digester already configured accordingly. The XML document configuring the Digester
must comply with the digester-rules.dtd , which is part of the xmlrules
package.
Below is the contents of the configuration file (named rules.xml ) for the example application. I want to point out several things here.
Patterns can be specified in two different ways: either as attributes to each XML element representing a rule, or using the <pattern>
element. The pattern defined by the latter is valid for all contained rule elements. Both ways can be mixed, and <pattern>
elements can be nested -- in either case, the pattern defined by the child element is appended to the pattern defined in the enclosing <pattern>
element.
The <alias>
element is used with the <set-properties-rule>
to map an XML attribute to a bean property.
Finally, using the current release of the Digester package, it is not possible to specify the BeanPropertySetterRule
in the configuration file. Instead, we are using the CallMethodRule
to achieve the same effect, as explained above.
<?xml version="1.0"?>
<digester-rules>
<object-create-rule pattern="catalog" classname="Catalog" />
<set-properties-rule pattern="catalog" >
<alias attr-name="library" prop-name="library" />
</set-properties-rule>
<pattern value="catalog/book">
<object-create-rule classname="Book" />
<call-method-rule pattern="author" methodname="setAuthor"
paramcount="0" />
<call-method-rule pattern="title" methodname="setTitle"
paramcount="0" />
<set-next-rule methodname="addBook" />
</pattern>
<pattern value="catalog/magazine">
<object-create-rule classname="Magazine" />
<call-method-rule pattern="name" methodname="setName" paramcount="0" />
<pattern value="article">
<object-create-rule classname="Article" />
<set-properties-rule>
<alias attr-name="page" prop-name="page" />
</set-properties-rule>
<call-method-rule pattern="headline" methodname="setHeadline"
paramcount="0" />
<set-next-rule methodname="addArticle" />
</pattern>
<set-next-rule methodname="addMagazine" />
</pattern>
</digester-rules>
Since all the actual work has now been delegated to the Digester
and DigesterLoader
classes, the driver class itself becomes trivially simple. To run it, specify the catalog document as the first command line argument, and the rules.xml
file as the second. (Confusingly, the DigesterLoader
will not read the rules.xml
file from a File
or an org.xml.sax.InputSource
, but requires a URL -- the File
reference in the code below is therefore transformed into an equivalent URL.)
import org.apache.commons.digester.*;
import org.apache.commons.digester.xmlrules.*;
import java.io.*;
import java.util.*;
public class XmlRulesDriver {
public static void main( String[] args ) {
try {
File input = new File( args[0] );
File rules = new File( args[1] );
Digester digester = DigesterLoader.createDigester( rules.toURL() );
Catalog catalog = (Catalog)digester.parse( input );
System.out.println( catalog.toString() );
} catch( Exception exc ) {
exc.printStackTrace();
}
}
}
Conclusion
This concludes our brief overview of the Jakarta Commons Digester package. Of course, there is more. One topic ignored in this introduction are XML namespaces: Digester allows you to specify rules that only act on elements defined within a certain namespace.
We mentioned briefly the possibility of developing custom rules, by extending the Rule
class. The Digester
class exposes the customary push()
, peek()
, and pop()
methods, giving the individual developer freedom to manipulate the parse stack directly.
Lastly, note that there is an additional package providing a Digester implementation which deals with RSS (Rich-Site-Summary)-formatted newsfeeds. The Javadoc tells the full story.
References
Philipp K. Janert is a software project consultant, server programmer, and architect.
Beanutils用了魔術(shù)般的反射技術(shù),實(shí)現(xiàn)了很多夸張有用的功能,都是C/C++時代不敢想的。無論誰的項目,始終一天都會用得上它。我算是后知后覺了,第一回看到它的時候居然錯過。
1.屬性的動態(tài)getter,setter
在這框架滿天飛的年代,不能事事都保證執(zhí)行g(shù)etter,setter函數(shù)了,有時候?qū)傩允且枰鶕?jù)名字動態(tài)取得的,就像這樣:
BeanUtils.getProperty(myBean,"code");
而BeanUtils更強(qiáng)的功能是直接訪問內(nèi)嵌對象的屬性,只要使用點(diǎn)號分隔。
BeanUtils.getProperty(orderBean, "address.city");
相比之下其他類庫的BeanUtils通常都很簡單,不能訪問內(nèi)嵌的對象,所以經(jīng)常要用Commons BeanUtils替換它們。
BeanUtils還支持List和Map類型的屬性。如下面的語法即可取得顧客列表中第一個顧客的名字
BeanUtils.getProperty(orderBean, "customers[1].name");
其中BeanUtils會使用ConvertUtils類把字符串轉(zhuǎn)為Bean屬性的真正類型,方便從HttpServletRequest等對象中提取bean,或者把bean輸出到頁面。
而PropertyUtils就會原色的保留Bean原來的類型。
2.beanCompartor 動態(tài)排序
還是通過反射,動態(tài)設(shè)定Bean按照哪個屬性來排序,而不再需要在bean的Compare接口進(jìn)行復(fù)雜的條件判斷。
List peoples = ...; // Person對象的列表Collections.sort(peoples, new BeanComparator("age"));
如果要支持多個屬性的復(fù)合排序,如"Order By lastName,firstName"
ArrayList sortFields = new ArrayList();sortFields.add(new BeanComparator("lastName"));
sortFields.add(new BeanComparator("firstName"));
ComparatorChain multiSort = new ComparatorChain(sortFields);
Collections.sort(rows,multiSort);
其中ComparatorChain屬于jakata commons-collections包。
如果age屬性不是普通類型,構(gòu)造函數(shù)需要再傳入一個comparator對象為age變量排序。
另外, BeanCompartor本身的ComparebleComparator, 遇到屬性為null就會拋出異常, 也不能設(shè)定升序還是降序。
這個時候又要借助commons-collections包的ComparatorUtils.
Comparator mycmp = ComparableComparator.getInstance();
mycmp = ComparatorUtils.nullLowComparator(mycmp); //允許null
mycmp = ComparatorUtils.reversedComparator(mycmp); //逆序
Comparator cmp = new BeanComparator(sortColumn, mycmp);
3.Converter 把Request或ResultSet中的字符串綁定到對象的屬性
經(jīng)常要從request,resultSet等對象取出值來賦入bean中,下面的代碼誰都寫膩了,如果不用MVC框架的綁定功能的話。
String a = request.getParameter("a"); bean.setA(a); String b = ....
不妨寫一個Binder:
MyBean bean = ...; HashMap map = new HashMap(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); map.put(name, request.getParameterValues(name)); } BeanUtils.populate(bean, map);
其中BeanUtils的populate方法或者getProperty,setProperty方法其實(shí)都會調(diào)用convert進(jìn)行轉(zhuǎn)換。
但Converter只支持一些基本的類型,甚至連java.util.Date類型也不支持。而且它比較笨的一個地方是當(dāng)遇到不認(rèn)識的類型時,居然會拋出異常來。
對于Date類型,我參考它的sqldate類型實(shí)現(xiàn)了一個Converter,而且添加了一個設(shè)置日期格式的函數(shù)。
要把這個Converter注冊,需要如下語句:
ConvertUtilsBean convertUtils = new ConvertUtilsBean();
DateConverter dateConverter = new DateConverter();
convertUtils.register(dateConverter,Date.class);
//因為要注冊converter,所以不能再使用BeanUtils的靜態(tài)方法了,必須創(chuàng)建BeanUtilsBean實(shí)例
BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean());
beanUtils.setProperty(bean, name, value);
4 其他功能
4.1 PropertyUtils,當(dāng)屬性為Collection,Map時的動態(tài)讀?。?/span>
Collection: 提供index
BeanUtils.getIndexedProperty(orderBean,"items",1);
或者
BeanUtils.getIndexedProperty(orderBean,"items[1]");
Map: 提供Key Value
BeanUtils.getMappedProperty(orderBean, "items","111");//key-value goods_no=111
或者
BeanUtils.getMappedProperty(orderBean, "items(111)")
4.2 PropertyUtils,獲取屬性的Class類型
public static Class getPropertyType(Object bean, String name)
4.3 ConstructorUtils,動態(tài)創(chuàng)建對象
public static Object invokeConstructor(Class klass, Object arg)
4.4 MethodUtils,動態(tài)調(diào)用方法
MethodUtils.invokeMethod(bean, methodName, parameter);
4.5 動態(tài)Bean 見用DynaBean減除不必要的VO和FormBean
1.BeanUtils基本用法:
java 代碼
package com.beanutil;
import java.util.Map;
public class User {
private Integer id;
private Map map;
private String username;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this .map = map;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this .username = username;
}
}
java 代碼
public class Order {
private User user;
private Integer id;
private String desc;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this .desc = desc;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this .user = user;
}
}
java 代碼
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
public class Test {
private User user = new User();
private Order order1 = new Order();
private Order order2 = new Order();
private Order order3 = new Order();
private Map map = new HashMap();
private User user1 = new User();
public Test(){
init();
}
public static void main(String[] args) throws Exception{
Test test = new Test();
System.out.println(BeanUtils.getProperty(test.user, "username" ));
System.out.println(BeanUtils.getProperty(test.order1, "user.username" ));
System.out.println(BeanUtils.getProperty(test.user1, "map(order2).desc" ));
User tempUser = new User();
BeanUtils.copyProperties(tempUser, test.user1);
System.out.println(tempUser.getUsername());
System.out.println(tempUser.getId());
}
public void init(){
user.setId( 0 );
user.setUsername( "zhangshan" );
order1.setId( 1 );
order1.setDesc( "order1" );
order1.setUser(user);
order2.setId( 2 );
order2.setDesc( "order2" );
order2.setUser(user);
order3.setId( 3 );
order3.setDesc( "order3" );
order3.setUser(user);
map.put( "order1" , order1);
map.put( "order2" , order2);
map.put( "order3" , order3);
user1.setId( 1 );
user1.setUsername( "lisi" );
user1.setMap(map);
}
}
輸出結(jié)果為:
zhangshan
zhangshan
order2
lisi
1
2. BeanCompartor 動態(tài)排序
A:動態(tài)設(shè)定Bean按照哪個屬性來排序,而不再需要再實(shí)現(xiàn)bean的Compare接口進(jìn)行復(fù)雜的條件判斷
java 代碼
List<order></order> list = new ArrayList<order></order>();
list.add(test.order2);
list.add(test.order1);
list.add(test.order3);
for (Order order : list){
System.out.println(order.getId());
}
Collections.sort(list, new BeanComparator( "id" ));
for (Order order : list){
System.out.println(order.getId());
}
B:支持多個屬性的復(fù)合排序
java 代碼
List <beancomparator></beancomparator> sortFields = new ArrayList<beancomparator></beancomparator>();
sortFields.add( new BeanComparator( "id" ));
sortFields.add( new BeanComparator( "desc" ));
ComparatorChain multiSort = new ComparatorChain(sortFields);
Collections.sort(list, multiSort);
for (Order order : list){
System.out.println(order.getId());
}
C:使用ComparatorUtils進(jìn)一步指定排序條件
上面的排序遇到屬性為null就會拋出異常, 也不能設(shè)定升序還是降序。
不過,可以借助commons-collections包的ComparatorUtils
BeanComparator,ComparableComparator和ComparatorChain都是實(shí)現(xiàn)了Comparator這個接口
java 代碼
Comparator mycmp = ComparableComparator.getInstance();
mycmp = ComparatorUtils.nullLowComparator(mycmp);
mycmp = ComparatorUtils.reversedComparator(mycmp);
Comparator cmp = new BeanComparator( "id" , mycmp);
Collections.sort(list, cmp);
for (Order order : list){
System.out.println(order.getId());
}
摘要: CXF
最新版本:2.2.2開源服務(wù)框架,可以通過API,如JAX-WS,構(gòu)建和開發(fā)服務(wù)。服務(wù)可以使多種協(xié)議的,例如SOAP, XML/HTTP, RESTful HTTP, CORBA,并可以工作與多種傳輸協(xié)議之上,如HTTP,JMS,JBI。
主要特性
·支持Webservice標(biāo)準(zhǔn):包括SOAP, the Basic Profile, WSDL, ...
閱讀全文