Spring 2.5

2,714 views

Published on

Introduction to Spring 2.5 for developers not familiar with Spring.

Published in: Technology
3 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total views
2,714
On SlideShare
0
From Embeds
0
Number of Embeds
84
Actions
Shares
0
Downloads
402
Comments
3
Likes
3
Embeds 0
No embeds

No notes for slide

Spring 2.5

  1. 1. The Spring Framework Paul Bakker
  2. 2. Goals An introduction Not pointing a winner
  3. 3. Outline • History and overview • Dependency Injection • AOP • Module deep dive • Testing
  4. 4. History Founded by Rod Johnson First introduced in his book “J2EE Design & Development”
  5. 5. Modules
  6. 6. Why Dependency Injection? public
double
calculateTotalPrice()
{ 



ProductService
productService
=
new
ProductService(); 



List
products
=
productService.getProducts(); //iterate
products
and
sum
prices return
total; } How would you test this method?
  7. 7. productService.getProducts() Database
  8. 8. Throws public
void
testCalculateTotalPrice()
{ Calculator
calc
=
new
Calculator(); Exception... assertEquals(150,
calc.calculateTotalPrice()); } public
double
calculateTotalPrice()
{ 



ProductService
productService
=
new
ProductService(); 



List
products
=
productService.getProducts(); //iterate
products
and
sum
prices return
total; }
  9. 9. public
Calculator(ProductService
productService)
{ this.productService
=
productService; } public
double
calculateTotalPrice()
{ 



List
products
=
productService.getProducts(); //iterate
products
and
sum
prices return
total; }
  10. 10. public
void
testCalculateTotalPrice()
{ Calculator
calc
=
 

new
Calculator(new
ProductServiceMock()); assertEquals(150,
calc.calculateTotalPrice()); }
  11. 11. Program to interfaces not to implementations
  12. 12. Inversion of Control A container manages dependencies IoC Container ProductService Calculator
  13. 13. Configuring a container ProductService
pService
=
new
ProductServiceImpl(); Calculater
calc
=
new
Calculator(pService); //init
other
instances This will be a lot of work however...
  14. 14. Configuration using XML <bean
id=quot;productServicequot;


 


class=quot;services.ProductServiceImplquot;/> <bean
id=quot;calculatorquot;
class=quot;calc.Calculatorquot;> 


<constructor‐arg
index=quot;0quot;
ref=quot;productServicequot;/>






 
 </bean> Spring version 1 way
  15. 15. Configuration using annotations @Stateless(name
=
quot;ProductEJBquot;) public
class
ProductBean
{ 



public
ProductBean()
{ 



} } @EJB
ShoppingBasketBean
shoppingBasket;
 JEE 5 way
  16. 16. Spring annotations • Spring can now be configured using annotations • Use annotations for component declarations • Use XML for container wide configuration
  17. 17. Annotation example @Service public
class
SimpleMovieLister
{ 


private
MovieFinder
movieFinder; 


 


@Autowired 


public
SimpleMovieLister(MovieFinder
movieFinder)
{ 





this.movieFinder
=
movieFinder; 


} }
  18. 18. AutoWiring • Don’t explicitly wire dependencies • Just declare beans
  19. 19. Autowiring qualifiers <bean
class=quot;example.SimpleMovieCatalogquot;> 


<qualifier
value=quot;mainquot;/> </bean> <bean
class=quot;example.SimpleMovieCatalogquot;> 


