24
1 Spring 2.0 Gautam

Spring 2.0

Embed Size (px)

Citation preview

Page 1: Spring 2.0

1

Spring 2.0

Gautam

Page 2: Spring 2.0

2

Spring 2.0

• Simplified, extensible XML schema based configuration.• New JSP form tags.• Powerful new Spring AOP features and AspectJ 5

integration, also leveraging new XML configuration.• Asynchronous JMS facilities enabling message driven

POJOs.• JPA (Java Persistence API) Integration.• Spring Portlet MVC.• Full backwards compatibility.

Page 3: Spring 2.0

3

Spring 2.0 : XSD-driven Configuration

Spring 1.2.x:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

or<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

Spring 2.0:

<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.0.xsd”>

Page 4: Spring 2.0

4

Spring 2.0 : XSD-driven Configuration

• XML schema based configuration file makes Spring XML configuration easier.

Namespaces• More concise configuration.• Better validation and code-completion in IDE.• Extensible for higher level domain-specific configurations.Out-of-the-box namespaces: • Special namespace- property (“p”)• Utils (“util”)• Java EE(“jee”)• AOP(“aop”)• Transactions management(“tx”)• Dynamic Languages(“lang”)

Page 5: Spring 2.0

5

Spring 2.0 : Namespace examples-property(“p”)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation= “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

• Spring 1.2

<bean id="salesRepController" class="isis.oe.web.controllers.logon.SalesRepController"> <property name="formView"><value>login_repnum_entry</value></property> <property name="successView"><value>redirect:search.html</value></property></bean>

• Spring 2 .0

<bean id="salesRepController" class="isis.oe.web.controllers.logon.SalesRepController" p:formView="login_repnum_entry" p:successView="redirect:search.html" />

Page 6: Spring 2.0

6

Spring 2.0 : Namespace examples-property(“p”)

• Spring 1.2

<bean id="searchController" class="isis.oe.web.controllers.search.SearchController">

<property name="validator"><ref bean="searchParamsValidator"/></property>

<property name="formView"><value>search</value></property>

</bean>

<bean id="searchParamsValidator" class="isis.oe.web.validator.SearchParamsValidator"/>

• Spring 2 .0

<bean id="searchController" class="isis.oe.web.controllers.search.SearchController" p:validator-ref="searchParamsValidator" p:formView="search" />

<bean id="searchParamsValidator" class="isis.oe.web.validator.SearchParamsValidator"/>

Page 7: Spring 2.0

7

Spring 2.0 : Namespace examples-JNDI(“jee”)

<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.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

• Spring 1.2

<bean id="jaznDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"><property name="jndiName" value="java:comp/env/jdbc/useradmin"/><property name="resourceRef"><value>false</value></property>

</bean>

(JndiObjectFactoryBean is Spring internal implementation class which is unnecessary for end user)

• Spring 2 .0

<jee:jndi-lookup id="jaznDataSource" jndi-name="java:comp/env/jdbc/useradmin" resource-ref="false" />

Page 8: Spring 2.0

8

Spring 2.0 : Namespace examples-JNDI(“jee”)

• Spring 1.2

<bean id = "simple" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">

<property name="jndiName" value="ejb/RentalServiceBean"/>

<property name="businessInterface" value="com.foo.service.RentalService"/>

</bean>

(LocalStatelessSessionProxyFactoryBean is Spring internal implementation class which is unnecessary for end user)

• Spring 2 .0

<jee:local-slsb id="simpleSlsb" jndi-name = "ejb/RentalServiceBean" business- interface="com.foo.service.RentalService"/>

Page 9: Spring 2.0

9

Spring 2.0 : Namespace examples- util - Properties

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation= "http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-2.0.xsd">

• Spring 1.2

<bean id="loadProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">

<property name="location" value="classpath:message.properties"/></bean>

• Spring 2 .0<util:properties id="loadProperties" location="classpath: message.properties"/>

Page 10: Spring 2.0

10

• Spring 1.2

<bean id=“constant" class=“com.iusa.util.UtilTest">

<property name="isolation">

<bean id="java.sql.Connection.TRANSACTION_REPEATABLE_READ"

class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />

</property>

</bean>

• Spring 2 .0

<bean id=" constant " class=" com.iusa.util.UtilTest">

<property name="isolation">

<util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>

</property>

</bean>

Spring 2.0 : Namespace examples- util - Constant

Page 11: Spring 2.0

11

Spring 2.0 : Namespace examples- util - List

• Spring 1.2 <bean id="emailList" class="org.springframework.beans.factory.config.ListFactoryBean">

<property name="sourceList">

<list>

<value>gautamr@ infousa.com</value>

<value>[email protected]</value>

</list>

</property>

</bean>

• Spring 2.0<util:list id=" emailList ">

<value> gautamr @ gautamr. com </value>

<value> [email protected] </value>

</util:list>

Page 12: Spring 2.0

12

Spring 2.0 : Namespace examples- util - Map

• Spring 1.2 <bean id="emailMap" class="org.springframework.beans.factory.config.MapFactoryBean">

<property name="sourceMap">

<map>

<entry key="gautamr" value="gautamr@ infousa.com"/>

<entry key=" abc" value=“[email protected]"/>

</map>

</property>

</bean>

• Spring 2.0<util:map id="emailMap">

<entry key="gautamr" value="gautamr@ infousa.com"/>

<entry key=" abc" value=“[email protected]"/>

