`
kyo100900
  • 浏览: 633821 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【翻译】spring配置全书(上)

阅读更多

作者简介:

 

Craig Walls Texas-based 公司的软件开发人员,有着超过 13 年的开发经验,涉及的领域有通信,金融,零售,教育以及软件业等。他是 Spring Framework 的狂热拥护者,频繁的在当地 local user groups 讨论组和相关会议上演讲 Spring ,并且他的 Blog 上也有很多关于 Spring 的内容。

 

出版的著作有:

l          Spring in Action, 2nd Edition, 2007

l          XDoclet in Action, 2003

 

他的 Blog 是:

l          http://www.springinaction.com

 

所参与的项目:

l          Committer to XDoclet project;

l          Originator of Portlet and Spring modules for XDoclet

 

 

 

本手册主要是将分布于文档中的那些零散的配置文件部分统一成一个比较系统的整体。结合 Spring 文档一起查阅也许能节省你一些时间。不过,并不推荐你全部掌握;很多陌生的元素或标签只应用于特定场合。本手册英文版本可以在: http://java.dzone.com 下载。

 

 

 

 

 

 

 

Spring 配置全书

 

 

关于 Spring 的配置

 

Spring Framework 总是不断的改变着 Java 企业开发的方向,它用一种松耦合的方式来配置和组装应用程序对象和业务对象,比以往的 Java 企业开发来的更加简洁。一旦你开发了基于 Spring 的应用程序,在 Spring 上下文配置的那些资源简直就是唾手可得。

 

 

 

依赖注入是 Spring 容器的核心

 

 

尽管 Spring Framework 可以做很多事,但依赖注入却是 Spring 容器提供的最基本的功能。

 

任何稍微复杂一点的应用程序都至少由两个或两个以上的对象协作在一起,共同完成一些业务逻辑。以往的 Java 企业开发,每个对象都要自己去主动获得他们所引用(或依赖)的对象,才可正常运作。这将导致代码之间的紧耦合,难以测试。

 

 

 

 

 

 

有了依赖注入后,对象所依赖的资源则可通过外部来获得。换句话说,对象所依赖的资源是按照它们的需要给注入进去的。对于基于 Spring 的应用程序来说,是 Spring 容器将这些对象所依赖的资源帮助实现注入依赖的。

 

 

 

 

 

 

 

XML 来配置 Spring

 

 

到了 Spring2.0 Spring 鼓励你使用基于 XML Scheme 的配置方式来应用于你的系统,这比起过去基于 DTD 的方式要更加灵活。一个典型的 Spring2.5 配置文件至少拥有以下结构:

 

 

 

 

 

<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd> 

<!-- place configuration details here -->

</beans>
 

 

 

 

 

Beans 命名空间简介

 

 

Schema URI

www.springframework.org/schema/beans

 

Schema XSD

www.springframework.org/schema/beans/spring-beans-2.5.xsd

 

beans 命名空间是Spring 命名空间的核心,也是你配置Spring 时使用最多的一个。根元素是<beans> ,它不仅可以包含一个或多个<bean> 子元素,而且还可以包含其它命名空间的元素,甚至在<beans> 下你可以不配置任何<bean> 子元素。

 

 

Spring XML 图表的一些约定

 

Spring XML 图通常使用以下符号来表示哪些元素是必选的,可选的以及它们之间的包含关系。

 

 

 

 

 

 

 

 

 

Bean 命名空间下的元素简介

<bean> 元素揭密

 

虽然有很多 XML 元素可以用来配置 Spring context ,但也许你用的最多的可能还是 <bean> 元素。因此,让你深入了解 <bean> 标签是十分必要的。

 

 

 

Bean 命名空间实例

 

下面的 Spring XML 配置文件配置了两个 beans ,其中一个注入到另一个中去:

 

 

<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd”> 

<bean id=”pirate” class=”Pirate”> 
<constructor-arg value=”Long John Silver” /> 
<property name=”map” ref=”treasureMap” /> 
</bean> 

<bean id=”treasureMap” class=”TreasureMap” />

</beans>

 

 

 

第一个 bean ID 为“ pirate ”,类型为“ Pirate ”。它使用了构造函数注入,该构造函数带有一个 String 参数,在这个例子中参数的值为“ Long John Silver ”。另外,它的“ map ”属性引用了另一个叫“ treasureMap ”的 bean ,该 bean TreasureMap 的一个实例。

 

 

 

 

温馨提示:

 

不要把你所有的 beans 都定义在一个 XML 文件中。一旦你的应用程序变得越来越复杂,在 Spring XML 配置文件中定义的 beans 的数量一定让你印象深刻。也没有什么理由要把所有的 beans 都定义在一个 XML 配置文件中去。通过将所有的 beans 分别放在多个 XML 文件中,有助于你的 Spring 配置文件更易于管理。当应用程序上下文 (application context) 建立的时候,可以使用 <import> 元素将它们全部组装起来:

 

 

<import resource=”service-layer-config.xml” /> 
<import resource=”data-layer-config.xml” /> 
<import resource=”transaction-config.xml” />

 

 

 

 

 

 

Context 命名空间简介

 

 

Schema URI

www.springframework.org/schema/context

 

Schema XSD

www.springframework.org/schema/context/spring-context-2.5.xsd

 

 

Spring2.5 中, context 命名空间主要用来提供多种 application context 特定的配置。它包括:支持基于 annotation 的配置方式, JMX 以及领域对象 (domain object) 的注入。

 

 

 

 

 

 

 

Context 命名空间元素简介

 

 

 

 

 

 

Bean 命名空间实例

 

 

下面的 Spring 配置文件使用了 <context:component-scan> 用来自动注册“ com.springinactin.service ”包下的 beans

 

 

<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xmlns:context=”http://www.springframework.org/schema/context” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd”> 

<context:component-scan base-package=”com.springinac¬tion.service” />

</beans>
 

 

 

 

在上面的配置文件中, <context:component-scan> 元素会自动扫描“ com.springinac­tion.service ”包下的类,并自动将那些标记有 @Component, @Controller, @Repository, @Service @Aspect. 的类全部注册到 Spring 容器中。

 

 

 

 

 

温馨提示:

 

尽量为你的最终用户提供外部配置文件。

将所有的配置都定义在 Spring 配置文件中并不推荐。你根本别指望应用程序的管理员或最终用户会去研究 Spring XML 然后搞懂数据库的配置和其它特定布署细节,你不会真打算靠他们吧?相反,使用外部配置文件,我们可以使用 <context:property-placeholder> 这么来做:

 

<context:property-placeholder location=”file:////etc/pirate.properties”
 

 

定义在 /etc/pirate.properties 属性文件中的那些键值对现在可以在 Spring 配置文件中大显身手啦:

 

<bean id=”pirate” class=”Pirate”> 
<constructor-arg value=”${pirate.name}” />
</bean>
 

 

 

 

 

AOP 命名空间简介

 

 

Schema URI

www.springframework.org/schema/aop

 

Schema XSD

www.springframework.org/schema/aop/spring-context-2.5.xsd

 

 

 

aop 命名空间是用来在 Spring context 中声明 aspects, pointcuts advice 的。同样,使用 @Aspectj

annotation 的话,也可以基于 annotation 方式来配置你的 aop 。使用 aspects 的话,可以定义被你多个程序切点使用(或织入)的功能。

 

 

 

 

 

AOP 命名空间元素简介

 

 

 

 

 

 

AOP 命名空间实例

 

下面的 Spring 配置文件使用了 aop 命名空间来建一个 aspect

 

 

<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xmlns:aop=”http://www.springframework.org/schema/aop” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”> 
<bean id=”pirateTalker” class=”PirateTalker” /> 
	<aop:config> 
	<aop:pointcut id=”plunderPointcut” expression=”execution(* *.plunder(..))” /> 
	<aop:aspect ref=”pirateTalker”> 
	<aop:before pointcut-ref=”plunderPointcut” method=”sayAvastMeHearties” /> 
	<aop:after-returning pointcut-ref=”plunderPointcut” method=”sayYarr” /> 
</aop:aspect> 
</aop:config>
</beans>
 

 

 

 

 

aspect 由一个 pointcut 和两个 advice 组成。该 pointcut 用来定义所有对象 plunder() 方法的执行。标有 <asp:before> advice 用来配置当 plunder() 执行时,立即调用 pirateTalker bean sayAvastMeHearties() 方法;类似的,当 plunder() 方法调用成功时, sayYarr() 方法同样也会触发。

 

 

 

 

 

JEE 命名空间简介

 

 

Schema URI

www.springframework.org/schema/jee

 

Schema XSD

www.springframework.org/schema/jee/spring-context-2.5.xsd

 

 

 

 

 

JEE 命名空间元素简介

 

 

 

 

 

 

JEE 命名空间实例

 

下面的 Spring 配置文件使用了部分 jee 命名空间元素,用来获取 Spring 容器外的对象,并且将这些对象作为 Spring beans

 

 

<?xml version=”1.0” encoding=”UTF-8”?>

<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xmlns:jee=”http://www.springframework.org/schema/jee” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd”> 

<jee:remote-slsb id=”hispaniola” jndi-name=”ejb/PirateShip” business-interface=”com.pirate.PirateShipEjb”  resource-ref=”true” />
<jee:jndi-lookup id=”parrot” jndi-name=”pirate/Parrot “ resource-ref=”false” />

</beans>
 

 

 

第一个元素 <jee:remote-slsb> 配置了一个叫“ Hispaniola ”的 bean ,它实际上引用的是一个 EJB2 remote stateless session bean 。这里 EJB home interface 可以通过 JNDI 名称“ java:comp/env/ejb/PirateShip ”找到。属性“ resource-ref ”表示应该值会自动加上“ java:comp/env/ ”前缀。 EJB 的实现方法定义在 PirateShipEjb 业务接口中。

 

 

另一个元素 <jee:jndi-lookup> 用于从 JNDI 获取对象的引用(它可以是一个 EJB3 session bean 或一个普通 pojo )。对象会在这个叫“ pirate/Parrot ”的 JNDI 中获得。注意这里我们将“ resource-ref ”属性配置为“ false ”,所以应该 jndi-name 不会加上“ java:comp/env ”前缀。

 

25
7
分享到:
评论
14 楼 williamtwo 2011-12-15  
     
13 楼 daoyongyu 2008-12-04  
楼主辛苦了,谢谢,向楼主学习!
12 楼 lovefly_zero 2008-07-18  
很感谢楼主的辛勤劳动
11 楼 sidubi 2008-07-17  
好东西,收藏啦
10 楼 JerryWan 2008-07-16  
   正好用的着~ 学习了·  楼主辛苦了····
9 楼 wm920 2008-07-10  
精华帖子··期待下的
8 楼 linliangyi2007 2008-07-10  
期待下集出现
7 楼 sunnylovewindy 2008-07-10  
楼主能传一些用Annotation代替xml的简易例子吗?谢谢
6 楼 lijie250 2008-07-09  
我只用到了IOC了,其余的作用我都不是很清楚!
5 楼 yanbingwei 2008-07-09  
good
4 楼 itsynclbw 2008-07-09  
还不错,学习啦
3 楼 zgqynx 2008-07-09  
支持你的翻译!
2 楼 robustwang 2008-07-09  
不错
1 楼 hantsy 2008-07-08  
refcard!=book

相关推荐

    【翻译】spring配置全书(下)——附PDF完整版下载

    spring配置全书PDF完整版 博文链接:https://superleo.iteye.com/blog/214535

    详细的Spring配置和Spring Boot-外文翻译

    毕业设计中的外文翻译,可以直接使用,借鉴

    Spring Security-3.0.1中文官方文档(翻译版)

    Spring Security-3.0.1 中文官方文档(翻译版) 这次发布的Spring Security-3.0.1 是一个bug fix 版,主要是对3.0 中存在的一些问题进 行修 正。文档中没有添加新功能的介绍,但是将之前拼写错误的一些类名进行...

    thymeleaf-spring4-3.0.3.RELEASE-API文档-中文版.zip

    包含翻译后的API文档:thymeleaf-spring4-3.0.3.RELEASE-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:org.thymeleaf,artifactId:thymeleaf-spring4,version:3.0.3.RELEASE 使用方法:解压翻译后...

    spring-boot中文教程

    Spring Boot中文文档提供了详细的中文翻译和说明,包括了基本概念、使用方法、配置参数、代码示例等,可以帮助用户快速上手和深入理解Spring Boot的使用。该文档还提供了丰富的链接和参考资料,方便用户深入学习和...

    spring-websocket-5.0.8.RELEASE-API文档-中英对照版.zip

    包含翻译后的API文档:spring-websocket-5.0.8.RELEASE-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.springframework:spring-websocket:5.0.8.RELEASE; 标签:springframework、spring、...

    spring.net中文手册在线版

    Spring.NET以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。 第一章 序言 第二章 简介 2.1.概述 2.2.背景 2.3.模块 2.4.许可证信息 2.5.支持 第三章 背景 3.1.控制反转 第...

    spring-aop-5.1.3.RELEASE-API文档-中英对照版.zip

    包含翻译后的API文档:spring-aop-5.1.3.RELEASE-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.springframework:spring-aop:5.1.3.RELEASE; 标签:springframework、spring、aop、中英对照文档、...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 ...

    Spring 2.0 开发参考手册

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    SpringCloud教程

    史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config) 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus) 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪...

    Spring_Security-3.0.1文档(翻译版

    一种是全部利用配置文件,将用户、权限、资源(url)硬编码在xml文件中。 二种是用户和权限用数据库存储,而资源(url)和权限的对应采用硬编码配置。 三种是细分角色和权限,并将用户、角色、权限和资源均采用数据库...

    Spring-Boot中application配置文件的所有属性(包括官网的原版以及翻译后的中文版)

    Spring-Boot中application配置文件的所有属性,对我们平时开发搭建Spring Boot项目时有很大的帮助,基本上我们平时可以用到的,里面都有哦 。。。

    【Spring整合Mybatis配置及测试代码】

    MyBatis-Spring 会帮助你... 而且它也会处理事务, 翻译 MyBatis 的异常到 Spring 的 DataAccessException 异常(数据访问异常,译者注)中。最终,它并 不会依赖于 MyBatis,Spring 或 MyBatis-Spring 来构建应用程序代码。

    spring chm文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    计算机相关专业本科生毕业设计标准外文翻译一篇(外文+翻译)

    计算机相关专业本科生毕业设计标准外文翻译一篇(外文+翻译)

    3.Spring Cloud服务管理框架Eureka简单示例

    Spring Cloud为开发人员提供了工具,可以快速构建分布式系统中的一些常见模式(譬如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领袖选举(字面翻译)、分布式会话、集群状态)。...

Global site tag (gtag.js) - Google Analytics