<qualifier
value=quot;actionquot;/> </bean> @Autowired @Qualifier(quot;mainquot;) private
MovieCatalog
movieCatalog;
  20. 20. Aspect Oriented Programming • Used internally by Spring; • e.g. Transaction Management • AspectJ integration for easy AOP on your Spring beans
  21. 21. 2 minute AOP intro Class A public void trace(..) { //log something Before } public void myMethod(..) { //do something }
  22. 22. Module deep dive
  23. 23. JdbcTemplate int
countOfActorsNamedJoe
=
this.jdbcTemplate.queryForInt( quot;select
count(0)
from
t_actors
where
first_name
=
?quot;,
 
new
Object[]{quot;Joequot;});
  24. 24. JdbcTemplate this.jdbcTemplate.update( 
quot;delete
from
actor
where
id
=
?quot;, 

new
Object[]
{new
Long.valueOf(actorId)});
  25. 25. SimpleJdbcTemplate String
sql
=
quot;select
id,
first_name,
last_name
from
T_ACTOR
where
id
=
?quot;; ParameterizedRowMapper<Actor>
mapper
=
new
ParameterizedRowMapper<Actor>()
 { 

public
Actor
mapRow(ResultSet
rs,
int
rowNum)
throws
SQLException
{ 




Actor
actor
=
new
Actor(); 




actor.setId(rs.getLong(quot;idquot;)); 




actor.setFirstName(rs.getString(quot;first_namequot;)); 




actor.setLastName(rs.getString(quot;last_namequot;)); 




return
actor; 

} }; return
this.simpleJdbcTemplate.queryForObject(sql,
mapper,
id);
  26. 26. 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
  27. 27. Module deep dive
  28. 28. ORM Integration • Hibernate • iBATIS • JDO • TopLink • JPA
  29. 29. Session Factory Configuration <bean
id=quot;mySessionFactoryquot;

 

class=quot;org.springframework.orm.hibernate3.LocalSessionFactoryBeanquot;> 


<property
name=quot;dataSourcequot;
ref=quot;myDataSourcequot;/> 


<property
name=quot;mappingResourcesquot;> 





<list> 








<value>product.hbm.xml</value> 





</list> 


</property> 


<property
name=quot;hibernatePropertiesquot;> 





<value> 








hibernate.dialect=org.hibernate.dialect.HSQLDialect 





</value> 


</property> </bean>
  30. 30. Hibernate Template Configuration <bean
id=quot;myProductDaoquot;
class=quot;product.ProductDaoImplquot;> <property
name=quot;sessionFactoryquot;
ref=quot;mySessionFactoryquot;/> </bean>
  31. 31. Hibernate Template public
class
HibernateProductDao
extends
HibernateDaoSupport
{ 


public
Collection
loadProductsByCategory(String
category)
throws





 








DataAccessException,
MyException
{ 





return
this.hibernateTemplate.find( 




quot;from
test.Product
product
where
product.category=?quot;,
category); } }
  32. 32. Transactions It’s just more configuration
  33. 33. <bean
id=quot;myTxManagerquot;





 

class=quot;org.springframework.orm.hibernate3.HibernateTransactionManagerquot;> 


<property
name=quot;sessionFactoryquot;
ref=quot;mySessionFactoryquot;/> </bean> <aop:config> 


<aop:pointcut
id=quot;productServiceMethodsquot;
expression=quot;execution(*



 




product.ProductService.*(..))quot;/> 


<aop:advisor
advice‐ref=quot;txAdvicequot;
pointcut‐

 




ref=quot;productServiceMethodsquot;/> </aop:config> <tx:advice
id=quot;txAdvicequot;
transaction‐manager=quot;myTxManagerquot;> 


<tx:attributes> 





<tx:method
name=quot;increasePrice*quot;
propagation=quot;REQUIREDquot;/> 





<tx:method
name=quot;someOtherBusinessMethodquot;
 







propagation=quot;REQUIRES_NEWquot;/> 





<tx:method
name=quot;*quot;
propagation=quot;SUPPORTSquot;
read‐only=quot;truequot;/> 


</tx:attributes> </tx:advice>
  34. 34. <bean
id=quot;myTxManagerquot;





 

class=quot;org.springframework.orm.hibernate3.HibernateTransactionManagerquot;> 


<property
name=quot;sessionFactoryquot;
ref=quot;mySessionFactoryquot;/> </bean> <aop:config> 


<aop:pointcut
id=quot;productServiceMethodsquot;
expression=quot;execution(*



 




product.ProductService.*(..))quot;/> 


<aop:advisor
advice‐ref=quot;txAdvicequot;
pointcut‐

 




ref=quot;productServiceMethodsquot;/> </aop:config> <tx:advice
id=quot;txAdvicequot;
transaction‐manager=quot;myTxManagerquot;> 


<tx:attributes> 





<tx:method
name=quot;increasePrice*quot;
propagation=quot;REQUIREDquot;/> 





<tx:method
name=quot;someOtherBusinessMethodquot;
 







propagation=quot;REQUIRES_NEWquot;/> 





<tx:method
name=quot;*quot;
propagation=quot;SUPPORTSquot;
read‐only=quot;truequot;/> 


</tx:attributes> </tx:advice> Transaction manager
  35. 35. <bean
id=quot;myTxManagerquot;





 

class=quot;org.springframework.orm.hibernate3.HibernateTransactionManagerquot;> 


<property
name=quot;sessionFactoryquot;
ref=quot;mySessionFactoryquot;/> </bean> <aop:config> 


<aop:pointcut
id=quot;productServiceMethodsquot;
expression=quot;execution(*



 




product.ProductService.*(..))quot;/> 


<aop:advisor
advice‐ref=quot;txAdvicequot;
pointcut‐

 




ref=quot;productServiceMethodsquot;/> </aop:config> <tx:advice
id=quot;txAdvicequot;
transaction‐manager=quot;myTxManagerquot;> 


<tx:attributes> 





<tx:method
name=quot;increasePrice*quot;
propagation=quot;REQUIREDquot;/> 





<tx:method
name=quot;someOtherBusinessMethodquot;
 







propagation=quot;REQUIRES_NEWquot;/> 





<tx:method
name=quot;*quot;
propagation=quot;SUPPORTSquot;
read‐only=quot;truequot;/> 


</tx:attributes> </tx:advice> Pointcut and Advisor
  36. 36. <bean
id=quot;myTxManagerquot;





 

class=quot;org.springframework.orm.hibernate3.HibernateTransactionManagerquot;> 


<property
name=quot;sessionFactoryquot;
ref=quot;mySessionFactoryquot;/> </bean> <aop:config> 


<aop:pointcut
id=quot;productServiceMethodsquot;
expression=quot;execution(*



 




product.ProductService.*(..))quot;/> 


<aop:advisor
advice‐ref=quot;txAdvicequot;
pointcut‐

 




ref=quot;productServiceMethodsquot;/> </aop:config> <tx:advice
id=quot;txAdvicequot;
transaction‐manager=quot;myTxManagerquot;> 


<tx:attributes> 





<tx:method
name=quot;increasePrice*quot;
propagation=quot;REQUIREDquot;/> 





<tx:method
name=quot;someOtherBusinessMethodquot;
 







propagation=quot;REQUIRES_NEWquot;/> 





<tx:method
name=quot;*quot;
propagation=quot;SUPPORTSquot;
read‐only=quot;truequot;/> 


</tx:attributes> </tx:advice> Advice - Propagation properties
  37. 37. Module deep dive
  38. 38. Web MVC Components • Controllers • View Handlers • Validation
  39. 39. Controller Hierarchy AbstractController MultiactionController CommandController FormController WizzardController
  40. 40. 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
  41. 41. Spring Portlets • Web MVC for JSR-168 • Exactly the same programming model for Portlets and Servlets • Doesn’t hide the Portlet specific flow
  42. 42. Testing • Spring Mock • Integration Testing
  43. 43. Spring Mocks • Mock often used objects • Request, Response, Session, JNDI etc.
  44. 44. Spring integration testing • Test Wiring • Test DAOs
  45. 45. Test DAOs • Wire beans • Manage transactions • Rollback transaction after each test • Mix Hibernate and plain JDBC in one transaction
  46. 46. DAO test example @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={quot;daos.xmlquot;}) public
final
class
HibernateTitleDaoTests
{ 


@Autowired 


private
HibernateTitleDao
titleDao; 


 


public
void
testLoadTitle()
throws
Exception
{ 





Title
title
=
this.titleDao.loadTitle(new
Long(10)); 





assertNotNull(title); 


} }
  47. 47. Closed source Spring?
  48. 48. The verdict • Spring is very complete • It’s “environment friendly” • It helps loose coupling • It’s much better than J2EE
  49. 49. Do we still need it? • JEE 5 has a similar programming model • Seam is a much better web framework
  50. 50. Where to start • http://springframework.org • Reference documentation • Spring Pet store
  51. 51. Spring related • Spring Webflow • Spring security (acegi) • Spring webservices • Spring integration
  52. 52. Enjoy your evening

×