spring 2.5
DESCRIPTION
Introduction to Spring 2.5 for developers not familiar with Spring.TRANSCRIPT
![Page 1: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/1.jpg)
The Spring FrameworkPaul Bakker
![Page 2: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/2.jpg)
Goals
An introduction
Not pointing a winner
![Page 3: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/3.jpg)
Outline
• History and overview
• Dependency Injection
• AOP
• Module deep dive
• Testing
![Page 4: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/4.jpg)
History
Founded by Rod Johnson
First introduced in his book “J2EE Design & Development”
![Page 5: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/5.jpg)
Modules
![Page 6: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/6.jpg)
Why Dependency Injection?
H o w w o u l d y o u t e s t t h i s m e t h o d ?
publicdoublecalculateTotalPrice(){ProductServiceproductService=newProductService();Listproducts=productService.getProducts();
//iterateproductsandsumprices
returntotal;}
![Page 7: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/7.jpg)
productService.getProducts()
Database
![Page 8: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/8.jpg)
publicdoublecalculateTotalPrice(){ProductServiceproductService=newProductService();Listproducts=productService.getProducts();
//iterateproductsandsumprices
returntotal;}
publicvoidtestCalculateTotalPrice(){Calculatorcalc=newCalculator();assertEquals(150,calc.calculateTotalPrice());
}
Throws Exception...
![Page 9: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/9.jpg)
publicCalculator(ProductServiceproductService){this.productService=productService;
}
publicdoublecalculateTotalPrice(){Listproducts=productService.getProducts();
//iterateproductsandsumprices
returntotal;}
![Page 10: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/10.jpg)
publicvoidtestCalculateTotalPrice(){Calculatorcalc=newCalculator(newProductServiceMock());
assertEquals(150,calc.calculateTotalPrice());}
![Page 11: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/11.jpg)
Program to interfaces
not to implementations
![Page 12: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/12.jpg)
Inversion of Control
A container manages dependencies
ProductService Calculator
IoC Container
![Page 13: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/13.jpg)
Configuring a container
ProductServicepService=newProductServiceImpl();Calculatercalc=newCalculator(pService);
//initotherinstances
This will be a lot of work however...
![Page 14: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/14.jpg)
Configuration using XML
<beanid="productService"class="services.ProductServiceImpl"/>
<beanid="calculator"class="calc.Calculator"><constructor‐argindex="0"ref="productService"/></bean>
Spring version 1 way
![Page 15: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/15.jpg)
Configuration using annotations
@Stateless(name="ProductEJB")publicclassProductBean{publicProductBean(){}}
JEE 5 way
@EJBShoppingBasketBeanshoppingBasket;
![Page 16: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/16.jpg)
Spring annotations
• Spring can now be configured using annotations
• Use annotations for component declarations
• Use XML for container wide configuration
![Page 17: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/17.jpg)
Annotation example
@ServicepublicclassSimpleMovieLister{privateMovieFindermovieFinder;@AutowiredpublicSimpleMovieLister(MovieFindermovieFinder){this.movieFinder=movieFinder;}}
![Page 18: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/18.jpg)
AutoWiring
• Don’t explicitly wire dependencies
• Just declare beans
![Page 19: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/19.jpg)
Autowiring qualifiers<beanclass="example.SimpleMovieCatalog"><qualifiervalue="main"/></bean>
<beanclass="example.SimpleMovieCatalog"><qualifiervalue="action"/></bean>
@Autowired@Qualifier("main")privateMovieCatalogmovieCatalog;
![Page 20: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/20.jpg)
Aspect Oriented Programming
• Used internally by Spring;
• e.g. Transaction Management
• AspectJ integration for easy AOP on your Spring beans
![Page 21: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/21.jpg)
2 minute AOP intro
public void myMethod(..) { //do something}
public void trace(..) { //log something}
Class A
Before
![Page 22: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/22.jpg)
Module deep dive
Spring DAOSpring DAO
![Page 23: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/23.jpg)
JdbcTemplate
intcountOfActorsNamedJoe=this.jdbcTemplate.queryForInt("selectcount(0)fromt_actorswherefirst_name=?",newObject[]{"Joe"});
![Page 24: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/24.jpg)
JdbcTemplate
this.jdbcTemplate.update("deletefromactorwhereid=?",newObject[]{newLong.valueOf(actorId)});
![Page 25: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/25.jpg)
SimpleJdbcTemplate
Stringsql="selectid,first_name,last_namefromT_ACTORwhereid=?";
ParameterizedRowMapper<Actor>mapper=newParameterizedRowMapper<Actor>(){publicActormapRow(ResultSetrs,introwNum)throwsSQLException{Actoractor=newActor();actor.setId(rs.getLong("id"));actor.setFirstName(rs.getString("first_name"));actor.setLastName(rs.getString("last_name"));returnactor;}};
returnthis.simpleJdbcTemplate.queryForObject(sql,mapper,id);
![Page 26: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/26.jpg)
Exceptions
• Spring wraps every jdbc exception
• Same structure for ORM exceptions
• Handle specific database exceptions in a uniform way
• Translate checked exception to unchecked exceptions
![Page 27: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/27.jpg)
Module deep dive
Spring ORMSpring ORM
![Page 28: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/28.jpg)
ORM Integration
• Hibernate
• iBATIS
• JDO
• TopLink
• JPA
![Page 29: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/29.jpg)
Session FactoryConfiguration
<beanid="mySessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="dataSource"ref="myDataSource"/><propertyname="mappingResources"><list><value>product.hbm.xml</value></list></property><propertyname="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value></property></bean>
![Page 30: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/30.jpg)
Hibernate TemplateConfiguration
<beanid="myProductDao"class="product.ProductDaoImpl"><propertyname="sessionFactory"ref="mySessionFactory"/>
</bean>
![Page 31: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/31.jpg)
Hibernate Template
publicclassHibernateProductDaoextendsHibernateDaoSupport{
publicCollectionloadProductsByCategory(Stringcategory)throwsDataAccessException,MyException{
returnthis.hibernateTemplate.find("fromtest.Productproductwhereproduct.category=?",category);}
}
![Page 32: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/32.jpg)
Transactions
It’s just more configuration
![Page 33: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/33.jpg)
<beanid="myTxManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="mySessionFactory"/></bean>
<aop:config><aop:pointcutid="productServiceMethods"expression="execution(*product.ProductService.*(..))"/><aop:advisoradvice‐ref="txAdvice"pointcut‐ref="productServiceMethods"/></aop:config>
<tx:adviceid="txAdvice"transaction‐manager="myTxManager"><tx:attributes><tx:methodname="increasePrice*"propagation="REQUIRED"/><tx:methodname="someOtherBusinessMethod"propagation="REQUIRES_NEW"/><tx:methodname="*"propagation="SUPPORTS"read‐only="true"/></tx:attributes></tx:advice>
![Page 34: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/34.jpg)
<beanid="myTxManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="mySessionFactory"/></bean>
<aop:config><aop:pointcutid="productServiceMethods"expression="execution(*product.ProductService.*(..))"/><aop:advisoradvice‐ref="txAdvice"pointcut‐ref="productServiceMethods"/></aop:config>
<tx:adviceid="txAdvice"transaction‐manager="myTxManager"><tx:attributes><tx:methodname="increasePrice*"propagation="REQUIRED"/><tx:methodname="someOtherBusinessMethod"propagation="REQUIRES_NEW"/><tx:methodname="*"propagation="SUPPORTS"read‐only="true"/></tx:attributes></tx:advice>
Transaction manager
![Page 35: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/35.jpg)
<beanid="myTxManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="mySessionFactory"/></bean>
<aop:config><aop:pointcutid="productServiceMethods"expression="execution(*product.ProductService.*(..))"/><aop:advisoradvice‐ref="txAdvice"pointcut‐ref="productServiceMethods"/></aop:config>
<tx:adviceid="txAdvice"transaction‐manager="myTxManager"><tx:attributes><tx:methodname="increasePrice*"propagation="REQUIRED"/><tx:methodname="someOtherBusinessMethod"propagation="REQUIRES_NEW"/><tx:methodname="*"propagation="SUPPORTS"read‐only="true"/></tx:attributes></tx:advice>
Pointcut and Advisor
![Page 36: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/36.jpg)
<beanid="myTxManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="mySessionFactory"/></bean>
<aop:config><aop:pointcutid="productServiceMethods"expression="execution(*product.ProductService.*(..))"/><aop:advisoradvice‐ref="txAdvice"pointcut‐ref="productServiceMethods"/></aop:config>
<tx:adviceid="txAdvice"transaction‐manager="myTxManager"><tx:attributes><tx:methodname="increasePrice*"propagation="REQUIRED"/><tx:methodname="someOtherBusinessMethod"propagation="REQUIRES_NEW"/><tx:methodname="*"propagation="SUPPORTS"read‐only="true"/></tx:attributes></tx:advice>
Advice - Propagation properties
![Page 37: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/37.jpg)
Module deep dive
Spring WEBSpring WEB
![Page 38: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/38.jpg)
Web MVC Components
• Controllers
• View Handlers
• Validation
![Page 39: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/39.jpg)
Controller Hierarchy
AbstractController
CommandController
FormController
WizzardController
MultiactionController
![Page 40: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/40.jpg)
Compared to other web frameworks
• Web MVC is very simple
• It’s ‘action’ based
• Close to the request/response model
• It doesn’t have conversational state
![Page 41: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/41.jpg)
![Page 42: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/42.jpg)
Spring Portlets
• Web MVC for JSR-168
• Exactly the same programming model for Portlets and Servlets
• Doesn’t hide the Portlet specific flow
![Page 43: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/43.jpg)
Testing
• Spring Mock• Integration Testing
![Page 44: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/44.jpg)
Spring Mocks
• Mock often used objects
• Request, Response, Session, JNDI etc.
![Page 45: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/45.jpg)
Spring integration testing
• Test Wiring
• Test DAOs
![Page 46: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/46.jpg)
Test DAOs
• Wire beans
• Manage transactions
• Rollback transaction after each test
• Mix Hibernate and plain JDBC in one transaction
![Page 47: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/47.jpg)
DAO test example
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"daos.xml"})publicfinalclassHibernateTitleDaoTests{@AutowiredprivateHibernateTitleDaotitleDao;publicvoidtestLoadTitle()throwsException{Titletitle=this.titleDao.loadTitle(newLong(10));assertNotNull(title);}}
![Page 48: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/48.jpg)
Closed source Spring?
![Page 49: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/49.jpg)
The verdict
• Spring is very complete
• It’s “environment friendly”
• It helps loose coupling
• It’s much better than J2EE
![Page 50: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/50.jpg)
Do we still need it?
• JEE 5 has a similar programming model
• Seam is a much better web framework
![Page 51: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/51.jpg)
Where to start
• http://springframework.org
• Reference documentation
• Spring Pet store
![Page 52: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/52.jpg)
Spring related
• Spring Webflow
• Spring security (acegi)
• Spring webservices
• Spring integration
![Page 53: Spring 2.5](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b685894a795946128b45ad/html5/thumbnails/53.jpg)
Enjoy your evening