</util: map >

Page 13: Spring 2.0

13

Spring 2.0 : Scopes

• Spring 1.2• Singleton & Prototype• Ex: <!-- spring-beans-2.0.dtd -->

<bean id="id" class="com.foo.TestScope" scope="singleton"/><!-- spring-beans.dtd -->

<bean id="id" class="com.foo.TestScope" singleton="true"/>

• Spring 2.0• Singleton, Prototype, Request, Session, GlobalSession,

UserDefined scope• Ex:

<bean id="id" class="com.foo.TestScope" scope=“request"/>

Page 14: Spring 2.0

14

Spring 2.0 : Scopes

Scope Descriptionsingleton Scopes a single bean definition to a single

object instance per Spring IoC container.

prototype Scopes a single bean definition to any number of object instances.

request Scopes a single bean definition to the lifecycle of a single HTTP request; i.e. each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring

ApplicationContext.

session Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring

ApplicationContext.

Page 15: Spring 2.0

15

Spring 2.0 : Scopes

Global Session Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

Page 16: Spring 2.0

16

Spring 2.0 : Scopes – request & session

• Minor initial configuration is required before you can set about defining definitions request and session

• If you are using a Servlet 2.4+ web container, add the following ContextListener to the XML declarations in your web application's 'web.xml' file.

<web-app><listener>

<listener-class>org.springframework.web.context.request.RequestContextListener.

</listener-class></listener></web-app>

• If you are using an older web container (before Servlet 2.4), add the following to ‘web.xml’<filter>

<filter-name>requestContextFilter</filter-name><filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>

</filter><filter-mapping>

<filter-name>requestContextFilter</filter-name><url-pattern>/*</url-pattern>

</filter-mapping>

Page 17: Spring 2.0

17

Spring 2.0 : Scopes – Custom scope

• As of Spring 2.0, the bean scoping mechanism in Spring is extensible, that is you can define your own scopes.

• Steps to create your own custom scope• Implement org.springframework.beans.factory.config.Scope

interface• Make the Spring container aware of your new scope(s).

Scope myScope = new MyScope();beanFactory.registerScope(“scopename", myScope);

• You can then create bean definitions that adhere to the scoping rules of your custom Scope<bean id="..." class="..." scope="scopename"/>

Page 18: Spring 2.0

18

Spring 2.0 : Form tag

• As of version 2.0, Spring provides a comprehensive set of data binding-aware tags for handling form elements when using JSP and Spring Web MVC

• Similar to the Struts <html:xxxx> tags.

• Spring's form tag library is integrated with Spring Web MVC, giving the

tags access to the command object and reference data your controller deals with.

• To use the tags from this library, add the following directive to the top of your JSP page:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

• List of form tags:

<form:checkbox />, <form:errors />, <form:form />, <form:hidden />, <form:input />, <form:label /> <form:option />, <form:options />, <form:password />, <form:radiobutton />, <form:select />, <form:textarea />,

Page 19: Spring 2.0

19

Spring 2.0 : Other features - Namespaces

• Dynamic Language support(“lang”)• AOP(“aop”)• Transactions management(“tx”)Ex: <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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/lang

http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">

Page 20: Spring 2.0

20

Spring 2.0 : Other features - Dynamic Language support(“lang”)

• Spring 2.0 introduces comprehensive support for using classes and objects

that have been defined using a dynamic language• The dynamic languages currently supported are:

• JRuby• Groovy• BeanShell

Ex:• <!-- this is the bean definition for the Groovy-backed Messenger implementation -->

<lang:groovy id="messenger" script-source="classpath:Messenger.groovy">

<lang:property name="message" value=“Hello Groovy" />

</lang:groovy>

Page 21: Spring 2.0

21

Spring 2.0 : Other features

• Powerful new Spring AOP features and AspectJ 5 integration, also leveraging new XML configuration

• Asynchronous JMS facilities enabling Message Driven POJOs(MDP)

• Prior to Spring 2.0, Spring's JMS offering was limited to sending messages and the synchronous receiving of messages. Spring 2.0 now ships with full support for the reception of messages in an asynchronous fashion using MDP

• JPA (Java Persistence API) Integration

• Spring Portlet MVC

Page 22: Spring 2.0

22

Spring 2.0 : Deprecated classes and methods

• Previously were marked as @deprecated classed and methods have been removed from the Spring 2.0 codebase.

• The following classes/interfaces have been removed from the Spring 2.0 codebase:

• ResultReader : Use the RowMapper interface instead.

• BeanFactoryBootstrap : Consider using a BeanFactoryLocator

Page 23: Spring 2.0

23

Spring 2.0 : 1.2 to 2.0 migration steps

• Download latest spring.jar from http://www.springframework.org/download

(Spring Framework 2.0.2 is the current production release )

Note:

Jar packaging:

The packaging of the Spring Framework jars has changed quite substantially

between the 1.2.x and 2.0 releases. In particular, there are now dedicated jars for

the JDO, Hibernate 2/3, TopLink ORM integration classes: they are no longer

bundled in the core 'spring.jar' file anymore.

Page 24: Spring 2.0

24

Spring 2.0 : Resources

• http://static.springframework.org/spring/docs/2.0.x/reference/index.html

• http://www.infoq.com/articles/spring-2-intro by Rod Johnson (Creater of spring)

• http://www.infoq.com/articles/Simplifying-Enterprise-Apps