spring 框架簡介外文翻譯_第1頁
已閱讀1頁,還剩26頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、<p>  xxxx大學(xué)xxx學(xué)院</p><p>  畢業(yè)設(shè)計(jì)(論文)外文文獻(xiàn)翻譯</p><p><b>  2013年 3 月</b></p><p> 系 部xxxx</p><p> 專 業(yè)xxxx</p><p> 學(xué)生姓名xxxx學(xué)號(hào)xxxx</p>

2、<p> 指導(dǎo)教師xxxx職稱xxxx</p><p>  Introducing the Spring Framework</p><p>  The Spring Framework: a popular open source application framework that addresses many of the issues outlined in this

3、 book. This chapter will introduce the basic ideas of Spring and dis-cuss the central “bean factory” lightweight Inversion-of-Control (IoC) container in detail.</p><p>  Spring makes it particularly easy to

4、implement lightweight, yet extensible, J2EE archi-tectures. It provides an out-of-the-box implementation of the fundamental architectural building blocks we recommend. Spring provides a consistent way of structuring your

5、 applications, and provides numerous middle tier features that can make J2EE development significantly easier and more flexible than in traditional approaches.</p><p>  The basic motivations for Spring are:&

6、lt;/p><p>  To address areas not well served by other frameworks. There are numerous good solutions to specific areas of J2EE infrastructure: web frameworks, persistence solutions, remoting tools, and so on. Ho

7、wever, integrating these tools into a comprehensive architecture can involve significant effort, and can become a burden. Spring aims to provide an end-to-end solution, integrating spe-cialized frameworks into a coherent

8、 overall infrastructure. Spring also addresses some areas that other frameworks do</p><p>  To allow for easy adoption. A framework should be cleanly layered, allowing the use of indi-vidual features without

9、 imposing a whole world view on the application. Many Spring features, such as the JDBC abstraction layer or Hibernate integration, can be used in a library style or as part of the Spring end-to-end solution. </p>

10、<p>  To deliver ease of use. As we’ve noted, J2EE out of the box is relatively hard to use to solve many common problems. A good infrastructure framework should make simple tasks simple to achieve, without forcing

11、 tradeoffs for future complex requirements (like distributed transactions) on the application developer. It should allow developers to leverage J2EE services such as JTA where appropriate, but to avoid dependence on them

12、 in cases when they are unnecessarily complex. </p><p>  To make it easier to apply best practices. Spring aims to reduce the cost of adhering to best practices such as programming to interfaces, rather than

13、 classes, almost to zero. However, it leaves the choice of architectural style to the developer. </p><p>  Non-invasiveness. Application objects should have minimal dependence on the framework. If leveraging

14、 a specific Spring feature, an object should depend only on that particular feature, whether by implementing a callback interface or using the framework as a class library. IoC and AOP are the key enabling technologies f

15、or avoiding framework dependence. </p><p>  Consistent configuration. A good infrastructure framework should keep application configuration flexible and consistent, avoiding the need for custom singletons an

16、d factories. A single style should be applicable to all configuration needs, from the middle tier to web controllers. </p><p>  Ease of testing. Testing either whole applications or individual application cl

17、asses in unit tests should be as easy as possible. Replacing resources or application objects with mock objects should be straightforward. </p><p>  To allow for extensibility. Because Spring is itself based

18、 on interfaces, rather than classes, it is easy to extend or customize it. Many Spring components use strategy interfaces, allowing easy customization. </p><p>  A Layered Application Framework</p>&l

19、t;p>  Chapter 6 introduced the Spring Framework as a lightweight container, competing with IoC containers such as PicoContainer. While the Spring lightweight container for JavaBeans is a core concept, this is just the

20、 foundation for a solution for all middleware layers.</p><p>  Basic Building Blocks</p><p>  pring is a full-featured application framework that can be leveraged at many levels. It consists of

21、multi-ple sub-frameworks that are fairly independent but still integrate closely into a one-stop shop, if desired. The key areas are:</p><p>  Bean factory. The Spring lightweight IoC container, capable of c

22、onfiguring and wiring up Java-Beans and most plain Java objects, removing the need for custom singletons and ad hoc configura-tion. Various out-of-the-box implementations include an XML-based bean factory. The lightweigh

23、t IoC container and its Dependency Injection capabilities will be the main focus of this chapter. </p><p>  Application context. A Spring application context extends the bean factory concept by adding suppor

24、t for message sources and resource loading, and providing hooks into existing environ-ments. Various out-of-the-box implementations include standalone application contexts and an XML-based web application context. </p

