Spring 2.5

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

2 Favorites

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=\"productService\"


 


class=\"services.ProductServiceImpl\"/> <bean
id=\"calculator\"
class=\"calc.Calculator\"> 


<constructor‐arg
index=\"0\"
ref=\"productService\"/>






 
 </bean> Spring version 1 way
  15. Configuration using annotations @Stateless(name
=
\"ProductEJB\") 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=\"example.SimpleMovieCatalog\"> 


<qualifier
value=\"main\"/> </bean> <bean
class=\"example.SimpleMovieCatalog\"> 


<qualifier
value=\"action\"/> </bean> @Autowired @Qualifier(\"main\") 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( \"select
count(0)
from
t_actors
where
first_name
=
?\",
 
new
Object[]{\"Joe\"});
  24. JdbcTemplate this.jdbcTemplate.update( 
\"delete
from
actor
where
id
=
?\", 

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

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




Actor
actor
=
new
Actor(); 




actor.setId(rs.getLong(\"id\")); 




actor.setFirstName(rs.getString(\"first_name\")); 




actor.setLastName(rs.getString(\"last_name\")); 




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=\"mySessionFactory\"

 

class=\"org.springframework.orm.hibernate3.LocalSessionFactoryBean\"> 


<property
name=\"dataSource\"
ref=\"myDataSource\"/> 


<property
name=\"mappingResources\"> 





<list> 








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





</list> 


</property> 


<property
name=\"hibernateProperties\"> 





<value> 








hibernate.dialect=org.hibernate.dialect.HSQLDialect 





</value> 


</property> </bean>
  30. Hibernate Template Configuration <bean
id=\"myProductDao\"
class=\"product.ProductDaoImpl\"> <property
name=\"sessionFactory\"
ref=\"mySessionFactory\"/> </bean>
  31. Hibernate Template public
class
HibernateProductDao
extends
HibernateDaoSupport
{ 


public
Collection
loadProductsByCategory(String
category)
throws





 








DataAccessException,
MyException
{ 





return
this.hibernateTemplate.find( 




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





 

class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\"> 


<property
name=\"sessionFactory\"
ref=\"mySessionFactory\"/> </bean> <aop:config> 


<aop:pointcut
id=\"productServiceMethods\"
expression=\"execution(*



 




product.ProductService.*(..))\"/> 


<aop:advisor
advice‐ref=\"txAdvice\"
pointcut‐

 




ref=\"productServiceMethods\"/> </aop:config> <tx:advice
id=\"txAdvice\"
transaction‐manager=\"myTxManager\"> 


<tx:attributes> 





<tx:method
name=\"increasePrice*\"
propagation=\"REQUIRED\"/> 





<tx:method
name=\"someOtherBusinessMethod\"
 







propagation=\"REQUIRES_NEW\"/> 





<tx:method
name=\"*\"
propagation=\"SUPPORTS\"
read‐only=\"true\"/> 


</tx:attributes> </tx:advice>
  34. <bean
id=\"myTxManager\"





 

class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\"> 


<property
name=\"sessionFactory\"
ref=\"mySessionFactory\"/> </bean> <aop:config> 


<aop:pointcut
id=\"productServiceMethods\"
expression=\"execution(*



 




product.ProductService.*(..))\"/> 


<aop:advisor
advice‐ref=\"txAdvice\"
pointcut‐

 




ref=\"productServiceMethods\"/> </aop:config> <tx:advice
id=\"txAdvice\"
transaction‐manager=\"myTxManager\"> 


<tx:attributes> 





<tx:method
name=\"increasePrice*\"
propagation=\"REQUIRED\"/> 





<tx:method
name=\"someOtherBusinessMethod\"
 







propagation=\"REQUIRES_NEW\"/> 





<tx:method
name=\"*\"
propagation=\"SUPPORTS\"
read‐only=\"true\"/> 


</tx:attributes> </tx:advice> Transaction manager
  35. <bean
id=\"myTxManager\"





 

class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\"> 


<property
name=\"sessionFactory\"
ref=\"mySessionFactory\"/> </bean> <aop:config> 


<aop:pointcut
id=\"productServiceMethods\"
expression=\"execution(*



 




product.ProductService.*(..))\"/> 


<aop:advisor
advice‐ref=\"txAdvice\"
pointcut‐

 




ref=\"productServiceMethods\"/> </aop:config> <tx:advice
id=\"txAdvice\"
transaction‐manager=\"myTxManager\"> 


<tx:attributes> 





<tx:method
name=\"increasePrice*\"
propagation=\"REQUIRED\"/> 





<tx:method
name=\"someOtherBusinessMethod\"
 







propagation=\"REQUIRES_NEW\"/> 





<tx:method
name=\"*\"
propagation=\"SUPPORTS\"
read‐only=\"true\"/> 


</tx:attributes> </tx:advice> Pointcut and Advisor
  36. <bean
id=\"myTxManager\"





 

class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\"> 


<property
name=\"sessionFactory\"
ref=\"mySessionFactory\"/> </bean> <aop:config> 


<aop:pointcut
id=\"productServiceMethods\"
expression=\"execution(*



 




product.ProductService.*(..))\"/> 


<aop:advisor
advice‐ref=\"txAdvice\"
pointcut‐

 




ref=\"productServiceMethods\"/> </aop:config> <tx:advice
id=\"txAdvice\"
transaction‐manager=\"myTxManager\"> 


<tx:attributes> 





<tx:method
name=\"increasePrice*\"
propagation=\"REQUIRED\"/> 





<tx:method
name=\"someOtherBusinessMethod\"
 







propagation=\"REQUIRES_NEW\"/> 





<tx:method
name=\"*\"
propagation=\"SUPPORTS\"
read‐only=\"true\"/> 


</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={\"daos.xml\"}) 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

+ Paul BakkerPaul Bakker, 8 months ago

custom

1737 views, 2 favs, 1 embeds more stats

Introduction to Spring 2.5 for developers not famil more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 1737
    • 1728 on SlideShare
    • 9 from embeds
  • Comments 1
  • Favorites 2
  • Downloads 182
Most viewed embeds
  • 9 views on http://www.net4java.com

more

All embeds
  • 9 views on http://www.net4java.com

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories