关于Commons Configuration 2.x版本

Apache Commons Configuration 已经升级到了2.x的版本,然而,国内多数的博客在介绍这个框架的时候,还是基于1.x版本的,现在简单介绍下2.x版本和1.x版本的区别。

1.结构发生了变化;

1.x的时候:

import org.apache.commons.configuration.ConfigurationException;

import org.apache.commons.configuration.PropertiesConfiguration;

import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

2.x:

import org.apache.commons.configuration2.FileBasedConfiguration;

import org.apache.commons.configuration2<
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
1. jpivot加入其它应用中 解压jpivot.war 文件,在你的应用当中你需要下面的这些文件: /wcf/** /jpivot/** /WEB-INF/wcf/** /WEB-INF/jpivot/** /WEB-INF/lib/** 最后你还需要对比 jpivot.war 里的 web.xml 文件去修改你自己的 web.xml 文件。 2. 例子说明 一个Schema的xml文件,定义立方体;(feeSchema.xml) 一个jsp文件(fee.jsp),使用jpivot的标签(jp:mondrianQuery),写入多维查询表达式。 2.1. 数据库连接  直接用jdbc <jp:mondrianQuery dataSource="" id="query01" jdbcDriver="oracle.jdbc.driver.OracleDriver" jdbcUrl="jdbc:oracle:thin:ngykt/[email protected]:1521:orcl10" catalogUri="/WEB-INF/queries/feeSchema.xml">  在应用服务器中定义数据源 <jp:mondrianQuery dataSource="feeDS" id="query01" catalogUri="/WEB-INF/queries/feeSchema.xml"> 例子在tomcat中定义了数据源feeDS 2.2. 各种维度类型  一般维度 例子中定义了维度:[term].[所有终端] 按终端id统计缴费情况。  多level维度 例子中定义了维度:[agentTerm].[所有终端] 按代理商,终端两级统计缴费情况  有父子关系维度 例子中定义了维度:[area].[所有地区] 按组织机构级别统计缴费情况 在定义父子级别时,要指定parentColumn和nullParentValue 且要定义Closure,如果不定义,指标钻取的数据有问题。 Closure表针对维度表计算distance,Closure表的生产参见例子中的存储过程sp_zycreateorgclosure 例如: <Level name="地区" uniqueMembers="true" nameColumn="ORGENTITYNAME" column="ORGENTITYID" parentColumn="ORGENTITYPARENT" nullParentValue="null"> <Closure parentColumn="ORGENTITYPARENT" childColumn="ORGENTITYID"> <Table name="ZY_ORG_CLOSURE"/> </Closure> </Level> 3. Mondrian Schema详解 3.1. Schema Schema 定义了一个多维数据库。包含了一个逻辑模型,而这个逻辑模型的目的是为了书写 MDX 语言的查询语句。这个逻辑模型实际上提供了这几个概念: Cubes (立方体)、维度( Dimensions )、层次( Hierarchies )、级别( Levels )、和成员( Members )。而一个 schema 文件就是编辑这个 schema 的一个 xml 文件。在这个文件中形成逻辑模型和数据库物理模型的对应。 3.2. Cube 一个 Cube 是一系列维度 (Dimension) 和度量 (Measure) 的集合区域。在 Cube 中, Dimension 和 Measure 的共同地方就是共用一个事实表。 Cube 中的有以下几个属性: 属性名 含义 name Cube 的名字 caption 标题 , 在表示层显示的 cache 是否对 Cube 对应的实表用 mondrian 进行存储 , 默认为 true enabled 是布尔型的 , 如果是被激活 ,Cubes 就执行 , 否则就不予理睬,默认为 true Cube 里面有一个全局的标签定义了所用的事实表的表名 3.3. Dimension 他是一个层次( Hierarchies )的集合 , 维度一般有其相对应的维度表 . 他的组成是由层次( Hierarchies )而层次( Hierarchies )又是有级别( Level )组成 . 其属性如下: 属性名 含义 name Dimension 的名称 type 类型,有两个可选的类型: StandarDimension 和 TimeDimension ,默认为 StandardDimension caption 标题 , 在表示层显示的 UsagePrefix 加前缀 , 消除歧义 foreignKey 外键,对应事实表中的一个列,它通过 <Hierarchy> 元素中的主键属性连接起来。 3.4. Hierarchy 你一定要指定其中的各种关系 , 如果没有指定 , 就默认 Hierarchy 里面装的是来自立方体中的真实表 . 属性如下: 属性名 含义 name Hierarchy 的名称,该值可以为空,为空时表示 Hirearchy 的名字和 Dimension 的名字相同。当一个 Dimension 有多个 Hierarchy 时,注意 name 值要唯一。 hasAll 布尔型的 , 决定是否包含全部的成员 member allMemberName 所有成员的名字 , 也就是总的标题 , 例如: allMemberName= “全部产品” allLevelName 所有级别的名字,它会覆盖其下所有的 Member 的 name 和所有的 Level 的 name 属性的值。 allMemberCaption 例如 : allMemberCaption= “全部产品”这个是在表示层显示的内容 PrimaryKey 通过主键来确定成员,该主键指的是成员表中的主键,该主键同时要与 Dimension 里设置的 foreignKey 属性对应的字段形成外键对应关系 primaryKeyTable 如果成员表不只一个,而是多个表通过 join 关系形成的,那么就要通过这个属性来指明 join 的这些表中,哪一个与 Dimension 里设置的 foreignKey 属性形成外键关系。通过该属性来指明主表 caption 标题 , 在表示层显示的 defaultMember memberReaderClass 设定一个成员读取器,默认情况下 Hierarchy 都是从关系型数据库里读取的,如果你的数据不在 RDBMS 里面的话,你可以通过自定义一个 member reader 来表现一个 Hierarchy 。 3.5. Level 级别 , 他是组成 Hierarchy 的部分。属性很多,并且是 schema 编写的关键,使用它可以构成一个结构树, Level 的先后顺序决定了 Level 在这棵树上的的位置,最顶层的 Level 位于树的第一级,依次类推。 Level 的属性如下: 属性名 含义 name 名称 table 该 Level 要使用的表名 column 用上面指定的表中某一列作为该 Level 的关键字 nameColumn 用来显示的时候使用,如果不定义,那么就采用上面的 column 的值来进行显示。 oridinalColumn 定义该 Level 上的成员的显示顺序,如果不指定,那么采用 column 的值。 parentColumn 在一个有父 - 子关系的 Hierarchy 当中,当前 Level 引用的是其父成员的列名。好比是一张部门表,在一张表里表现部门的上下级关系,一个是主键,肯定还有一个字段为连接到该主键的外键的列名,这里的 parentColumn 指的就是这个列名。 nullParentValue 如果当前的 Level 是有上下级关系(设置了 parentColumn 属性),如果该 Level 又处于顶级,我们需要将顶级的数据取出来,这里指的是位于顶级的父成员的值,有些数据库不支持 null, 那么也可以使用 ’0’ 或 ’-1’ 等,这就表示顶级的成员的父 ID 为 ’0’ 或为 ’-1’ 。 type 数据类型,默认值为 string 。当然还可以是 Numeric 、 Integer 、 Boolean 、 Date 等。 uniqueMembers 该属性用于优化产生的 SQL ,如果你知道这个级别和其父级别交叉后的值或者是维度表中给定的级别所有的值是唯一的,那么就可以设置该值为 true ,否则为 false 。 levelType 该 Level 的类型,默认为 regular (正常的),如果你在其 Dimension 属性 type 里选择了 TimeDimension 那么这里就可以选择 TimeYears 、 TimeQuarters 、 TimeMonth 、 TimeWeeds 、 TimeDays 。 hideMemberIf 在什么时候不隐藏该成员,可选的值有三个: Never 、 IfBlankName 、 IfParentName approxRowCount 该属性可以用来提高性能,可以通过指定一个数值以减少判断级别、层次、维度基数的时间,该属性在通过使用 XMLA 连接 Mondrian 很有用处。 caption 标题 , 在表示层显示的 captionColumn 用来显示标题的列 formatter 该属性定义了 Member.getCaption() 方法返回的动作值,这里需要是一个实现了 mondrian.olap.MemberFormatter 接口的类,用来对 Caption 地值进行格式化。 3.6. Join 对于一个 Hierarchy 来说,有两种方式为其指定:一种是直接通过一个 Table 标签指定;一种是通过 Join 将若干张表连接起来指定。一旦采用 Join 的话,那么就要在 Hierarchy 里的 primaryKeyTable 属性指定主表。 3.7. Measure Measure 就是我们要计算的数值,操作的核心。它的属性如下: 属性名 含义 name 名称 aggregator 要采用的计算函数 column 要计算的列名 formatString 计算结果的显示格式。 visible 是否可见 datatype 数据类型,默认为 Numeric formatter 采用类来对该 Measure 的值进行格式,具体参考 Level 的 formatter 属性。 caption 标题,用来显示时使用。 4. JPivot标签库使用详解 4.1. Introduce JPivot 是一套基于 Mondrian 的 OLAP 前端展现工具,它提供了一套标签库来解决的 OLAP 的展现层问题。 在一个 JSP 页面当中,如果要使用 JPivot 标签库,除了要配置相关的配置文件外,我们需要在 JSP 头的位置声明对 JPivot 标签库的引用,引用方法如下: <%@ taglib uri = "http://www.tonbeller.com/jpivot" prefix = "jp" %> JPivot 标签库包含以下几个标签: chart chooseQuery clickable destroyQuery mondrianQuery navitator print scalarQuery setParam table testQuery xmlaQuery 接下来我们将详细介绍这些标签的具体用法及相关参数的含义。 4.2. chart 创建一个图表组件,这个图表组件的标签不能直接产生可见输出,它必须通过 WCF 的渲染标签才行。 chart 标签的主要属性如下: 属性名 必须 数据类型 是否能动态计算指定 描述 id 是 String 是 标签的 ID visible 否 boolean 是 用来设置该组件是否可见,当该属性为 false 时 WCF 标签半角不显示该组件。 role 否 String 是 Role 的名称,如: role=”tomcat” 就允许当前用户角色为 tomcat 的访问该控件, role=”!tomcat” 则就允许除角色为 tomcat 以外的所有的用户访问。 query 是 OlapModel 是 一个 mondrianQuery 标签的 ID 值 baseDisplayURL 否 String 是 显示图表的链接,链接的后面还要添加参数“ ?=filename=[ 临时图表文件的名称 ] ” controllerURL 否 String 是 链接到 JPivot Controller 的 URL ,该属性在一些复杂环境下比如 Portal 之类就很有用。 4.3. chooseQuery 从前面创建的若干个查询当中选择一个合适的查询(通过查询名称进行选择),例如: <jp:mondrianQuery id="query01" queryName="name1"> SELECT ... </jp:mondrianQuery> <jp:mondrianQuery id="query01" queryName="name2"> SELECT ... </jp:mondrianQuery> ... <jp:chooseQuery id="query01" queryName="name1"/> 它的属性比较简单,只有 id 和 queryName 两个,而且比较容易理解。 4.4. clickable 该标签的作用是给一个 dimension 或一个 level 里的所有的 members 加上超链,使得它们变的可以进行点击操作。生成的 URL 中包含这个 member 的唯一名称,这个标签必须要在一个 table 或一个 query 的标签里嵌套使用。 这个动作还依赖于该标签的 sessionParam 属性,如果该属性存在,那么参数值将在页面显示之前写到 com.tonbeller.jpivot.param.SessionParamPool 当中,如果不存在该属性,参数将会被编码到超链接当中。 例如: <jp:mondrianQuery ...> select .. from Sales <jp:clickable urlPattern="/otherpage.jsp?param={0}" uniqueName="[Customers]"/> <jp:clickable page="/yetotherpage.jsp" uniqueName="[Products].[Category]" sessionParam="Category"/> </jp:mondrianQuery> 该标签的主要属性如下: 属性名 必须 数据类型 是否能动态计算指定 描述 urlPattern 否 String 是 该属性已过时,使用 page 属性替代它。 page 否 String 是 目标页面的名称,该属性的值必须要以“ / ”开始,如果不设置,那么会采用当前页面的名称。 uniqueName 否 String 是 一个 dimension 或 hierarchy 或 level 的唯一名称,用来标识哪个 member 将可以点击。 menuLabel 否 String 是 如果多次定义 clickable ,那么它将通过一个右键来进行显示,这里的值就是右键菜单的名称。 sessionParam 否 String 是 参数名称,它将会被回写到 com.tonbeller.jpivot.param.SessionParamPool 当中。 propertyName 否 String 是 该属性如果存在,该 member 属性的值将会被带到 com.tonbeller.jpivot.param.Parameter 的 sqlValue 的属性当中。 propertyPrefix 否 String 是 如果存在该属性,多个 SessionParam 将会被创建,每一个 member 的名字的开始部分将采用该属性的值。 providerClass 否 String 是 如果存在该属性,这个类的实例将会从当前的 member 中获取 SessionParam 对象的实例,这里的类必须实现 com.tonbeller.jpivot.table.navi.ClickableMember.ParameterProvider 接口。 4.5. destroyQuery 该标签的主要作用是销毁所有的查询,它的用法如下: <jp:destroyQuery id="query01"/> 它只有一个 ID 属性。 4.6. mondrianQuery 该标签的主要作用是让 mondrian 执行指定的查询,该标签需要指定一个 JDBC 的 datasource 或单独指定 JDBC 连接的各个属性从而可以让 mondrian 连接到指定的数据库。 它的用法如下: <jp:mondrianQuery id="query01" jdbcDriver="com.mysql.jdbc.Driver" jdbcUrl="jdbc:mysql://localhost/foodmart" catalogUri="/WEB-INF/test/FoodMart.xml"> select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns, {[Product].[All Products]} ON rows from Sales where ([Time].[1997]) </jp:mondrianQuery> 它的主要属性如下: 属性名 必须 数据类型 是否能动态计算指定 描述 id 是 String 是 标签的 ID jdbcDriver 否 String 是 要连接到目标数据库的驱动类名 jdbcUrl 否 String 是 目标数据库的 URL jdbcUser 否 String 是 登录数据库的用户名 jdbcPassword 否 String 是 登录数据库的密码 dataSource 否 String 是 可以连接到数据库的 JNDI 名称,如 jdbc/SampleDB ,当使用它时另外四个单独连接数据库的属性不可以再用。 catalogUri 是 String 是 Mondrian Schema 文件所在的路径,指定路径时需要从 web 应用的根开始,如 /WEB-INF/FoodMart.xml 。 role 否 String 是 指定角色,该角色的值来自 Mondrian Schema 里定义的角色。 dynResolver 否 String 是 指定一个用来解析 Mondrian Schema 里定义的动态变量的解析类。 connectionPooling 否 String 是 当该值为 false 时,将从连接池里阻止 Mondrian dynLocale 否 String 是 设置动态解析 Mondrian Schema 里的 Local dataSourceChangeListener 否 String 是 指定一个类用来检测 datasource 的变化。 queryName 否 String 是 这个属性允许保留多个查询,对于每一个查询,最后一个查询将会被存储下来,可以使用 chooseQuery 标签在查询间进行切换。 stackMode 否 boolean 是 如果设置为 false ,那么所有的查询将同时被处理;如果设置为 true ,那么将保持其原有的顺序,例如: 查询名称为 qn1 的创建后,标签就马上将其显示出来 ; 接下来创建 qn2 ,现在 stack 里有 qn1 和 qn2 , qn2 也将被显示出来。 4.7. navigator 创建一个导航组件,这个组件不会直接输出,它必须通过 WCF 的标签来 render 输出。 示例用法: <jp:navigator id="navi01" query="#{query01}" visible="false"/> <wcf:render ref="navi01" xslUri="/WEB-INF/jpivot/navi/navigator.xsl" xslCache="true"/> 该标签的主要属性如下: 属性名 必须 数据类型 是否能动态计算指定 描述 id 是 String 是 标签的 ID query 是 OlapModel 是 指定一个 mondrianQuery 标签的名称 visible 否 boolean 是 设置该组件是否可见,如果设置为 false 那么 WCF 的 render 标签将不会 render 该组件。 role 否 String 是 角色的名称,可以加前缀“!”,表示“除 … 角色之外”都可以访问该组件,否则就是只有该角色才能访问该组件。 4.8. print 创建一个打印控件,该控件不能直接输出,你必须创建 WCF form 来调用打印的 servlet 来生成 XLS/PDF 文件。 该标签只有一个 ID 属性。 4.9. scalarQuery 创建一个包含由单个单元格的组成的 OLAP 结果的会话属性,它的值由标签的属性提供。 示例用法: <jp:scalarQuery id="query01" value="#{some.bean.property}"> formattedValue="#{some.bean.otherProperty}" caption="Some Caption" /> 它的主要属性如下: 属性名 必须 数据类型 是否能动态计算指定 描述 id 是 String 是 标签的 ID value 是 String 是 是一个 EL 的表达式,通过 EL 表达式来计算值,这里是一个数字。 formattedValue 否 String 是 用一个 EL 表达式来计算格式化后的值,是一个字符串 caption 否 String 是 用一个 EL 表达式来生成 caption 的值,这里是一个字符串 queryName 否 String 是 请参考 mondrianQuery 标签 stackMode 否 boolean 是 请参考 mondrianQuery 标签 4.10. selectProperties 创建一个选择属性的组件,该组件不会直接输出,必须要与 WCF 的 render 标签结合。 示例用法: <jp:selectproperties id="selectprop01" table="#{table01}" visible="false"/> <wcf:render ref="selectprop01" xslUri="/WEB-INF/jpivot/navi/navigator.xsl" xslCache="true"/> 它的主要属性如下: 属性名 必须 数据类型 是否能动态计算指定 描述 id 是 String 是 标签的 ID table 是 TableComponent 是 该属性用来指定一个表格组件。 visible 否 boolean 是 设置该组件是否可见,如果设置为 false 那么 WCF 组件将不会显示该组件。 role 否 String 是 指定角色,同样可以使用“!” 4.11. setParam 通过 get 或 post 或 session 里的值为 MDX 查询设置查询参数。示例用法: <jp:mondrianQuery id="query01"...> SELECT ... Parameter("Param01", ... WHERE ... </jp:mondrianQuery/> <jp:setParam query="query01" httpParam="param" mdxParam="Param01"/> or <jp:setParam query="query01" sessionParam="CUSTOMER" mdxParam="Param01"/> 它的主要属性如下: 属性名 必须 数据类型 是否能动态计算指定 描述 query 是 OlapModel 是 指定一个 mondrianQuery 标签的名称 httpParam 否 String 是 http 参数的名称,如果存在该属性,那么它的值将会被告解析并设置到 mdx 查询的 parameter 当中 sessionParam 否 String 是 Session 的参数名称 mdxParam 是 String 是 MDX 的 parameter 的名称 4.12. table 创建一个 pivot 的表格组件,该组件要借助于 WCF 的 render 标签输出。 示例用法: <jp:table id="table01" query="#{query01}" visible="true"/> 该标签的主要属性如下: 属性名 必须 数据类型 是否能动态计算指定 描述 id 是 String 是 标签的 ID visible 否 boolean 是 设置组件是否可见,如果为 false 那么 WCF 的标签将不会将其 render 。 role 否 String 是 可访问该组件的角度,支持“!”用法 query 是 OlapModel 是 指定一个 mondrianQuery 标签的名称 5. 多维查询表达式 5.1. 什么是 MDX MDX 的全称是 Mutil Dimensional Expressions ,是由 Microsoft , Hyperion 等公司研究多维查询表达式,是所有 OLAP 高级分析所采用的核心查询语言。 MDX 可以用来进行以下操作: 1. calculated members (计算成员) 2. Calculated Cells (计算单元) 3. Security Settion (安全设置) 4. Custom member formula ( 自定义函数 ) 5. Custom level rollup () 6. Actions (动作) 7. Named “ sets ” ( 命名集合 ) 8. Server side formatting (服务器数据格式化) 5.2. MDX 的基本结构 MDX 的基本结构有三种: Members 、 Tuple 、 Set 。 5.2.1. Members 指的是维度树上的一个节点,这里有一点需要指出,量度也是一个特殊的维度,所以对于普通维度上的 Member 可以有几下几种表示方法: [Customer] 或 [Time].[1996] 等,对于特殊的维度——量度而言,也可以表示一个 Member ,如: [Measures].[ unit sales] 等。 Member 的表示方法就是用中括号的形式,“ [……] ”。 5.2.2. Tuple Tuple :是由若干个 Members 组成,每一个维度上最多只能有一个 Member ,对于一个 Tuple 而言至少有一个维度,多则不限,同时对于没有列出来的那就表示为默认的 Member 。 Tuple 的表示方法是小括号“ () ”,因为其又有 Member 构成,所以通常的格式为“ ([…],[…][…],…) ”;示例: a) ([Regin ].[USA]) b) ([product].[ computers],[time].[2008]) 。 5.2.3. Sets 同一维度上若干个 Members 的集合,或者是若干个 Tuples 的集合,但这里有一个地方需要注意,那就是如果是若干个 Tuple 组成的集合是,各个 Tuple 里的 Member 之间存在着一定的对应关系。集合的表示方法用大括号“ {} ”,所以可能的表示方法为: a) {[time].[2008],[time].[2009],[time].[2000]} ,这里 Set 是由同一维度的若干个 Member 组成。 b) {([computer],[usa ]),([ mobile],[china])} ,这个 Set 是由两个 Tuples 组成,这里大家可以看到,在第一个 Tuple 当中,第一个 Member 是名为 computer 的产品,所以后面的 Tuple 的第一个 Member 也必须是一个产品,所以我们这里看到的是 mobile ,第一个 Tuple 里第二个 Member 是一个国家,所以第二个 Tuple 的第二个 Member 也必须是一个国家名,依次类推。 5.3. MDX 查询语句 一个标准的 MDX 查询语句就是由我们前面介绍的 MDX 的三个基本对象构成,也就是 Member 、 Tuple 、 Set 。 一个标准的 MDX 查询的语法如下: SELECT Set ON COLUMNS, Set ON ROWS FROM Cube WHERE Tuple 示例: SELECT {[time].[ 1997],[time].[1998]} ON COLUMNS, {([product].[drink],[customer].[gender].[F]),( [product].[food],[customer].[gender].[M])} ON ROWS FROM [Sales] WHERE ([Measures].[ StoreSales ]) 这个 MDX 查询就表示:查询时间为 1997 和 1998 两年的 drink 类产品,女消费者和 food 类产品男消费者的 Store Sales 。 5.4. MDX 表达式 5.4.1. children 它的作用是列出指定分类下的所有项,如 1. [product type].[food].children ,这就表示列出产品类型为 food 的所有产品。 2. [region ].[country].children ,这就表示列出所有的国家名称。 该函数不能用在 Tuple 中 5.4.2. CurrentMember 当前某个 Member ,举例: 某个产品销售额贡献度计算,对于某一个产品或一种类型的产品而言它的销售额贡献度指的是它的销售额与所有产品销售额的比值,此时,如果我们要用 MDX 来计算这个贡献度的值是可以采用 CurrentMember 来实现,如: 此时我们的 MDX 表达可以是: ([product].currentMember ,[ Measures].[stores sales]) / ([product].[ all products],[Measures].[stores sales]) 同时因为默认情况下指的就是 currentMember ,所以上面的 currentMember 可以去掉: ([Measures].[ stores sales]) / ([product].[ all products],[Measures].[stores sales]) 所有 MDX 表达式的写法是由若干个 Tuple 组成 ,比如上面的这个表达式就是由两个 Tuple 的相除构成。 5.4.3. prevMember 和 nextMember 这两个元素分别指的是当前 Member 的上一个 Member 和当前 Member 的下一个 Member 。 利用这两个元素我们可以来实现实际当中常见的同期比(前期比),所谓的同期比指的 是: (当前 member- 当前 member 的前一个 member ) / 当前 member 的前一个 member 对应到 MDX 表达式我们可以用 prevMember 和 nextMember 来实现,如: (([time].currentMember ,[ Measures].[store sales])-([time].prevMember ,[Measures].[storessales ])) / ([time].prevMember ,[ Measures].[store sales]) 当然因为 currentMember 可以省略,所以上面的写法也可以是: (([ Measures ].[store sales])-([time].prevMember ,[Measures].[storessales ])) / ([time].prevMember ,[ Measures].[store sales]) 5.4.4. Parents 、 FirstChild 、 Descendants 下面的这张图就显示一个树 这张图中 all 这个节点是 97 和 98 的 parents , 97 和 98 也是 all 的 children ,同时 97 是 all 的 firstChild 如果我们要取到 97 下的所有的月份,那么我们就需要用 Descendants 来实现,它的语法是: Descendants( [time].[97],month) ,这里的 month 是一个 level 的名称。所有它的格式为: Descendants([ 一个 Member], 一个 level 的名称 ) 比如在食品销售当中,我们要知道哪一类的食品更受消费者欢迎,这样我们需要只知道各类食品的销量,比如食物( food )、饮料 (drink) 等的具体销量 (unit sales) ,然后再计算出所有食品总的销量( total sales ) , 如果采用 MDX 我们可以很容易的实现,采用 MDX 后的计算哪种食品更受欢迎的表达式如下: ([food].currentMember ,[ Measures].[unit sales]) / ([food].currentMember.parent ,[ Measures].[total sales]) 因为 currentMember 可以省略,所以上面的写法又可以是: ( [ Measures].[unit sales]) / ([food].currentMember.parent ,[ Measures].[total sales]) 5.5. 参见MDX的基本语法及概念.pdf 6. 常见问题 6.1. MDX编辑器中文乱码  web.xml中添加过滤器 <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>com.tonbeller.wcf.charset.CharsetFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 注意,该filter-mapping放在所有filter-mapping的最前面  jsp页面编码用UTF-8(testpage.jsp) 6.2. pdf中文显示“#” 1).在WEB-INF\jpivot\print目录下建立userconfig.xml文件。内容如下:(仅配置黑体,fontBaseDir属性我也没有配置,源码中有) <configuration> <fonts> <font metrics-file ="simhei.xml" kerning ="yes" embed-file ="simhei.ttf"> <font-triplet name ="SimHei" style ="normal" weight ="normal"/> <font-triplet name ="SimHei" style ="normal" weight ="bold"/> <font-triplet name ="SimHei" style ="italic" weight ="normal"/> <font-triplet name ="SimHei" style ="italic" weight ="bold"/> </font> </fonts> </configuration> 2). 下载Apache的FOP包,应用所需JAR包,生成simhei.xml。Java命令行为: java -cp D:\op\fop.jar;D:\op\avalon-framework-4 .2.0.jar;D:\op\commons-logging-1.0.4.jar;D:\op\commons-io-1.1.jar;D:\op\xmlgraph ics-commons-1.4.jar org.apache.fop.fonts.apps.TTFReader C:\WINDOWS\Fonts\simhei. ttf simhei.xml 3). 将simhei.xml,以及simhei.ttf文件放到WEB-INF\jpivot\print目录下。 4 ). 修改WEB-INF\jpivot\table目录下的fo_mdxtable.xsl文件,把所有的font-family对应成SimHei,(以SimHei开头)。 6.3. pdf格式修改(原格式行太宽) 修改jpivot\WebRoot\WEB-INF\jpivot\table\fo_mdxtable.xsl 中font-size,line-height,padding 6.4. 钻取数据的格式化 在利用jpivot钻取的时候,发现出来的数字数据都精确到了小数点后两位,我可不想所有数字都这么精确,连带出来的id值也被这样格式化了,悲惨! 我的解决方法:取消格式定义 找到wcf.jar,修改com.tonbeller.wfc.format.config.xml 删除double和nandouble的定义 6.5. mondrian 的志的配置 将log4j.xml放置到web-inf/classes下 文件内容如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <!-- ================================================== =================== --> <!-- --> <!-- Log4j Configuration --> <!-- --> <!-- Default log configuration file for the Workbench. --> <!-- Copied to the user home directory, where the user can change --> <!-- settings for their own purposes. --> <!-- --> <!-- Sends log messages to the console and the workbench.log file --> <!-- in the user's home directory. --> <!-- --> <!-- ================================================== =================== --> <!-- $Id: //open/mondrian/log4j.xml#2 $ --> <!-- | For more configuration infromation and examples see the Jakarta Log4j | owebsite: http://jakarta.apache.org/log4j --> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false"> <!-- ================================= --> <!-- Preserve messages in a local file --> <!-- ================================= --> <!-- A size based file rolling appender, capturing everything --> <appender name="FILE" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="e:/workbench.log"/> <param name="Append" value="false"/> <param name="MaxFileSize" value="500KB"/> <param name="MaxBackupIndex" value="1"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/> </layout> </appender> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <!-- 设置监视器输出方式 --> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" /> </layout> </appender> <!-- ============================== --> <!-- Append messages to the console --> <!-- ============================== --> <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out"/> <param name="Threshold" value="WARN"/> <layout class="org.apache.log4j.PatternLayout"> <!-- The default pattern: Date Priority [Category] Message\n --> <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/> </layout> </appender> <!-- ================ --> <!-- Limit categories --> <!-- ================ --> <!-- Limit the org.apache category to INFO as its DEBUG is verbose --> <category name="org.apache"> <priority value="INFO"/> </category> <category name="com.tonbeller"> <priority value="WARN"/> </category> <category name="mondrian.rolap"> <priority value="WARN"/> </category> <category name="mondrian.gui"> <priority value="WARN"/> </category> <category name="mondrian.sql"> <priority value="DEBUG"/> </category> <category name="mondrian.mdx"> <priority value="DEBUG"/> </category> <!-- ======================= --> <!-- Setup the Root category --> <!-- ======================= --> <root> <priority value="INFO"/> <appender-ref ref="FILE"/> <appender-ref ref="STDOUT" /> </root> </log4j:configuration> 6.6. schema workbench schema的编辑器,界面化编辑schema。 但不知道为什么,在我的本机上,在打开,新建schema文件,都非常慢。 可以到http://sourceforge.net/projects/mondrian/files/下载,最新版本psw-ce-3.2.1.13885 7. 参考资料 http://mondrian.pentaho.com/documentation/ mondrian的官方网站 http://jacky6024.javaeye.com/ 比较有用的中文资料
Not Using Commons Logging ................................................................... 12 Using SLF4J ............................................................................................ 13 Using Log4J ............................................................................................. 14 II. What’s New in Spring Framework 4.x .................................................................................... 16 3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .................................................................. 17 3.2. Removed Deprecated Packages and Methods .................................................... 17 3.3. Java 8 (as well as 6 and 7) ............................................................................... 17 3.4. Java EE 6 and 7 ............................................................................................... 18 3.5. Groovy Bean Definition DSL .............................................................................. 18 3.6. Core Container Improvements ............................................................................ 19 3.7. General Web Improvements ............................................................................... 19 3.8. WebSocket, SockJS, and STOMP Messaging ..................................................... 19 3.9. Testing Improvements ........................................................................................ 20 III. Core Technologies .............................................................................................................. 21 4. The IoC container ........................................................................................................ 22 4.1. Introduction to the Spring IoC container and beans .............................................. 22 4.2. Container overview ............................................................................................ 22 Configuration metadata ..................................................................................... 23 Instantiating a container .................................................................................... 24 Composing XML-based configuration metadata .......................................... 25 Using the container .......................................................................................... 26 4.3. Bean overview ................................................................................................... 27 Naming beans .................................................................................................. 28 Aliasing a bean outside the bean definition ................................................ 28 Instantiating beans ........................................................................................... 29 Instantiation with a constructor .................................................................. 29 Instantiation with a static factory method .................................................... 30 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation iii Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ................................................................................................... 32 Dependency injection ....................................................................................... 32 Constructor-based dependency injection .................................................... 32 Setter-based dependency injection ............................................................ 34 Dependency resolution process ................................................................. 35 Examples of dependency injection ............................................................. 36 Dependencies and configuration in detail ........................................................... 38 Straight values (primitives, Strings, and so on) ........................................... 38 References to other beans (collaborators) .................................................. 40 Inner beans .............................................................................................. 41 Collections ............................................................................................... 41 Null and empty string values ..................................................................... 44 XML shortcut with the p-namespace .......................................................... 44 XML shortcut with the c-namespace .......................................................... 46 Compound property names ....................................................................... 46 Using depends-on ............................................................................................ 47 Lazy-initialized beans ....................................................................................... 47 Autowiring collaborators .................................................................................... 48 Limitations and disadvantages of autowiring ............................................... 49 Excluding a bean from autowiring .............................................................. 50 Method injection ............................................................................................... 50 Lookup method injection ........................................................................... 51 Arbitrary method replacement ................................................................... 53 4.5. Bean scopes ..................................................................................................... 54 The singleton scope ......................................................................................... 55 The prototype scope ......................................................................................... 55 Singleton beans with prototype-bean dependencies ............................................ 56 Request, session, and global session scopes .................................................... 56 Initial web configuration ............................................................................ 57 Request scope ......................................................................................... 58 Session scope .......................................................................................... 58 Global session scope ............................................................................... 58 Scoped beans as dependencies ................................................................ 58 Custom scopes ................................................................................................ 60 Creating a custom scope .......................................................................... 60 Using a custom scope .............................................................................. 61 4.6. Customizing the nature of a bean ....................................................................... 62 Lifecycle callbacks ............................................................................................ 62 Initialization callbacks ............................................................................... 63 Destruction callbacks ................................................................................ 64 Default initialization and destroy methods .................................................. 64 Combining lifecycle mechanisms ............................................................... 66 Startup and shutdown callbacks ................................................................ 66 Shutting down the Spring IoC container gracefully in non-web applications ................................................................................................................. 68 ApplicationContextAware and BeanNameAware ................................................. 68 Other Aware interfaces ..................................................................................... 69 4.7. Bean definition inheritance ................................................................................. 71 4.8. Container Extension Points ................................................................................ 72 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation iv Customizing beans using a BeanPostProcessor ................................................. 72 Example: Hello World, BeanPostProcessor-style ........................................ 74 Example: The RequiredAnnotationBeanPostProcessor ............................... 75 Customizing configuration metadata with a BeanFactoryPostProcessor ................ 75 Example: the Class name substitution PropertyPlaceholderConfigurer .......... 76 Example: the PropertyOverrideConfigurer .................................................. 77 Customizing instantiation logic with a FactoryBean ............................................. 78 4.9. Annotation-based container configuration ............................................................ 79 @Required ....................................................................................................... 80 @Autowired ..................................................................................................... 80 Fine-tuning annotation-based autowiring with qualifiers ....................................... 83 Using generics as autowiring qualifiers .............................................................. 89 CustomAutowireConfigurer ................................................................................ 90 @Resource ...................................................................................................... 90 @PostConstruct and @PreDestroy .................................................................... 92 4.10. Classpath scanning and managed components ................................................. 92 @Component and further stereotype annotations ............................................... 93 Meta-annotations .............................................................................................. 93 Automatically detecting classes and registering bean definitions .......................... 94 Using filters to customize scanning ................................................................... 95 Defining bean metadata within components ....................................................... 96 Naming autodetected components ..................................................................... 97 Providing a scope for autodetected components ................................................ 98 Providing qualifier metadata with annotations ..................................................... 99 4.11. Using JSR 330 Standard Annotations ............................................................... 99 Dependency Injection with @Inject and @Named ............................................. 100 @Named: a standard equivalent to the @Component annotation ....................... 100 Limitations of the standard approach ............................................................... 101 4.12. Java-based container configuration ................................................................. 102 Basic concepts: @Bean and @Configuration ................................................... 102 Instantiating the Spring container using AnnotationConfigApplicationContext ....... 103 Simple construction ................................................................................ 103 Building the container programmatically using register(Class<?>…) ........... 104 Enabling component scanning with scan(String…) .................................... 104 Support for web applications with AnnotationConfigWebApplicationContext ............................................................................................................... 105 Using the @Bean annotation .......................................................................... 106 Declaring a bean .................................................................................... 107 Receiving lifecycle callbacks ................................................................... 107 Specifying bean scope ............................................................................ 108 Customizing bean naming ....................................................................... 109 Bean aliasing ......................................................................................... 109 Bean description ..................................................................................... 110 Using the @Configuration annotation ............................................................... 110 Injecting inter-bean dependencies ............................................................ 110 Lookup method injection ......................................................................... 111 Further information about how Java-based configuration works internally .... 111 Composing Java-based configurations ............................................................. 112 Using the @Import annotation ................................................................. 112 Conditionally including @Configuration classes or @Beans ....................... 116 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation v Combining Java and XML configuration ................................................... 117 4.13. Bean definition profiles and environment abstraction ........................................ 120 4.14. PropertySource Abstraction ............................................................................ 120 4.15. Registering a LoadTimeWeaver ...................................................................... 120 4.16. Additional Capabilities of the ApplicationContext .............................................. 120 Internationalization using MessageSource ........................................................ 121 Standard and Custom Events .......................................................................... 124 Convenient access to low-level resources ........................................................ 127 Convenient ApplicationContext instantiation for web applications ....................... 128 Deploying a Spring ApplicationContext as a J2EE RAR file ............................... 128 4.17. The BeanFactory ........................................................................................... 129 BeanFactory or ApplicationContext? ................................................................ 129 Glue code and the evil singleton ..................................................................... 131 5. Resources .................................................................................................................. 132 5.1. Introduction ..................................................................................................... 132 5.2. The Resource interface .................................................................................... 132 5.3. Built-in Resource implementations .................................................................... 133 UrlResource ................................................................................................... 133 ClassPathResource ........................................................................................ 133 FileSystemResource ....................................................................................... 134 ServletContextResource .................................................................................. 134 InputStreamResource ..................................................................................... 134 ByteArrayResource ......................................................................................... 134 5.4. The ResourceLoader ....................................................................................... 134 5.5. The ResourceLoaderAware interface ................................................................ 135 5.6. Resources as dependencies ............................................................................. 136 5.7. Application contexts and Resource paths .......................................................... 137 Constructing application contexts ..................................................................... 137 Constructing ClassPathXmlApplicationContext instances - shortcuts .......... 137 Wildcards in application context constructor resource paths ............................... 138 Ant-style Patterns ................................................................................... 138 The Classpath*: portability classpath*: prefix ............................................ 139 Other notes relating to wildcards ............................................................. 139 FileSystemResource caveats .......................................................................... 140 6. Validation, Data Binding, and Type Conversion ............................................................ 141 6.1. Introduction ..................................................................................................... 141 6.2. Validation using Spring’s Validator interface ...................................................... 141 6.3. Resolving codes to error messages .................................................................. 143 6.4. Bean manipulation and the BeanWrapper ......................................................... 144 Setting and getting basic and nested properties ............................................... 144 Built-in PropertyEditor implementations ............................................................ 146 Registering additional custom PropertyEditors .......................................... 149 6.5. Spring Type Conversion ................................................................................... 151 Converter SPI ................................................................................................ 151 ConverterFactory ............................................................................................ 152 GenericConverter ........................................................................................... 153 ConditionalGenericConverter ................................................................... 154 ConversionService API ................................................................................... 154 Configuring a ConversionService ..................................................................... 154 Using a ConversionService programmatically ................................................... 155 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation vi 6.6. Spring Field Formatting .................................................................................... 155 Formatter SPI ................................................................................................. 156 Annotation-driven Formatting ........................................................................... 157 Format Annotation API ............................................................................ 158 FormatterRegistry SPI ..................................................................................... 159 FormatterRegistrar SPI ................................................................................... 159 Configuring Formatting in Spring MVC ............................................................. 159 6.7. Configuring a global date & time format ............................................................ 161 6.8. Spring Validation ............................................................................................. 163 Overview of the JSR-303 Bean Validation API ................................................. 163 Configuring a Bean Validation Provider ............................................................ 164 Injecting a Validator ................................................................................ 164 Configuring Custom Constraints .............................................................. 164 Additional Configuration Options .............................................................. 165 Configuring a DataBinder ................................................................................ 165 Spring MVC 3 Validation ................................................................................. 166 Triggering @Controller Input Validation .................................................... 166 Configuring a Validator for use by Spring MVC ......................................... 166 Configuring a JSR-303/JSR-349 Validator for use by Spring MVC .............. 167 7. Spring Expression Language (SpEL) ........................................................................... 168 7.1. Introduction ..................................................................................................... 168 7.2. Feature Overview ............................................................................................ 168 7.3. Expression Evaluation using Spring’s Expression Interface ................................. 169 The EvaluationContext interface ...................................................................... 171 Type Conversion .................................................................................... 171 7.4. Expression support for defining bean definitions ................................................ 172 XML based configuration ................................................................................ 172 Annotation-based configuration ........................................................................ 173 7.5. Language Reference ........................................................................................ 174 Literal expressions .......................................................................................... 174 Properties, Arrays, Lists, Maps, Indexers ......................................................... 174 Inline lists ....................................................................................................... 175 Array construction ........................................................................................... 175 Methods ......................................................................................................... 176 Operators ....................................................................................................... 176 Relational operators ................................................................................ 176 Logical operators .................................................................................... 177 Mathematical operators ........................................................................... 177 Assignment .................................................................................................... 178 Types ............................................................................................................. 178 Constructors ................................................................................................... 179 Variables ........................................................................................................ 179 The #this and #root variables .................................................................. 179 Functions ....................................................................................................... 180 Bean references ............................................................................................. 180 Ternary Operator (If-Then-Else) ....................................................................... 180 The Elvis Operator ......................................................................................... 181 Safe Navigation operator ................................................................................ 181 Collection Selection ........................................................................................ 182 Collection Projection ....................................................................................... 182 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation vii Expression templating ..................................................................................... 183 7.6. Classes used in the examples .......................................................................... 183 8. Aspect Oriented Programming with Spring ................................................................... 187 8.1. Introduction ..................................................................................................... 187 AOP concepts ................................................................................................ 187 Spring AOP capabilities and goals ................................................................... 189 AOP Proxies .................................................................................................. 190 8.2. @AspectJ support ........................................................................................... 190 Enabling @AspectJ Support ............................................................................ 190 Enabling @AspectJ Support with Java configuration ................................. 190 Enabling @AspectJ Support with XML configuration ................................. 191 Declaring an aspect ........................................................................................ 191 Declaring a pointcut ........................................................................................ 192 Supported Pointcut Designators .............................................................. 192 Combining pointcut expressions .............................................................. 194 Sharing common pointcut definitions ........................................................ 194 Examples ............................................................................................... 196 Writing good pointcuts ............................................................................ 198 Declaring advice ............................................................................................. 199 Before advice ......................................................................................... 199 After returning advice .............................................................................. 200 After throwing advice .............................................................................. 200 After (finally) advice ................................................................................ 201 Around advice ........................................................................................ 202 Advice parameters .................................................................................. 203 Advice ordering ...................................................................................... 206 Introductions ................................................................................................... 206 Aspect instantiation models ............................................................................. 207 Example ......................................................................................................... 208 8.3. Schema-based AOP support ............................................................................ 209 Declaring an aspect ........................................................................................ 210 Declaring a pointcut ........................................................................................ 210 Declaring advice ............................................................................................. 212 Before advice ......................................................................................... 212 After returning advice .............................................................................. 212 After throwing advice .............................................................................. 213 After (finally) advice ................................................................................ 214 Around advice ........................................................................................ 214 Advice parameters .................................................................................. 215 Advice ordering ...................................................................................... 216 Introductions ................................................................................................... 217 Aspect instantiation models ............................................................................. 217 Advisors ......................................................................................................... 217 Example ......................................................................................................... 218 8.4. Choosing which AOP declaration style to use .................................................... 220 Spring AOP or full AspectJ? ........................................................................... 220 @AspectJ or XML for Spring AOP? ................................................................. 221 8.5. Mixing aspect types ......................................................................................... 222 8.6. Proxying mechanisms ...................................................................................... 222 Understanding AOP proxies ............................................................................ 223 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation viii 8.7. Programmatic creation of @AspectJ Proxies ..................................................... 225 8.8. Using AspectJ with Spring applications ............................................................. 225 Using AspectJ to dependency inject domain objects with Spring ........................ 226 Unit testing @Configurable objects .......................................................... 228 Working with multiple application contexts ................................................ 228 Other Spring aspects for AspectJ .................................................................... 229 Configuring AspectJ aspects using Spring IoC ................................................. 229 Load-time weaving with AspectJ in the Spring Framework ................................. 230 A first example ....................................................................................... 231 Aspects .................................................................................................. 234 ' META-INF/aop.xml' ............................................................................... 234 Required libraries (JARS) ........................................................................ 234 Spring configuration ................................................................................ 235 Environment-specific configuration ........................................................... 237 8.9. Further Resources ........................................................................................... 239 9. Spring AOP APIs ....................................................................................................... 240 9.1. Introduction ..................................................................................................... 240 9.2. Pointcut API in Spring ...................................................................................... 240 Concepts ........................................................................................................ 240 Operations on pointcuts .................................................................................. 241 AspectJ expression pointcuts .......................................................................... 241 Convenience pointcut implementations ............................................................ 241 Static pointcuts ....................................................................................... 241 Dynamic pointcuts .................................................................................. 242 Pointcut superclasses ..................................................................................... 243 Custom pointcuts ............................................................................................ 243 9.3. Advice API in Spring ........................................................................................ 243 Advice lifecycles ............................................................................................. 243 Advice types in Spring .................................................................................... 244 Interception around advice ...................................................................... 244 Before advice ......................................................................................... 244 Throws advice ........................................................................................ 245 After Returning advice ............................................................................ 246 Introduction advice .................................................................................. 247 9.4. Advisor API in Spring ....................................................................................... 249 9.5. Using the ProxyFactoryBean to create AOP proxies ........................................... 250 Basics ............................................................................................................ 250 JavaBean properties ....................................................................................... 250 JDK- and CGLIB-based proxies ...................................................................... 251 Proxying interfaces ......................................................................................... 252 Proxying classes ............................................................................................ 254 Using global advisors ...................................................................................... 255 9.6. Concise proxy definitions ................................................................................. 255 9.7. Creating AOP proxies programmatically with the ProxyFactory ............................ 256 9.8. Manipulating advised objects ............................................................................ 257 9.9. Using the "auto-proxy" facility ........................................................................... 258 Autoproxy bean definitions .............................................................................. 258 BeanNameAutoProxyCreator ................................................................... 259 DefaultAdvisorAutoProxyCreator .............................................................. 259 AbstractAdvisorAutoProxyCreator ............................................................ 260 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation ix Using metadata-driven auto-proxying ............................................................... 260 9.10. Using TargetSources ...................................................................................... 262 Hot swappable target sources ......................................................................... 263 Pooling target sources .................................................................................... 263 Prototype target sources ................................................................................. 265 ThreadLocal target sources ............................................................................. 265 9.11. Defining new Advice types ............................................................................. 265 9.12. Further resources ........................................................................................... 266 10. Testing ..................................................................................................................... 267 10.1. Introduction to Spring Testing ......................................................................... 267 10.2. Unit Testing ................................................................................................... 267 Mock Objects ................................................................................................. 267 Environment ........................................................................................... 267 JNDI ...................................................................................................... 267 Servlet API ............................................................................................. 267 Portlet API ............................................................................................. 268 Unit Testing support Classes .......................................................................... 268 General utilities ...................................................................................... 268 Spring MVC ........................................................................................... 268 10.3. Integration Testing ......................................................................................... 268 Overview ........................................................................................................ 268 Goals of Integration Testing ............................................................................ 269 Context management and caching ........................................................... 269 Dependency Injection of test fixtures ....................................................... 269 Transaction management ........................................................................ 270 Support classes for integration testing ..................................................... 270 JDBC Testing Support .................................................................................... 271 Annotations .................................................................................................... 271 Spring Testing Annotations ..................................................................... 271 Standard Annotation Support .................................................................. 276 Spring JUnit Testing Annotations ............................................................. 277 Meta-Annotation Support for Testing ........................................................ 278 Spring TestContext Framework ....................................................................... 279 Key abstractions ..................................................................................... 280 Context management .............................................................................. 281 Dependency injection of test fixtures ........................................................ 297 Testing request and session scoped beans .............................................. 299 Transaction management ........................................................................ 301 TestContext Framework support classes .................................................. 304 Spring MVC Test Framework .......................................................................... 306 Server-Side Tests ................................................................................... 306 Client-Side REST Tests .......................................................................... 312 PetClinic Example .......................................................................................... 313 10.4. Further Resources ......................................................................................... 314 IV. Data Access ..................................................................................................................... 316 11. Transaction Management .......................................................................................... 317 11.1. Introduction to Spring Framework transaction management .............................. 317 11.2. Advantages of the Spring Framework’s transaction support model ..................... 317 Global transactions ......................................................................................... 317 Local transactions ........................................................................................... 318 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation x Spring Framework’s consistent programming model ......................................... 318 11.3. Understanding the Spring Framework transaction abstraction ............................ 319 11.4. Synchronizing resources with transactions ....................................................... 323 High-level synchronization approach ................................................................ 323 Low-level synchronization approach ................................................................. 323 TransactionAwareDataSourceProxy ................................................................. 324 11.5. Declarative transaction management ............................................................... 324 Understanding the Spring Framework’s declarative transaction implementation ... 325 Example of declarative transaction implementation ........................................... 326 Rolling back a declarative transaction .............................................................. 330 Configuring different transactional semantics for different beans ........................ 331 <tx:advice/> settings ....................................................................................... 333 Using @Transactional ..................................................................................... 335 @Transactional settings .......................................................................... 339 Multiple Transaction Managers with @Transactional ................................. 340 Custom shortcut annotations ................................................................... 341 Transaction propagation .................................................................................. 341 Required ................................................................................................ 342 RequiresNew .......................................................................................... 342 Nested ................................................................................................... 343 Advising transactional operations ..................................................................... 343 Using @Transactional with AspectJ ................................................................. 346 11.6. Programmatic transaction management ........................................................... 347 Using the TransactionTemplate ....................................................................... 347 Specifying transaction settings ................................................................ 349 Using the PlatformTransactionManager ............................................................ 349 11.7. Choosing between programmatic and declarative transaction management ........ 350 11.8. Application server-specific integration .............................................................. 350 IBM WebSphere ............................................................................................. 351 Oracle WebLogic Server ................................................................................. 351 11.9. Solutions to common problems ....................................................................... 351 Use of the wrong transaction manager for a specific DataSource ....................... 351 11.10. Further Resources ....................................................................................... 351 12. DAO support ............................................................................................................ 352 12.1. Introduction .................................................................................................... 352 12.2. Consistent exception hierarchy ....................................................................... 352 12.3. Annotations used for configuring DAO or Repository classes ............................ 353 13. Data access with JDBC ............................................................................................ 355 13.1. Introduction to Spring Framework JDBC .......................................................... 355 Choosing an approach for JDBC database access ........................................... 355 Package hierarchy .......................................................................................... 356 13.2. Using the JDBC core classes to control basic JDBC processing and error handling ................................................................................................................. 357 JdbcTemplate ................................................................................................. 357 Examples of JdbcTemplate class usage ................................................... 357 JdbcTemplate best practices ................................................................... 359 NamedParameterJdbcTemplate ....................................................................... 361 SQLExceptionTranslator .................................................................................. 363 Executing statements ...................................................................................... 365 Running queries ............................................................................................. 365 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation xi Updating the database .................................................................................... 366 Retrieving auto-generated keys ....................................................................... 367 13.3. Controlling database connections .................................................................... 367 DataSource .................................................................................................... 367 DataSourceUtils .............................................................................................. 369 SmartDataSource ........................................................................................... 369 AbstractDataSource ........................................................................................ 369 SingleConnectionDataSource .......................................................................... 369 DriverManagerDataSource .............................................................................. 369 TransactionAwareDataSourceProxy ................................................................. 370 DataSourceTransactionManager ...................................................................... 370 NativeJdbcExtractor ........................................................................................ 370 13.4. JDBC batch operations .................................................................................. 371 Basic batch operations with the JdbcTemplate ................................................. 371 Batch operations with a List of objects ............................................................. 372 Batch operations with multiple batches ............................................................ 373 13.5. Simplifying JDBC operations with the SimpleJdbc classes ................................ 374 Inserting data using SimpleJdbcInsert .............................................................. 374 Retrieving auto-generated keys using SimpleJdbcInsert .................................... 375 Specifying columns for a SimpleJdbcInsert ...................................................... 376 Using SqlParameterSource to provide parameter values ................................... 376 Calling a stored procedure with SimpleJdbcCall ............................................... 377 Explicitly declaring parameters to use for a SimpleJdbcCall ............................... 379 How to define SqlParameters .......................................................................... 380 Calling a stored function using SimpleJdbcCall ................................................. 381 Returning ResultSet/REF Cursor from a SimpleJdbcCall ................................... 381 13.6. Modeling JDBC operations as Java objects ..................................................... 382 SqlQuery ........................................................................................................ 383 MappingSqlQuery ........................................................................................... 383 SqlUpdate ...................................................................................................... 384 StoredProcedure ............................................................................................. 385 13.7. Common problems with parameter and data value handling .............................. 388 Providing SQL type information for parameters ................................................. 389 Handling BLOB and CLOB objects .................................................................. 389 Passing in lists of values for IN clause ............................................................ 390 Handling complex types for stored procedure calls ........................................... 391 13.8. Embedded database support .......................................................................... 392 Why use an embedded database? .................................................................. 392 Creating an embedded database instance using Spring XML ............................ 392 Creating an embedded database instance programmatically .............................. 392 Extending the embedded database support ...................................................... 393 Using HSQL ................................................................................................... 393 Using H2 ........................................................................................................ 393 Using Derby ................................................................................................... 393 Testing data access logic with an embedded database ..................................... 393 13.9. Initializing a DataSource ................................................................................. 394 Initializing a database instance using Spring XML ............................................. 394 Initialization of Other Components that Depend on the Database ............... 395 14. Object Relational Mapping (ORM) Data Access .......................................................... 397 14.1. Introduction to ORM with Spring ..................................................................... 397 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation xii 14.2. General ORM integration considerations ......................................................... 398 Resource and transaction management ........................................................... 398 Exception translation ....................................................................................... 399 14.3. Hibernate ....................................................................................................... 399 SessionFactory setup in a Spring container ...................................................... 400 Implementing DAOs based on plain Hibernate 3 API ........................................ 400 Declarative transaction demarcation ................................................................ 402 Programmatic transaction demarcation ............................................................ 404 Transaction management strategies ................................................................ 405 Comparing container-managed and locally defined resources ............................ 407 Spurious application server warnings with Hibernate ......................................... 408 14.4. JDO .............................................................................................................. 409 PersistenceManagerFactory setup ................................................................... 409 Implementing DAOs based on the plain JDO API ............................................. 410 Transaction management ................................................................................ 412 JdoDialect ...................................................................................................... 413 14.5. JPA ............................................................................................................... 414 Three options for JPA setup in a Spring environment ........................................ 414 LocalEntityManagerFactoryBean .............................................................. 414 Obtaining an EntityManagerFactory from JNDI ......................................... 415 LocalContainerEntityManagerFactoryBean ............................................... 415 Dealing with multiple persistence units ..................................................... 417 Implementing DAOs based on plain JPA .......................................................... 418 Transaction Management ................................................................................ 420 JpaDialect ...................................................................................................... 421 15. Marshalling XML using O/X Mappers ......................................................................... 423 15.1. Introduction .................................................................................................... 423 Ease of configuration ...................................................................................... 423 Consistent Interfaces ...................................................................................... 423 Consistent Exception Hierarchy ....................................................................... 423 15.2. Marshaller and Unmarshaller .......................................................................... 423 Marshaller ...................................................................................................... 423 Unmarshaller .................................................................................................. 424 XmlMappingException ..................................................................................... 425 15.3. Using Marshaller and Unmarshaller ................................................................. 425 15.4. XML Schema-based Configuration .................................................................. 427 15.5. JAXB ............................................................................................................. 427 Jaxb2Marshaller ............................................................................................. 428 XML Schema-based Configuration ........................................................... 428 15.6. Castor ........................................................................................................... 429 CastorMarshaller ............................................................................................ 429 Mapping ......................................................................................................... 429 XML Schema-based Configuration ........................................................... 429 15.7. XMLBeans ..................................................................................................... 430 XmlBeansMarshaller ....................................................................................... 430 XML Schema-based Configuration ........................................................... 430 15.8. JiBX .............................................................................................................. 431 JibxMarshaller ................................................................................................ 431 XML Schema-based Configuration ........................................................... 431 15.9. XStream ........................................................................................................ 432 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation xiii XStreamMarshaller ......................................................................................... 432 V. The Web ........................................................................................................................... 434 16. Web MVC framework ................................................................................................ 435 16.1. Introduction to Spring Web MVC framework .................................................... 435 Features of Spring Web MVC ......................................................................... 436 Pluggability of other MVC implementations ...................................................... 437 16.2. The DispatcherServlet .................................................................................... 437 Special Bean Types In the WebApplicationContext ........................................... 440 Default DispatcherServlet Configuration ........................................................... 441 DispatcherServlet Processing Sequence .......................................................... 441 16.3. Implementing Controllers ................................................................................ 443 Defining a controller with @Controller .............................................................. 443 Mapping Requests With Using @RequestMapping ........................................... 444 New Support Classes for @RequestMapping methods in Spring MVC 3.1 .. 446 URI Template Patterns ........................................................................... 447 URI Template Patterns with Regular Expressions ..................................... 448 Path Patterns ......................................................................................... 449 Patterns with Placeholders ...................................................................... 449 Matrix Variables ...................................................................................... 449 Consumable Media Types ....................................................................... 450 Producible Media Types .......................................................................... 451 Request Parameters and Header Values ................................................. 451 Defining @RequestMapping handler methods .................................................. 452 Supported method argument types .......................................................... 452 Supported method return types ............................................................... 454 Binding request parameters to method parameters with @RequestParam ... 455 Mapping the request body with the @RequestBody annotation .................. 456 Mapping the response body with the @ResponseBody annotation ............. 457 Creating REST Controllers with the @RestController annotation ................ 457 Using HttpEntity ...................................................................................... 457 Using @ModelAttribute on a method ....................................................... 458 Using @ModelAttribute on a method argument ......................................... 459 Using @SessionAttributes to store model attributes in the HTTP session between requests ................................................................................... 461 Specifying redirect and flash attributes ..................................................... 461 Working with "application/x-www-form-urlencoded" data ............................ 462 Mapping cookie values with the @CookieValue annotation ........................ 462 Mapping request header attributes with the @RequestHeader annotation ... 463 Method Parameters And Type Conversion ............................................... 463 Customizing WebDataBinder initialization ................................................. 464 Support for the Last-Modified Response Header To Facilitate Content Caching ................................................................................................. 465 Assisting Controllers with the @ControllerAdvice annotation ...................... 465 Asynchronous Request Processing .................................................................. 466 Exception Handling for Async Requests ................................................... 467 Intercepting Async Requests ................................................................... 467 Configuration for Async Request Processing ............................................ 468 Testing Controllers ......................................................................................... 469 16.4. Handler mappings .......................................................................................... 469 Intercepting requests with a HandlerInterceptor ................................................ 469 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation xiv 16.5. Resolving views ............................................................................................. 471 Resolving views with the ViewResolver interface .............................................. 471 Chaining ViewResolvers ................................................................................. 473 Redirecting to views ....................................................................................... 474 RedirectView .......................................................................................... 474 The redirect: prefix ................................................................................. 475 The forward: prefix ................................................................................. 475 ContentNegotiatingViewResolver ..................................................................... 475 16.6. Using flash attributes ..................................................................................... 478 16.7. Building URIs ................................................................................................. 479 16.8. Building URIs to Controllers and methods ....................................................... 480 16.9. Using locales ................................................................................................. 480 Obtaining Time Zone Information .................................................................... 481 AcceptHeaderLocaleResolver .......................................................................... 481 CookieLocaleResolver ..................................................................................... 481 SessionLocaleResolver ................................................................................... 481 LocaleChangeInterceptor ................................................................................ 482 16.10. Using themes ............................................................................................... 482 Overview of themes ........................................................................................ 482 Defining themes ............................................................................................. 482 Theme resolvers ............................................................................................. 483 16.11. Spring’s multipart (file upload) support ........................................................... 483 Introduction .................................................................................................... 483 Using a MultipartResolver with Commons FileUpload ........................................ 484 Using a MultipartResolver with Servlet 3.0 ....................................................... 484 Handling a file upload in a form ...................................................................... 484 Handling a file upload request from programmatic clients .................................. 486 16.12. Handling exceptions ..................................................................................... 486 HandlerExceptionResolver ............
Not Using Commons Logging ................................................................... 12 Using SLF4J ............................................................................................ 13 Using Log4J ............................................................................................. 14 II. What’s New in Spring Framework 4.x .................................................................................... 16 3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .................................................................. 17 3.2. Removed Deprecated Packages and Methods .................................................... 17 3.3. Java 8 (as well as 6 and 7) ............................................................................... 17 3.4. Java EE 6 and 7 ............................................................................................... 18 3.5. Groovy Bean Definition DSL .............................................................................. 18 3.6. Core Container Improvements ............................................................................ 19 3.7. General Web Improvements ............................................................................... 19 3.8. WebSocket, SockJS, and STOMP Messaging ..................................................... 19 3.9. Testing Improvements ........................................................................................ 20 III. Core Technologies .............................................................................................................. 21 4. The IoC container ........................................................................................................ 22 4.1. Introduction to the Spring IoC container and beans .............................................. 22 4.2. Container overview ............................................................................................ 22 Configuration metadata ..................................................................................... 23 Instantiating a container .................................................................................... 24 Composing XML-based configuration metadata .......................................... 25 Using the container .......................................................................................... 26 4.3. Bean overview ................................................................................................... 27 Naming beans .................................................................................................. 28 Aliasing a bean outside the bean definition ................................................ 28 Instantiating beans ........................................................................................... 29 Instantiation with a constructor .................................................................. 29 Instantiation with a static factory method .................................................... 30 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation iii Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ................................................................................................... 32 Dependency injection ....................................................................................... 32 Constructor-based dependency injection .................................................... 32 Setter-based dependency injection ............................................................ 34 Dependency resolution process ................................................................. 35 Examples of dependency injection ............................................................. 36 Dependencies and configuration in detail ........................................................... 38 Straight values (primitives, Strings, and so on) ........................................... 38 References to other beans (collaborators) .................................................. 40 Inner beans .............................................................................................. 41 Collections ............................................................................................... 41 Null and empty string values ..................................................................... 44 XML shortcut with the p-namespace .......................................................... 44 XML shortcut with the c-namespace .......................................................... 46 Compound property names ....................................................................... 46 Using depends-on ............................................................................................ 47 Lazy-initialized beans ....................................................................................... 47 Autowiring collaborators .................................................................................... 48 Limitations and disadvantages of autowiring ............................................... 49 Excluding a bean from autowiring .............................................................. 50 Method injection ............................................................................................... 50 Lookup method injection ........................................................................... 51 Arbitrary method replacement ................................................................... 53 4.5. Bean scopes ..................................................................................................... 54 The singleton scope ......................................................................................... 55 The prototype scope ......................................................................................... 55 Singleton beans with prototype-bean dependencies ............................................ 56 Request, session, and global session scopes .................................................... 56 Initial web configuration ............................................................................ 57 Request scope ......................................................................................... 58 Session scope .......................................................................................... 58 Global session scope ............................................................................... 58 Scoped beans as dependencies ................................................................ 58 Custom scopes ................................................................................................ 60 Creating a custom scope .......................................................................... 60 Using a custom scope .............................................................................. 61 4.6. Customizing the nature of a bean ....................................................................... 62 Lifecycle callbacks ............................................................................................ 62 Initialization callbacks ............................................................................... 63 Destruction callbacks ................................................................................ 64 Default initialization and destroy methods .................................................. 64 Combining lifecycle mechanisms ............................................................... 66 Startup and shutdown callbacks ................................................................ 66 Shutting down the Spring IoC container gracefully in non-web applications ................................................................................................................. 68 ApplicationContextAware and BeanNameAware ................................................. 68 Other Aware interfaces ..................................................................................... 69 4.7. Bean definition inheritance ................................................................................. 71 4.8. Container Extension Points ................................................................................ 72 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation iv Customizing beans using a BeanPostProcessor ................................................. 72 Example: Hello World, BeanPostProcessor-style ........................................ 74 Example: The RequiredAnnotationBeanPostProcessor ............................... 75 Customizing configuration metadata with a BeanFactoryPostProcessor ................ 75 Example: the Class name substitution PropertyPlaceholderConfigurer .......... 76 Example: the PropertyOverrideConfigurer .................................................. 77 Customizing instantiation logic with a FactoryBean ............................................. 78 4.9. Annotation-based container configuration ............................................................ 79 @Required ....................................................................................................... 80 @Autowired ..................................................................................................... 80 Fine-tuning annotation-based autowiring with qualifiers ....................................... 83 Using generics as autowiring qualifiers .............................................................. 89 CustomAutowireConfigurer ................................................................................ 90 @Resource ...................................................................................................... 90 @PostConstruct and @PreDestroy .................................................................... 92 4.10. Classpath scanning and managed components ................................................. 92 @Component and further stereotype annotations ............................................... 93 Meta-annotations .............................................................................................. 93 Automatically detecting classes and registering bean definitions .......................... 94 Using filters to customize scanning ................................................................... 95 Defining bean metadata within components ....................................................... 96 Naming autodetected components ..................................................................... 97 Providing a scope for autodetected components ................................................ 98 Providing qualifier metadata with annotations ..................................................... 99 4.11. Using JSR 330 Standard Annotations ............................................................... 99 Dependency Injection with @Inject and @Named ............................................. 100 @Named: a standard equivalent to the @Component annotation ....................... 100 Limitations of the standard approach ............................................................... 101 4.12. Java-based container configuration ................................................................. 102 Basic concepts: @Bean and @Configuration ................................................... 102 Instantiating the Spring container using AnnotationConfigApplicationContext ....... 103 Simple construction ................................................................................ 103 Building the container programmatically using register(Class<?>…) ........... 104 Enabling component scanning with scan(String…) .................................... 104 Support for web applications with AnnotationConfigWebApplicationContext ............................................................................................................... 105 Using the @Bean annotation .......................................................................... 106 Declaring a bean .................................................................................... 107 Receiving lifecycle callbacks ................................................................... 107 Specifying bean scope ............................................................................ 108 Customizing bean naming ....................................................................... 109 Bean aliasing ......................................................................................... 109 Bean description ..................................................................................... 110 Using the @Configuration annotation ............................................................... 110 Injecting inter-bean dependencies ............................................................ 110 Lookup method injection ......................................................................... 111 Further information about how Java-based configuration works internally .... 111 Composing Java-based configurations ............................................................. 112 Using the @Import annotation ................................................................. 112 Conditionally including @Configuration classes or @Beans ....................... 116 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation v Combining Java and XML configuration ................................................... 117 4.13. Bean definition profiles and environment abstraction ........................................ 120 4.14. PropertySource Abstraction ............................................................................ 120 4.15. Registering a LoadTimeWeaver ...................................................................... 120 4.16. Additional Capabilities of the ApplicationContext .............................................. 120 Internationalization using MessageSource ........................................................ 121 Standard and Custom Events .......................................................................... 124 Convenient access to low-level resources ........................................................ 127 Convenient ApplicationContext instantiation for web applications ....................... 128 Deploying a Spring ApplicationContext as a J2EE RAR file ............................... 128 4.17. The BeanFactory ........................................................................................... 129 BeanFactory or ApplicationContext? ................................................................ 129 Glue code and the evil singleton ..................................................................... 131 5. Resources .................................................................................................................. 132 5.1. Introduction ..................................................................................................... 132 5.2. The Resource interface .................................................................................... 132 5.3. Built-in Resource implementations .................................................................... 133 UrlResource ................................................................................................... 133 ClassPathResource ........................................................................................ 133 FileSystemResource ....................................................................................... 134 ServletContextResource .................................................................................. 134 InputStreamResource ..................................................................................... 134 ByteArrayResource ......................................................................................... 134 5.4. The ResourceLoader ....................................................................................... 134 5.5. The ResourceLoaderAware interface ................................................................ 135 5.6. Resources as dependencies ............................................................................. 136 5.7. Application contexts and Resource paths .......................................................... 137 Constructing application contexts ..................................................................... 137 Constructing ClassPathXmlApplicationContext instances - shortcuts .......... 137 Wildcards in application context constructor resource paths ............................... 138 Ant-style Patterns ................................................................................... 138 The Classpath*: portability classpath*: prefix ............................................ 139 Other notes relating to wildcards ............................................................. 139 FileSystemResource caveats .......................................................................... 140 6. Validation, Data Binding, and Type Conversion ............................................................ 141 6.1. Introduction ..................................................................................................... 141 6.2. Validation using Spring’s Validator interface ...................................................... 141 6.3. Resolving codes to error messages .................................................................. 143 6.4. Bean manipulation and the BeanWrapper ......................................................... 144 Setting and getting basic and nested properties ............................................... 144 Built-in PropertyEditor implementations ............................................................ 146 Registering additional custom PropertyEditors .......................................... 149 6.5. Spring Type Conversion ................................................................................... 151 Converter SPI ................................................................................................ 151 ConverterFactory ............................................................................................ 152 GenericConverter ........................................................................................... 153 ConditionalGenericConverter ................................................................... 154 ConversionService API ................................................................................... 154 Configuring a ConversionService ..................................................................... 154 Using a ConversionService programmatically ................................................... 155 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation vi 6.6. Spring Field Formatting .................................................................................... 155 Formatter SPI ................................................................................................. 156 Annotation-driven Formatting ........................................................................... 157 Format Annotation API ............................................................................ 158 FormatterRegistry SPI ..................................................................................... 159 FormatterRegistrar SPI ................................................................................... 159 Configuring Formatting in Spring MVC ............................................................. 159 6.7. Configuring a global date & time format ............................................................ 161 6.8. Spring Validation ............................................................................................. 163 Overview of the JSR-303 Bean Validation API ................................................. 163 Configuring a Bean Validation Provider ............................................................ 164 Injecting a Validator ................................................................................ 164 Configuring Custom Constraints .............................................................. 164 Additional Configuration Options .............................................................. 165 Configuring a DataBinder ................................................................................ 165 Spring MVC 3 Validation ................................................................................. 166 Triggering @Controller Input Validation .................................................... 166 Configuring a Validator for use by Spring MVC ......................................... 166 Configuring a JSR-303/JSR-349 Validator for use by Spring MVC .............. 167 7. Spring Expression Language (SpEL) ........................................................................... 168 7.1. Introduction ..................................................................................................... 168 7.2. Feature Overview ............................................................................................ 168 7.3. Expression Evaluation using Spring’s Expression Interface ................................. 169 The EvaluationContext interface ...................................................................... 171 Type Conversion .................................................................................... 171 7.4. Expression support for defining bean definitions ................................................ 172 XML based configuration ................................................................................ 172 Annotation-based configuration ........................................................................ 173 7.5. Language Reference ........................................................................................ 174 Literal expressions .......................................................................................... 174 Properties, Arrays, Lists, Maps, Indexers ......................................................... 174 Inline lists ....................................................................................................... 175 Array construction ........................................................................................... 175 Methods ......................................................................................................... 176 Operators ....................................................................................................... 176 Relational operators ................................................................................ 176 Logical operators .................................................................................... 177 Mathematical operators ........................................................................... 177 Assignment .................................................................................................... 178 Types ............................................................................................................. 178 Constructors ................................................................................................... 179 Variables ........................................................................................................ 179 The #this and #root variables .................................................................. 179 Functions ....................................................................................................... 180 Bean references ............................................................................................. 180 Ternary Operator (If-Then-Else) ....................................................................... 180 The Elvis Operator ......................................................................................... 181 Safe Navigation operator ................................................................................ 181 Collection Selection ........................................................................................ 182 Collection Projection ....................................................................................... 182 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation vii Expression templating ..................................................................................... 183 7.6. Classes used in the examples .......................................................................... 183 8. Aspect Oriented Programming with Spring ................................................................... 187 8.1. Introduction ..................................................................................................... 187 AOP concepts ................................................................................................ 187 Spring AOP capabilities and goals ................................................................... 189 AOP Proxies .................................................................................................. 190 8.2. @AspectJ support ........................................................................................... 190 Enabling @AspectJ Support ............................................................................ 190 Enabling @AspectJ Support with Java configuration ................................. 190 Enabling @AspectJ Support with XML configuration ................................. 191 Declaring an aspect ........................................................................................ 191 Declaring a pointcut ........................................................................................ 192 Supported Pointcut Designators .............................................................. 192 Combining pointcut expressions .............................................................. 194 Sharing common pointcut definitions ........................................................ 194 Examples ............................................................................................... 196 Writing good pointcuts ............................................................................ 198 Declaring advice ............................................................................................. 199 Before advice ......................................................................................... 199 After returning advice .............................................................................. 200 After throwing advice .............................................................................. 200 After (finally) advice ................................................................................ 201 Around advice ........................................................................................ 202 Advice parameters .................................................................................. 203 Advice ordering ...................................................................................... 206 Introductions ................................................................................................... 206 Aspect instantiation models ............................................................................. 207 Example ......................................................................................................... 208 8.3. Schema-based AOP support ............................................................................ 209 Declaring an aspect ........................................................................................ 210 Declaring a pointcut ........................................................................................ 210 Declaring advice ............................................................................................. 212 Before advice ......................................................................................... 212 After returning advice .............................................................................. 212 After throwing advice .............................................................................. 213 After (finally) advice ................................................................................ 214 Around advice ........................................................................................ 214 Advice parameters .................................................................................. 215 Advice ordering ...................................................................................... 216 Introductions ................................................................................................... 217 Aspect instantiation models ............................................................................. 217 Advisors ......................................................................................................... 217 Example ......................................................................................................... 218 8.4. Choosing which AOP declaration style to use .................................................... 220 Spring AOP or full AspectJ? ........................................................................... 220 @AspectJ or XML for Spring AOP? ................................................................. 221 8.5. Mixing aspect types ......................................................................................... 222 8.6. Proxying mechanisms ...................................................................................... 222 Understanding AOP proxies ............................................................................ 223 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation viii 8.7. Programmatic creation of @AspectJ Proxies ..................................................... 225 8.8. Using AspectJ with Spring applications ............................................................. 225 Using AspectJ to dependency inject domain objects with Spring ........................ 226 Unit testing @Configurable objects .......................................................... 228 Working with multiple application contexts ................................................ 228 Other Spring aspects for AspectJ .................................................................... 229 Configuring AspectJ aspects using Spring IoC ................................................. 229 Load-time weaving with AspectJ in the Spring Framework ................................. 230 A first example ....................................................................................... 231 Aspects .................................................................................................. 234 ' META-INF/aop.xml' ............................................................................... 234 Required libraries (JARS) ........................................................................ 234 Spring configuration ................................................................................ 235 Environment-specific configuration ........................................................... 237 8.9. Further Resources ........................................................................................... 239 9. Spring AOP APIs ....................................................................................................... 240 9.1. Introduction ..................................................................................................... 240 9.2. Pointcut API in Spring ...................................................................................... 240 Concepts ........................................................................................................ 240 Operations on pointcuts .................................................................................. 241 AspectJ expression pointcuts .......................................................................... 241 Convenience pointcut implementations ............................................................ 241 Static pointcuts ....................................................................................... 241 Dynamic pointcuts .................................................................................. 242 Pointcut superclasses ..................................................................................... 243 Custom pointcuts ............................................................................................ 243 9.3. Advice API in Spring ........................................................................................ 243 Advice lifecycles ............................................................................................. 243 Advice types in Spring .................................................................................... 244 Interception around advice ...................................................................... 244 Before advice ......................................................................................... 244 Throws advice ........................................................................................ 245 After Returning advice ............................................................................ 246 Introduction advice .................................................................................. 247 9.4. Advisor API in Spring ....................................................................................... 249 9.5. Using the ProxyFactoryBean to create AOP proxies ........................................... 250 Basics ............................................................................................................ 250 JavaBean properties ....................................................................................... 250 JDK- and CGLIB-based proxies ...................................................................... 251 Proxying interfaces ......................................................................................... 252 Proxying classes ............................................................................................ 254 Using global advisors ...................................................................................... 255 9.6. Concise proxy definitions ................................................................................. 255 9.7. Creating AOP proxies programmatically with the ProxyFactory ............................ 256 9.8. Manipulating advised objects ............................................................................ 257 9.9. Using the "auto-proxy" facility ........................................................................... 258 Autoproxy bean definitions .............................................................................. 258 BeanNameAutoProxyCreator ................................................................... 259 DefaultAdvisorAutoProxyCreator .............................................................. 259 AbstractAdvisorAutoProxyCreator ............................................................ 260 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation ix Using metadata-driven auto-proxying ............................................................... 260 9.10. Using TargetSources ...................................................................................... 262 Hot swappable target sources ......................................................................... 263 Pooling target sources .................................................................................... 263 Prototype target sources ................................................................................. 265 ThreadLocal target sources ............................................................................. 265 9.11. Defining new Advice types ............................................................................. 265 9.12. Further resources ........................................................................................... 266 10. Testing ..................................................................................................................... 267 10.1. Introduction to Spring Testing ......................................................................... 267 10.2. Unit Testing ................................................................................................... 267 Mock Objects ................................................................................................. 267 Environment ........................................................................................... 267 JNDI ...................................................................................................... 267 Servlet API ............................................................................................. 267 Portlet API ............................................................................................. 268 Unit Testing support Classes .......................................................................... 268 General utilities ...................................................................................... 268 Spring MVC ........................................................................................... 268 10.3. Integration Testing ......................................................................................... 268 Overview ........................................................................................................ 268 Goals of Integration Testing ............................................................................ 269 Context management and caching ........................................................... 269 Dependency Injection of test fixtures ....................................................... 269 Transaction management ........................................................................ 270 Support classes for integration testing ..................................................... 270 JDBC Testing Support .................................................................................... 271 Annotations .................................................................................................... 271 Spring Testing Annotations ..................................................................... 271 Standard Annotation Support .................................................................. 276 Spring JUnit Testing Annotations ............................................................. 277 Meta-Annotation Support for Testing ........................................................ 278 Spring TestContext Framework ....................................................................... 279 Key abstractions ..................................................................................... 280 Context management .............................................................................. 281 Dependency injection of test fixtures ........................................................ 297 Testing request and session scoped beans .............................................. 299 Transaction management ........................................................................ 301 TestContext Framework support classes .................................................. 304 Spring MVC Test Framework .......................................................................... 306 Server-Side Tests ................................................................................... 306 Client-Side REST Tests .......................................................................... 312 PetClinic Example .......................................................................................... 313 10.4. Further Resources ......................................................................................... 314 IV. Data Access ..................................................................................................................... 316 11. Transaction Management .......................................................................................... 317 11.1. Introduction to Spring Framework transaction management .............................. 317 11.2. Advantages of the Spring Framework’s transaction support model ..................... 317 Global transactions ......................................................................................... 317 Local transactions ........................................................................................... 318 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation x Spring Framework’s consistent programming model ......................................... 318 11.3. Understanding the Spring Framework transaction abstraction ............................ 319 11.4. Synchronizing resources with transactions ....................................................... 323 High-level synchronization approach ................................................................ 323 Low-level synchronization approach ................................................................. 323 TransactionAwareDataSourceProxy ................................................................. 324 11.5. Declarative transaction management ............................................................... 324 Understanding the Spring Framework’s declarative transaction implementation ... 325 Example of declarative transaction implementation ........................................... 326 Rolling back a declarative transaction .............................................................. 330 Configuring different transactional semantics for different beans ........................ 331 <tx:advice/> settings ....................................................................................... 333 Using @Transactional ..................................................................................... 335 @Transactional settings .......................................................................... 339 Multiple Transaction Managers with @Transactional ................................. 340 Custom shortcut annotations ................................................................... 341 Transaction propagation .................................................................................. 341 Required ................................................................................................ 342 RequiresNew .......................................................................................... 342 Nested ................................................................................................... 343 Advising transactional operations ..................................................................... 343 Using @Transactional with AspectJ ................................................................. 346 11.6. Programmatic transaction management ........................................................... 347 Using the TransactionTemplate ....................................................................... 347 Specifying transaction settings ................................................................ 349 Using the PlatformTransactionManager ............................................................ 349 11.7. Choosing between programmatic and declarative transaction management ........ 350 11.8. Application server-specific integration .............................................................. 350 IBM WebSphere ............................................................................................. 351 Oracle WebLogic Server ................................................................................. 351 11.9. Solutions to common problems ....................................................................... 351 Use of the wrong transaction manager for a specific DataSource ....................... 351 11.10. Further Resources ....................................................................................... 351 12. DAO support ............................................................................................................ 352 12.1. Introduction .................................................................................................... 352 12.2. Consistent exception hierarchy ....................................................................... 352 12.3. Annotations used for configuring DAO or Repository classes ............................ 353 13. Data access with JDBC ............................................................................................ 355 13.1. Introduction to Spring Framework JDBC .......................................................... 355 Choosing an approach for JDBC database access ........................................... 355 Package hierarchy .......................................................................................... 356 13.2. Using the JDBC core classes to control basic JDBC processing and error handling ................................................................................................................. 357 JdbcTemplate ................................................................................................. 357 Examples of JdbcTemplate class usage ................................................... 357 JdbcTemplate best practices ................................................................... 359 NamedParameterJdbcTemplate ....................................................................... 361 SQLExceptionTranslator .................................................................................. 363 Executing statements ...................................................................................... 365 Running queries ............................................................................................. 365 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation xi Updating the database .................................................................................... 366 Retrieving auto-generated keys ....................................................................... 367 13.3. Controlling database connections .................................................................... 367 DataSource .................................................................................................... 367 DataSourceUtils .............................................................................................. 369 SmartDataSource ........................................................................................... 369 AbstractDataSource ........................................................................................ 369 SingleConnectionDataSource .......................................................................... 369 DriverManagerDataSource .............................................................................. 369 TransactionAwareDataSourceProxy ................................................................. 370 DataSourceTransactionManager ...................................................................... 370 NativeJdbcExtractor ........................................................................................ 370 13.4. JDBC batch operations .................................................................................. 371 Basic batch operations with the JdbcTemplate ................................................. 371 Batch operations with a List of objects ............................................................. 372 Batch operations with multiple batches ............................................................ 373 13.5. Simplifying JDBC operations with the SimpleJdbc classes ................................ 374 Inserting data using SimpleJdbcInsert .............................................................. 374 Retrieving auto-generated keys using SimpleJdbcInsert .................................... 375 Specifying columns for a SimpleJdbcInsert ...................................................... 376 Using SqlParameterSource to provide parameter values ................................... 376 Calling a stored procedure with SimpleJdbcCall ............................................... 377 Explicitly declaring parameters to use for a SimpleJdbcCall ............................... 379 How to define SqlParameters .......................................................................... 380 Calling a stored function using SimpleJdbcCall ................................................. 381 Returning ResultSet/REF Cursor from a SimpleJdbcCall ................................... 381 13.6. Modeling JDBC operations as Java objects ..................................................... 382 SqlQuery ........................................................................................................ 383 MappingSqlQuery ........................................................................................... 383 SqlUpdate ...................................................................................................... 384 StoredProcedure ............................................................................................. 385 13.7. Common problems with parameter and data value handling .............................. 388 Providing SQL type information for parameters ................................................. 389 Handling BLOB and CLOB objects .................................................................. 389 Passing in lists of values for IN clause ............................................................ 390 Handling complex types for stored procedure calls ........................................... 391 13.8. Embedded database support .......................................................................... 392 Why use an embedded database? .................................................................. 392 Creating an embedded database instance using Spring XML ............................ 392 Creating an embedded database instance programmatically .............................. 392 Extending the embedded database support ...................................................... 393 Using HSQL ................................................................................................... 393 Using H2 ........................................................................................................ 393 Using Derby ................................................................................................... 393 Testing data access logic with an embedded database ..................................... 393 13.9. Initializing a DataSource ................................................................................. 394 Initializing a database instance using Spring XML ............................................. 394 Initialization of Other Components that Depend on the Database ............... 395 14. Object Relational Mapping (ORM) Data Access .......................................................... 397 14.1. Introduction to ORM with Spring ..................................................................... 397 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation xii 14.2. General ORM integration considerations ......................................................... 398 Resource and transaction management ........................................................... 398 Exception translation ....................................................................................... 399 14.3. Hibernate ....................................................................................................... 399 SessionFactory setup in a Spring container ...................................................... 400 Implementing DAOs based on plain Hibernate 3 API ........................................ 400 Declarative transaction demarcation ................................................................ 402 Programmatic transaction demarcation ............................................................ 404 Transaction management strategies ................................................................ 405 Comparing container-managed and locally defined resources ............................ 407 Spurious application server warnings with Hibernate ......................................... 408 14.4. JDO .............................................................................................................. 409 PersistenceManagerFactory setup ................................................................... 409 Implementing DAOs based on the plain JDO API ............................................. 410 Transaction management ................................................................................ 412 JdoDialect ...................................................................................................... 413 14.5. JPA ............................................................................................................... 414 Three options for JPA setup in a Spring environment ........................................ 414 LocalEntityManagerFactoryBean .............................................................. 414 Obtaining an EntityManagerFactory from JNDI ......................................... 415 LocalContainerEntityManagerFactoryBean ............................................... 415 Dealing with multiple persistence units ..................................................... 417 Implementing DAOs based on plain JPA .......................................................... 418 Transaction Management ................................................................................ 420 JpaDialect ...................................................................................................... 421 15. Marshalling XML using O/X Mappers ......................................................................... 423 15.1. Introduction .................................................................................................... 423 Ease of configuration ...................................................................................... 423 Consistent Interfaces ...................................................................................... 423 Consistent Exception Hierarchy ....................................................................... 423 15.2. Marshaller and Unmarshaller .......................................................................... 423 Marshaller ...................................................................................................... 423 Unmarshaller .................................................................................................. 424 XmlMappingException ..................................................................................... 425 15.3. Using Marshaller and Unmarshaller ................................................................. 425 15.4. XML Schema-based Configuration .................................................................. 427 15.5. JAXB ............................................................................................................. 427 Jaxb2Marshaller ............................................................................................. 428 XML Schema-based Configuration ........................................................... 428 15.6. Castor ........................................................................................................... 429 CastorMarshaller ............................................................................................ 429 Mapping ......................................................................................................... 429 XML Schema-based Configuration ........................................................... 429 15.7. XMLBeans ..................................................................................................... 430 XmlBeansMarshaller ....................................................................................... 430 XML Schema-based Configuration ........................................................... 430 15.8. JiBX .............................................................................................................. 431 JibxMarshaller ................................................................................................ 431 XML Schema-based Configuration ........................................................... 431 15.9. XStream ........................................................................................................ 432 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation xiii XStreamMarshaller ......................................................................................... 432 V. The Web ........................................................................................................................... 434 16. Web MVC framework ................................................................................................ 435 16.1. Introduction to Spring Web MVC framework .................................................... 435 Features of Spring Web MVC ......................................................................... 436 Pluggability of other MVC implementations ...................................................... 437 16.2. The DispatcherServlet .................................................................................... 437 Special Bean Types In the WebApplicationContext ........................................... 440 Default DispatcherServlet Configuration ........................................................... 441 DispatcherServlet Processing Sequence .......................................................... 441 16.3. Implementing Controllers ................................................................................ 443 Defining a controller with @Controller .............................................................. 443 Mapping Requests With Using @RequestMapping ........................................... 444 New Support Classes for @RequestMapping methods in Spring MVC 3.1 .. 446 URI Template Patterns ........................................................................... 447 URI Template Patterns with Regular Expressions ..................................... 448 Path Patterns ......................................................................................... 449 Patterns with Placeholders ...................................................................... 449 Matrix Variables ...................................................................................... 449 Consumable Media Types ....................................................................... 450 Producible Media Types .......................................................................... 451 Request Parameters and Header Values ................................................. 451 Defining @RequestMapping handler methods .................................................. 452 Supported method argument types .......................................................... 452 Supported method return types ............................................................... 454 Binding request parameters to method parameters with @RequestParam ... 455 Mapping the request body with the @RequestBody annotation .................. 456 Mapping the response body with the @ResponseBody annotation ............. 457 Creating REST Controllers with the @RestController annotation ................ 457 Using HttpEntity ...................................................................................... 457 Using @ModelAttribute on a method ....................................................... 458 Using @ModelAttribute on a method argument ......................................... 459 Using @SessionAttributes to store model attributes in the HTTP session between requests ................................................................................... 461 Specifying redirect and flash attributes ..................................................... 461 Working with "application/x-www-form-urlencoded" data ............................ 462 Mapping cookie values with the @CookieValue annotation ........................ 462 Mapping request header attributes with the @RequestHeader annotation ... 463 Method Parameters And Type Conversion ............................................... 463 Customizing WebDataBinder initialization ................................................. 464 Support for the Last-Modified Response Header To Facilitate Content Caching ................................................................................................. 465 Assisting Controllers with the @ControllerAdvice annotation ...................... 465 Asynchronous Request Processing .................................................................. 466 Exception Handling for Async Requests ................................................... 467 Intercepting Async Requests ................................................................... 467 Configuration for Async Request Processing ............................................ 468 Testing Controllers ......................................................................................... 469 16.4. Handler mappings .......................................................................................... 469 Intercepting requests with a HandlerInterceptor ................................................ 469 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation xiv 16.5. Resolving views ............................................................................................. 471 Resolving views with the ViewResolver interface .............................................. 471 Chaining ViewResolvers ................................................................................. 473 Redirecting to views ....................................................................................... 474 RedirectView .......................................................................................... 474 The redirect: prefix ................................................................................. 475 The forward: prefix ................................................................................. 475 ContentNegotiatingViewResolver ..................................................................... 475 16.6. Using flash attributes ..................................................................................... 478 16.7. Building URIs ................................................................................................. 479 16.8. Building URIs to Controllers and methods ....................................................... 480 16.9. Using locales ................................................................................................. 480 Obtaining Time Zone Information .................................................................... 481 AcceptHeaderLocaleResolver .......................................................................... 481 CookieLocaleResolver ..................................................................................... 481 SessionLocaleResolver ................................................................................... 481 LocaleChangeInterceptor ................................................................................ 482 16.10. Using themes ............................................................................................... 482 Overview of themes ........................................................................................ 482 Defining themes ............................................................................................. 482 Theme resolvers ............................................................................................. 483 16.11. Spring’s multipart (file upload) support ........................................................... 483 Introduction .................................................................................................... 483 Using a MultipartResolver with Commons FileUpload ........................................ 484 Using a MultipartResolver with Servlet 3.0 ....................................................... 484 Handling a file upload in a form ...................................................................... 484 Handling a file upload request from programmatic clients .................................. 486 16.12. Handling exceptions ..................................................................................... 486 HandlerExceptionResolver ............

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值