Spring 2.5
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Spring 2.5

on

  • 5,435 views

Introduction to Spring 2.5 for developers not familiar with Spring.

Introduction to Spring 2.5 for developers not familiar with Spring.

Statistics

Views

Total Views
5,435
Views on SlideShare
5,351
Embed Views
84

Actions

Likes
3
Downloads
400
Comments
3

5 Embeds 84

http://www.net4java.com 58
http://www.slideshare.net 22
http://www.lmodules.com 2
http://blog.net4java.in 1
http://net4java.blogspot.in 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Spring 2.5 Presentation Transcript

  • 1. The Spring Framework Paul Bakker
  • 2. Goals An introduction Not pointing a winner
  • 3. Outline • History and overview • Dependency Injection • AOP • Module deep dive • Testing
  • 4. History Founded by Rod Johnson First introduced in his book “J2EE Design & Development”
  • 5. Modules
  • 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. productService.getProducts() Database
  • 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. public
Calculator(ProductService
productService)
{ this.productService
=
productService; } public
double
calculateTotalPrice()
{ 



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

new
Calculator(new
ProductServiceMock()); assertEquals(150,
calc.calculateTotalPrice()); }
  • 11. Program to interfaces not to implementations
  • 12. Inversion of Control A container manages dependencies IoC Container ProductService Calculator
  • 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. 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. Configuration using annotations @Stateless(name
=
quot;ProductEJBquot;) public
class
ProductBean
{ 



public
ProductBean()
{ 



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


private
MovieFinder
movieFinder; 


 


@Autowired 


public
SimpleMovieLister(MovieFinder
movieFinder)
{ 





this.movieFinder
=
movieFinder; 


} }
  • 18. AutoWiring • Don’t explicitly wire dependencies • Just declare beans
  • 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. Aspect Oriented Programming • Used internally by Spring; • e.g. Transaction Management • AspectJ integration for easy AOP on your Spring beans
  • 21. 2 minute AOP intro Class A public void trace(..) { //log something Before } public void myMethod(..) { //do something }
  • 22. Module deep dive
  • 23. JdbcTemplate int
countOfActorsNamedJoe
=
this.jdbcTemplate.queryForInt( quot;select
count(0)
from
t_actors
where
first_name
=
?quot;,
 
new
Object[]{quot;Joequot;});
  • 24. JdbcTemplate this.jdbcTemplate.update( 
quot;delete
from
actor
where
id
=
?quot;, 

new
Object[]
{new
Long.valueOf(actorId)});
  • 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. 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. Module deep dive
  • 28. ORM Integration • Hibernate • iBATIS • JDO • TopLink • JPA
  • 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. Hibernate Template Configuration <bean
id=quot;myProductDaoquot;
class=quot;product.ProductDaoImplquot;> <property
name=quot;sessionFactoryquot;
ref=quot;mySessionFactoryquot;/> </bean>
  • 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. Transactions It’s just more configuration
  • 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. <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. <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. <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. Module deep dive
  • 38. Web MVC Components • Controllers • View Handlers • Validation
  • 39. Controller Hierarchy AbstractController MultiactionController CommandController FormController WizzardController
  • 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. Spring Portlets • Web MVC for JSR-168 • Exactly the same programming model for Portlets and Servlets • Doesn’t hide the Portlet specific flow
  • 42. Testing • Spring Mock • Integration Testing
  • 43. Spring Mocks • Mock often used objects • Request, Response, Session, JNDI etc.
  • 44. Spring integration testing • Test Wiring • Test DAOs
  • 45. Test DAOs • Wire beans • Manage transactions • Rollback transaction after each test • Mix Hibernate and plain JDBC in one transaction
  • 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. Closed source Spring?
  • 48. The verdict • Spring is very complete • It’s “environment friendly” • It helps loose coupling • It’s much better than J2EE
  • 49. Do we still need it? • JEE 5 has a similar programming model • Seam is a much better web framework
  • 50. Where to start • http://springframework.org • Reference documentation • Spring Pet store
  • 51. Spring related • Spring Webflow • Spring security (acegi) • Spring webservices • Spring integration
  • 52. Enjoy your evening