25、><p>  AOP framework. The Spring AOP framework provides AOP support for method interception on any class managed by a Spring lightweight container. It supports easy proxying of beans in a bean factory, seamless

26、ly weaving in interceptors and other advice at runtime. Chapter 8 dis-cusses the Spring AOP framework in detail. The main use of the Spring AOP framework is to provide declarative enterprise services for POJOs. </p>

27、;<p>  Auto-proxying. Spring provides a higher level of abstraction over the AOP framework and low-level services, which offers similar ease-of-use to .NET within a J2EE context. In particular, the provision of de

28、clarative enterprise services can be driven by source-level metadata. </p><p>  Transaction management. Spring provides a generic transaction management infrastructure, with pluggable transaction strategies

29、(such as JTA and JDBC) and various means for demarcat-ing transactions in applications. Chapter 9 discusses its rationale and the power and flexibility that it offers. </p><p>  DAO abstraction. Spring defin

30、es a set of generic data access exceptions that can be used for cre-ating generic DAO interfaces that throw meaningful exceptions independent of the underlying persistence mechanism. Chapter 10 illustrates the Spring sup

31、port for DAOs in more detail, examining JDBC, JDO, and Hibernate as implementation strategies. </p><p>  JDBC support. Spring offers two levels of JDBC abstraction that significantly ease the effort of writi

32、ng JDBC-based DAOs: the org.springframework.jdbc.core package (a template/ </p><p>  callback approach) and the org.springframework.jdbc.object package (modeling RDBMS operations as reusable objects). Using

33、the Spring JDBC packages can deliver much greater pro-ductivity and eliminate the potential for common errors such as leaked connections, compared with direct use of JDBC. The Spring JDBC abstraction integrates with the

34、transaction and DAO abstractions. </p><p>  Integration with O/R mapping tools. Spring provides support classes for O/R Mapping tools like Hibernate, JDO, and iBATIS Database Layer to simplify resource setup

35、, acquisition, and release, and to integrate with the overall transaction and DAO abstractions. These integration packages allow applications to dispense with custom ThreadLocal sessions and native transac-tion handling,

36、 regardless of the underlying O/R mapping approach they work with. </p><p>  Web MVC framework. Spring provides a clean implementation of web MVC, consistent with the JavaBean configuration approach. The Spr

37、ing web framework enables web controllers to be configured within an IoC container, eliminating the need to write any custom code to access business layer services. It provides a generic DispatcherServlet and out-of-the-

38、box controller classes for command and form handling. Request-to-controller mapping, view resolution, locale resolution and other important services a</p><p>  Remoting support. Spring provides a thin abstra

39、ction layer for accessing remote services without hard-coded lookups, and for exposing Spring-managed application beans as remote services. Out-of-the-box support is included for RMI, Caucho’s Hessian and Burlap web serv

40、ice protocols, and WSDL Web Services via JAX-RPC. Chapter 11 discusses lightweight remoting.</p><p>  While Spring addresses areas as diverse as transaction management and web MVC, it uses a consistent appro

41、ach everywhere. Once you have learned the basic configuration style, you will be able to apply it in many areas. Resources, middle tier objects, and web components are all set up using the same bean configuration mechani

42、sm. You can combine your entire configuration in one single bean definition file or split it by application modules or layers; the choice is up to you as the application devel</p><p>  Spring on J2EE</p&g

43、t;<p>  Although many parts of Spring can be used in any kind of Java environment, it is primarily a J2EE application framework. For example, there are convenience classes for linking JNDI resources into a bean fa

44、ctory, such as JDBC DataSources and EJBs, and integration with JTA for distributed transaction management. In most cases, application objects do not need to work with J2EE APIs directly, improving reusability and meaning

45、 that there is no need to write verbose, hard-to-test, JNDI lookups.</p><p>  Thus Spring allows application code to seamlessly integrate into a J2EE environment without being unnecessarily tied to it. You c

46、an build upon J2EE services where it makes sense for your application, and choose lighter-weight solutions if there are no complex requirements. For example, you need to use JTA as transaction strategy only if you face d

47、istributed transaction requirements. For a single database, there are alternative strategies that do not depend on a J2EE container. Switching between </p><p>  Spring offers support for accessing EJBs. This

48、 is an important feature (and relevant even in a book on “J2EE without EJB”) because the use of dynamic proxies as codeless client-side business delegates means that Spring can make using a local stateless session EJB an

49、 implementation-level, rather than a fundamen-tal architectural, choice. Thus if you want to use EJB, you can within a consistent architecture; however, you do not need to make EJB the cornerstone of your architecture. T

