Spring overview

1,155
-1

Published on

Spring Overview

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,155
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Template closures JDBC support also provided
  • Spring overview

    1. 1. Spring Framework Dhaval Shah
    2. 2. Contents <ul><li>Why? </li></ul><ul><li>Overview </li></ul><ul><li>DI/IoC </li></ul><ul><li>Spring-Hibernate integration </li></ul><ul><li>Transaction Manager </li></ul><ul><li>AOP </li></ul>
    3. 3. Why Use Spring? <ul><li>Wiring of components (Dependency Injection) </li></ul><ul><ul><li>Promotes/simplifies decoupling, design to interfaces, TDD </li></ul></ul><ul><li>Declarative programming without J2EE </li></ul><ul><ul><li>Easily configured aspects, esp. transaction support </li></ul></ul><ul><li>Simplify use of popular technologies </li></ul><ul><ul><li>Abstractions insulate application from specifics, eliminate redundant code, and handle common error conditions </li></ul></ul><ul><ul><li>Underlying technology specifics still accessible (closures) </li></ul></ul>
    4. 4. Why Use Spring? <ul><li>Well designed </li></ul><ul><ul><li>Easy to extend </li></ul></ul><ul><ul><li>Many reusable classes </li></ul></ul>
    5. 5. Spring Overview <ul><li>Lightweight </li></ul><ul><li>Inversion of Control </li></ul><ul><li>Aspect Oriented </li></ul><ul><li>Container </li></ul><ul><li>Framework </li></ul>
    6. 6. Spring Framework
    7. 7. Spring DI/IoC <ul><li>Dependency Inversion Principle </li></ul><ul><ul><li>High level modules should not depend upon low level modules, both should depend upon abstractions </li></ul></ul><ul><ul><li>Abstractions should not depend upon details, details should depend upon abstractions </li></ul></ul><ul><li>Three defining factors of bad code </li></ul><ul><ul><li>Rigidity </li></ul></ul><ul><ul><li>Fragility </li></ul></ul><ul><ul><li>Immobility </li></ul></ul>
    8. 8. Spring DI/IoC (Cont’d) <ul><li>Ways to instantiate container/working with application context </li></ul><ul><ul><li>ClassPathXmlApplicationContext </li></ul></ul><ul><ul><li>FileSystemXmlApplicationContext </li></ul></ul><ul><ul><li>XmlWebApplicationContext </li></ul></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><ul><li>ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { &quot;cfg/applicationContext.xml&quot;}); </li></ul></ul></ul><ul><li>Variations on dependency injection </li></ul><ul><ul><li>Constructor-based (PicoContainer, Spring) </li></ul></ul><ul><ul><li>Setter-based (Spring) </li></ul></ul><ul><ul><li>Interface based (Avalon) </li></ul></ul>
    9. 9. Spring DI/IoC (Cont’d) <ul><li>Property and constructor based IoC </li></ul><bean id=&quot;exampleBean&quot; class=&quot;examples.ExampleBean&quot;> <property name=&quot;beanOne&quot;><ref bean=&quot;anotherExampleBean&quot;/></property> <property name=&quot;beanTwo&quot;><ref bean=&quot;yetAnotherBean&quot;/></property> <property name=&quot;integerProperty&quot;>1</property> </bean> <bean id=&quot;anotherExampleBean&quot; class=&quot;examples.AnotherBean&quot;/> <bean id=&quot;yetAnotherBean&quot; class=&quot;examples.YetAnotherBean&quot;/> <bean id=&quot;exampleBean&quot; class=&quot;examples.ExampleBean&quot;> <constructor-arg><ref bean=&quot;anotherExampleBean&quot;/></constructor-arg> <constructor-arg><ref bean=&quot;yetAnotherBean&quot;/></constructor-arg> <constructor-arg><value>1</value></constructor-arg> </bean> <bean id=&quot;anotherExampleBean&quot; class=&quot;examples.AnotherBean&quot;/> <bean id=&quot;yetAnotherBean&quot; class=&quot;examples.YetAnotherBean&quot;/>
    10. 10. Spring DI/IoC (Cont’d) <ul><li>Bean’s Lifecycle </li></ul>
    11. 11. Dependency Injection (cont'd) <ul><li>Singleton Vs Prototype </li></ul><ul><ul><li><bean id=&quot;myBean&quot; class=&quot;src.beans.TestBean&quot; singleton=&quot;false&quot;> </li></ul></ul><ul><ul><li><property name=&quot;name&quot; value=&quot;JAVA&quot;/> </li></ul></ul><ul><ul><li></bean> </li></ul></ul><ul><li>Intitialization & Destruction </li></ul><ul><ul><li><bean id=&quot;myBean&quot; class=&quot;src.beans.TestBean&quot; init-method=&quot;setUp&quot; destroy-method=&quot;tearDown&quot;> </li></ul></ul><ul><ul><li><property name=&quot;name&quot; value=&quot;JAVA&quot;/> </li></ul></ul><ul><ul><li></bean> </li></ul></ul><ul><li>Referencing other beans </li></ul><ul><ul><li><bean id=&quot;businesslogicbean&quot; class=&quot;org.springframework.aop.framework.ProxyFactoryBean&quot;> </li></ul></ul><ul><ul><li><property name=&quot;target&quot;> </li></ul></ul><ul><ul><li><ref local=&quot;beanTarget&quot;/> </li></ul></ul><ul><ul><li></property> </li></ul></ul><ul><ul><li></bean> </li></ul></ul><ul><ul><li><bean id=&quot;beanTarget&quot; class=&quot;src.aop.BusinessLogic&quot;/> </li></ul></ul>
    12. 12. Dependency Injection (cont'd) <ul><li>BeanFactory configured components need have no Spring dependencies </li></ul><ul><ul><li>Simple JavaBeans </li></ul></ul><ul><li>Beans are singletons by default </li></ul><ul><li>Properties may be simple values or references to other beans </li></ul><ul><li>Built-in support for defining Lists, Maps, Sets, and Properties collection types. </li></ul><ul><ul><li>Custom PropertyEditors may be defined to convert string values to other, arbitrary types. </li></ul></ul>
    13. 13. Spring AOP
    14. 14. AOP Fundamentals <ul><li>Aspect-oriented programming (AOP) provides for simplified application of cross-cutting concerns </li></ul><ul><ul><li>Transaction management </li></ul></ul><ul><ul><li>Security </li></ul></ul><ul><ul><li>Logging </li></ul></ul><ul><ul><li>Auditing </li></ul></ul><ul><ul><li>Locking </li></ul></ul>
    15. 15. AOP Fundamentals <ul><li>Aspect - Implementation of a cross-cutting functionality (E.g logging) </li></ul><ul><li>Joinpoint - Execution point of application where an aspect can be plugged (e.g methods) </li></ul><ul><li>Advice – Actual implementation of aspect </li></ul><ul><li>Pointcut - A set of joinpoints specifying where advice should be applied </li></ul><ul><li>Introduction – Allows to add methods or fields to existing classes. </li></ul><ul><li>Target – Class that is being advised </li></ul><ul><li>Proxy – Object created after applying advice to target </li></ul>
    16. 16. AOP Fundamentals <ul><li>Weaving - Process of applying aspects to a target object to create a new, proxied object </li></ul><ul><ul><li>Can take place at – </li></ul></ul><ul><ul><ul><li>Compile time </li></ul></ul></ul><ul><ul><ul><li>Class load time </li></ul></ul></ul><ul><ul><ul><li>Runtime </li></ul></ul></ul>
    17. 17. Spring AOP <ul><li>Advice Types- </li></ul>Advice Type Interface Description Around org.aopalliance.intercept.MethodInterceptor Intercepts call to the target method Before org.springframework.aop.BeforeAdvice Called before the target method is invoked After org.springframework.aop.AfterReturningAdvice Called after the target method is invoked Throws org.springframework.aop.ThrowsAdvice Called when target method throws an exception
    18. 18. Spring AOP Example
    19. 19. Transactions
    20. 20. Spring Transaction Manager Transaction Manager Implementation Purpose org.springframework.jdbc.datasource.DataSourceTransactionManager Manages transactions on a single JDBC DataSource. org.springframework.orm.hibernate.HibernateTransactionManager Used to manage transactions when Hibernate is the persistence mechanism. org.springframework.orm.jdo.JdoTransactionManager Used to manage transactions when JDO is used for persistence. org.springframework.transaction.jta.JtaTransactionManager Manages transactions using a Java Transaction API (JTA ) implementation. Must be used when a transaction spans multiple resources. org.springframework.orm.ojb.PersistenceBrokerTransactionManager Manages transactions when Apache’s Object Relational Bridge (OJB) is used for persistence.
    21. 21. Transaction Configuration <ul><ul><li><bean id=&quot;sessionFactory&quot; </li></ul></ul><ul><ul><li>class=&quot;org.springframework.orm.hibernate.LocalSessionFactoryBean&quot;> </li></ul></ul><ul><ul><li><property name=&quot;dataSource&quot;><ref bean=&quot;dataSource&quot;/></property> </li></ul></ul><ul><ul><li><property name=&quot;mappingResources&quot;> </li></ul></ul><ul><ul><li><list> </li></ul></ul><ul><ul><li><value>com/../model/*.hbm.xml</value> </li></ul></ul><ul><ul><li></list> </li></ul></ul><ul><ul><li></property> </li></ul></ul><ul><ul><li></bean> </li></ul></ul><ul><ul><li><bean id=&quot;transactionManager” </li></ul></ul><ul><ul><li>class=&quot;org.springframework.orm.hibernate.HibernateTransactionManager&quot;> </li></ul></ul><ul><ul><li><property name=&quot;sessionFactory&quot;> </li></ul></ul><ul><ul><li><ref bean=&quot;sessionFactory&quot;/> </li></ul></ul><ul><ul><li></property> </li></ul></ul><ul><ul><li></bean> </li></ul></ul>
    22. 22. Declarative Transactions <ul><li>Declarative transactional support can be added to any bean by using TransactionProxyFactoryBean </li></ul><ul><li>Similar to EJB, transaction attributes may be defined on a per-method basis </li></ul><ul><li>Also allows definition of pre- and post- interceptors (e.g. for security) </li></ul>
    23. 23. Injecting Transaction Support <bean id=“reservationService&quot; class=&quot;org.springframework.transaction.interceptor.TransactionProxyFactoryBean&quot;> <property name=&quot;transactionManager&quot;> <ref bean=&quot;transactionManager&quot;/> </property> <property name=&quot;target&quot;><ref local=“reservationServiceTarget&quot;/></property> <property name=&quot;transactionAttributes&quot;> <props> <prop key=“reserveRoom*&quot;>PROPAGATION_REQUIRED</prop> <prop key=&quot;*&quot;>PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> Declarative transaction support for single bean
    24. 24. Transaction Autoproxy < bean id=&quot;autoproxy&quot; class=&quot;org...DefaultAdvisorAutoProxyCreator&quot;> </bean> <bean id=&quot;transactionAdvisor&quot; class=&quot;org...TransactionAttributeSourceAdvisor&quot; autowire =&quot;constructor&quot; > </bean> <bean id=&quot;transactionInterceptor&quot; class=&quot;org...TransactionInterceptor&quot; autowire =&quot;byType&quot;> </bean> <bean id=&quot;transactionAttributeSource&quot; class=&quot;org...AttributesTransactionAttributeSource&quot; autowire =&quot;constructor&quot;> </bean> <bean id=&quot;attributes&quot; class=&quot;org...CommonsAttributes&quot; /> Caches metadata from classes Generic autoproxy support Applies transaction using transactionManager Invokes interceptor based on attributes
    25. 25. Data Access
    26. 26. Data Access <ul><li>DAO support provides pluggable framework for persistence </li></ul><ul><li>Currently supports JDBC, Hibernate, JDO, and iBatis </li></ul><ul><li>Defines consistent exception hierarchy (based on RuntimeException) </li></ul><ul><li>Provides abstract “Support” classes for each technology </li></ul><ul><ul><li>Template methods define specific queries </li></ul></ul>
    27. 27. Hibernate DAO Example public class ReservationDaoImpl extends HibernateDaoSupport implements ReservationDao { public Reservation getReservation (Long orderId) { return (Reservation)getHibernateTemplate().load(Reservation .class, orderId); } public void saveReservation (Reservation r) { getHibernateTemplate().saveOrUpdate(r); } public void remove(Reservation Reservation) { getHibernateTemplate().delete(r); }
    28. 28. Hibernate DAO (cont’d) public Reservation[] findReservations(Room room) { List list = getHibernateTemplate().find( &quot;from Reservation reservation “ + “ where reservation.resource =? “ + “ order by reservation.start&quot;, instrument); return (Reservation[]) list.toArray(new Reservation[list.size()]);
    29. 29. Hibernate DAO (cont’d) public Reservation[] findReservations(final DateRange range) { final HibernateTemplate template = getHibernateTemplate(); List list = (List) template.execute(new HibernateCallback() { public Object doInHibernate(Session session) { Query query = session.createQuery( &quot;from Reservation r “ + “ where r.start > :rangeStart and r.start < :rangeEnd “); query.setDate(&quot;rangeStart&quot;, range.getStartDate() query.setDate(&quot;rangeEnd&quot;, range.getEndDate()) return query.list(); } }); return (Reservation[]) list.toArray(new Reservation[list.size()]); } }
    30. 30. Hibernate Example <bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate.LocalSessionFactoryBean&quot;> <property name=&quot;dataSource&quot;><ref bean=&quot;dataSource&quot;/></property> <property name=&quot;mappingResources&quot;> <list> <value>com/jensenp/Reservation/Room.hbm.xml</value> <value>com/jensenp/Reservation/Reservation.hbm.xml</value> <value>com/jensenp/Reservation/Resource.hbm.xml</value> </list> </property> <property name=&quot;hibernateProperties&quot;> <props> <prop key=&quot;hibernate.dialect&quot;>${hibernate.dialect}</prop> <prop key=&quot;hibernate.hbm2ddl.auto&quot;>${hibernate.hbm2ddl.auto} </prop> <prop key=&quot;hibernate.show_sql&quot;>${hibernate.show_sql}</prop> </props> </property> </bean> <bean id=“reservationDao&quot; class=&quot;com.jensenp.Reservation.ReservationDaoImpl&quot;> <property name=&quot;sessionFactory&quot;><ref bean=&quot;sessionFactory&quot;/> </property> </bean>
    31. 31. JDBC Support <ul><li>JDBCTemplate provides </li></ul><ul><ul><li>Translation of SQLExceptions to more meaningful Spring Runtime exceptions </li></ul></ul><ul><ul><li>Integrates thread-specific transactions </li></ul></ul><ul><li>MappingSQLQuery simplifies mapping of ResultSets to Java objects </li></ul>
    32. 32. Web Framework
    33. 33. DispatcherServlet <ul><li>The DispatcherServlet is the Spring Front Controller </li></ul><ul><li>Initializes WebApplicationContext </li></ul><ul><li>Uses /WEB-INF/[servlet-name]-servlet.xml by default </li></ul><ul><li>WebApplicationContext is bound into ServletContext </li></ul>
    34. 34. DispatcherServlet Configuration <ul><li>HandlerMapping </li></ul><ul><ul><li>Routing of requests to handlers </li></ul></ul><ul><li>HandlerAdapter </li></ul><ul><ul><li>Adapts to handler interface. Default utilizes Controller s </li></ul></ul><ul><li>HandlerExceptionResolver </li></ul><ul><ul><li>Maps exceptions to error pages </li></ul></ul><ul><ul><li>Similar to standard Servlet, but more flexible </li></ul></ul><ul><li>ViewResolver </li></ul><ul><ul><li>Maps symbolic name to view </li></ul></ul>
    35. 35. Dispatcher Servlet Configuration <ul><li>MultipartResolver </li></ul><ul><ul><li>Handling of file upload </li></ul></ul><ul><li>LocaleResolver </li></ul><ul><ul><li>Default uses HTTP accept header, cookie, or session </li></ul></ul>
    36. 36. Controllers <ul><li>Controller interface defines one method </li></ul><ul><ul><li>ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception </li></ul></ul><ul><li>ModelAndView consists of a view identifier and a Map of model data </li></ul>
    37. 37. Controller Implementations <ul><li>CommandControllers bind parameters to data objects </li></ul><ul><li>AbstractCommandController </li></ul><ul><li>AbstractFormController </li></ul><ul><li>SimpleFormController </li></ul><ul><li>WizardFormController </li></ul>
    38. 38. References <ul><li>Spring’s homepage: http://www.springframework.org </li></ul><ul><li>“ Introducing the Spring Framework” by Rod Johnson: http://theserverside.com/news/thread.jsp?thread_id=21893 </li></ul><ul><li>“ Inversion of control containers and dependency injection” by Martin Fowler: http://www.martinfowler.com/articles/injection.html </li></ul><ul><li>AOP Alliance: http://aopalliance.sourceforge.net </li></ul>

    ×