50、his Spring fea</p><p>  Spring also provides support for implementing EJBs, in the form of convenience superclasses for EJB implementation classes, which load a Spring lightweight container based on an envir

51、onment variable specified in the ejb-jar.xml deployment descriptor. This is a powerful and convenient way of imple-menting SLSBs or MDBs that are facades for fine-grained POJOs: a best practice if you do choose to implem

52、ent an EJB application. Using this Spring feature does not conflict with EJB in any way—it merely</p><p>  Introducing the Spring Framework</p><p>  The main aim of Spring is to make J2EE easier

53、 to use and promote good programming practice. It does not reinvent the wheel; thus you’ll find no logging packages in Spring, no connection pools, no distributed transaction coordinator. All these features are provided

54、by other open source projects—such as Jakarta Commons Logging (which Spring uses for all its log output), Jakarta Commons DBCP (which can be used as local DataSource), and ObjectWeb JOTM (which can be used as transaction

55、 manager)—or by</p><p>  Spring does aim to make existing technologies easier to use. For example, although Spring is not in the business of low-level transaction coordination, it does provide an abstraction

56、 layer over JTA or any other transaction strategy. Spring is also popular as middle tier infrastructure for Hibernate, because it provides solutions to many common issues like SessionFactory setup, ThreadLocal sessions,

57、and exception handling. With the Spring HibernateTemplate class, implementation methods of Hiberna</p><p>  The Spring Framework does not aim to replace J2EE middle tier services as a whole. It is an applica

58、tion framework that makes accessing low-level J2EE container ser-vices easier. Furthermore, it offers lightweight alternatives for certain J2EE services in some scenarios, such as a JDBC-based transaction strategy instea

59、d of JTA when just working with a single database. Essentially, Spring enables you to write appli-cations that scale down as well as up.</p><p>  Spring for Web Applications</p><p>  A typical u

60、sage of Spring in a J2EE environment is to serve as backbone for the logical middle tier of a J2EE web application. Spring provides a web application context concept, a powerful lightweight IoC container that seamlessly

61、adapts to a web environment: It can be accessed from any kind of web tier, whether Struts, WebWork, Tapestry, JSF, Spring web MVC, or a custom solution.</p><p>  The following code shows a typical example of

62、 such a web application context. In a typical Spring web app, an applicationContext.xml file will reside in the WEB-INF directory, containing bean defini-tions according to the “spring-beans” DTD. In such a bean definiti

63、on XML file, business objects and resources are defined, for example, a “myDataSource” bean, a “myInventoryManager” bean, and a “myProductManager” bean. Spring takes care of their configuration, their wiring up, and thei

64、r lifecycle.</p><p><b>  <beans></b></p><p>  <bean id=”myDataSource” class=”org.springframework.jdbc. datasource.DriverManagerDataSource”></p><p>  <prop

65、erty name=”driverClassName”> <value>com.mysql.jdbc.</p><p>  Driver</value></p><p>  </property> <property name=”url”></p><p>  <value>jdbc:mysql:my

66、ds</value></p><p>  </property></p><p>  </bean><bean id=”myInventoryManager” class=”ebusiness.</p><p>  DefaultInventoryManager”> <property name=”dataSou

67、rce”></p><p>  <ref bean=”myDataSource”/> </property></bean></p><p>  <bean id=”myProductManager” class=”ebusiness.</p><p>  DefaultProductManager”></p

68、><p>  <property name=”inventoryManager”></p><p>  <ref bean=”myInventoryManager”/> </property></p><p>  <property name=”retrieveCurrentStock”> <value>tru

69、e</value></p><p>  </property></p><p><b>  </bean></b></p><p><b>  </beans></b></p><p>  By default, all such beans have

70、 “singleton” scope: one instance per context. The “myInventoryManager” bean will automatically be wired up with the defined DataSource, while “myProductManager” will in turn receive a reference to the “myInventoryManager

71、” bean. Those objects (traditionally called “beans” in Spring terminology) need to expose only the corresponding bean properties or constructor arguments (as you’ll see later in this chapter); they do not have to perform

72、 any custom lookups.</p><p>  A root web application context will be loaded by a ContextLoaderListener that is defined in web.xml as follows:</p><p><b>  <web-app></b></p>

73、;<p>  <listener> <listener-class></p><p>  org.springframework.web.context.ContextLoaderListener </listener-class></p><p>  </listener></p><p><b&

74、gt;  ...</b></p><p>  </web-app></p><p>  After initialization of the web app, the root web application context will be available as a ServletContext attribute to the whole web appli

75、cation, in the usual manner. It can be retrieved from there easily via fetching the corresponding attribute, or via a convenience method in org.springframework.web. context.support.WebApplicationContextUtils. This means

76、that the application context will be available in any web resource with access to the ServletContext, like a Servlet, Filter, JSP, or Struts</p><p>  WebApplicationContext wac = WebApplicationContextUtils.&l

77、t;/p><p>  getWebApplicationContext(servletContext);</p><p>  The Spring web MVC framework allows web controllers to be defined as JavaBeans in child application contexts, one per dispatcher servle

78、t. Such controllers can express dependencies on beans in the root application context via simple bean references. Therefore, typical Spring web MVC applications never need to perform a manual lookup of an application con

79、text or bean factory, or do any other form of lookup.</p><p>  Neither do other client objects that are managed by an application context themselves: They can receive collaborating objects as bean references

80、.</p><p>  The Core Bean Factory</p><p>  In the previous section, we have seen a typical usage of the Spring IoC container in a web environment: The provided convenience classes allow for seaml

81、ess integration without having to worry about low-level container details. Nevertheless, it does help to look at the inner workings to understand how Spring manages the container. Therefore, we will now look at the Sprin

82、g bean container in more detail, starting at the lowest building block: the bean factory. Later, we’ll continue with resource se</p><p>  One of the main incentives for a lightweight container is to dispense

83、 with the multitude of custom facto-ries and singletons often found in J2EE applications. The Spring bean factory provides one consistent way to set up any number of application objects, whether coarse-grained components

84、 or fine-grained busi-ness objects. Applying reflection and Dependency Injection, the bean factory can host components that do not need to be aware of Spring at all. Hence we call Spring a non-invasive applicati</p>

85、;<p>  Fundamental Interfaces</p><p>  The fundamental lightweight container interface is org.springframework.beans.factory.Bean Factory. This is a simple interface, which is easy to implement directl

86、y in the unlikely case that none of the implementations provided with Spring suffices. The BeanFactory interface offers two getBean() methods for looking up bean instances by String name, with the option to check for a r

87、equired type (and throw an exception if there is a type mismatch).</p><p>  public interface BeanFactory {</p><p>  Object getBean(String name) throws BeansException;</p><p>  Objec

88、t getBean(String name, Class requiredType) throws BeansException;</p><p>  boolean containsBean(String name);</p><p>  boolean isSingleton(String name) throws NoSuchBeanDefinitionException;</

89、p><p>  String[] getAliases(String name) throws NoSuchBeanDefinitionException;</p><p><b>  }</b></p><p>  The isSingleton() method allows calling code to check whether the

90、specified name represents a sin-gleton or prototype bean definition. In the case of a singleton bean, all calls to the getBean() method will return the same object instance. In the case of a prototype bean, each call to

91、getBean() returns an inde-pendent object instance, configured identically.</p><p>  The getAliases() method will return alias names defined for the given bean name, if any. This mecha-nism is used to provide

92、 more descriptive alternative names for beans than are permitted in certain bean factory storage representations, such as XML id attributes.</p><p>  The methods in most BeanFactory implementations are aware

93、 of a hierarchy that the implementation may be part of. If a bean is not found in the current factory, the parent factory will be asked, up until the root factory. From the point of view of a caller, all factories in suc

94、h a hierarchy will appear to be merged into one. Bean definitions in ancestor contexts are visible to descendant contexts, but not the reverse.</p><p>  All exceptions thrown by the BeanFactory interface and

95、 sub-interfaces extend org.springframework. beans.BeansException, and are unchecked. This reflects the fact that low-level configuration prob-lems are not usually recoverable: Hence, application developers can choose to

96、write code to recover from such failures if they wish to, but should not be forced to write code in the majority of cases where config-uration failure is fatal.</p><p>  Most implementations of the BeanFacto

97、ry interface do not merely provide a registry of objects by name; they provide rich support for configuring those objects using IoC. For example, they manage dependen-cies between managed objects, as well as simple prope

98、rties. In the next section, we’ll look at how such configuration can be expressed in a simple and intuitive XML structure.</p><p>  The sub-interface org.springframework.beans.factory.ListableBeanFactory sup

99、ports listing beans in a factory. It provides methods to retrieve the number of beans defined, the names of all beans, and the names of beans that are instances of a given type:</p><p>  public interface Lis

100、tableBeanFactory extends BeanFactory {</p><p>  int getBeanDefinitionCount();</p><p>  String[] getBeanDefinitionNames();</p><p>  String[] getBeanDefinitionNames(Class type);</p

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論