logo_EMBEDIT_RGB.jpg PA165: Service Tier II. Spring framework Petr Adámek logo_EMBEDIT_RGB.jpg Content: Spring Framework •Introduction into Spring •Spring IoC –XML driven –Annotation driven –Code driven •AOP •Transaction Management 2 PA165: Service Tier logo_EMBEDIT_RGB.jpg INTRODUCTION INTO SPRING • 3 PA165: Service Tier logo_EMBEDIT_RGB.jpg Spring framework •http://projects.spring.io/spring-framework/ •Rod Johnson: Expert One-on-One J2EE Design and Development (2002) •Non-invasive framework, very flexible, does not try to reinvent the wheel •Current version 3.2.4 •Very good documentation (http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/) • PA165: Service Tier 4 logo_EMBEDIT_RGB.jpg SPRING IOC • 5 PA165: Service Tier logo_EMBEDIT_RGB.jpg XML Application Context •Configured by XML •Different modules (different namespaces) •Benefits –Pure declarative approach –Allows to change system configuration without changing code (very useful for customizations) •Weaknesses –Not transparent –Lots of configuration is needed –Hard to maintain –Problem with refactoring •Example (property initialization, constructor initialization) • PA165: Service Tier 6 logo_EMBEDIT_RGB.jpg Annotation driven •Configured by annotations •Benefits –Less configuration needed –More clear and transparent –No problem with refaktoring •Weaknesses –Less flexibility •Can be combined with xml configuration (, ) • PA165: Service Tier 7 logo_EMBEDIT_RGB.jpg Annotation driven •Proprietary –@Component, @Service, @Repository –@Autowired, @Required •JSR-330: –@Named –@Inject, @Qualifier •Other –@Resource, @PersistenceContext, @PersistenceUnit •Example • PA165: Service Tier 8 logo_EMBEDIT_RGB.jpg Code driven •Benefits –Almost any code could be evaluated during initialization –No problem with refactoring •Weaknesses –Pure imperative approach –Configuration is hardcoded into the class •@Configuration, @Bean •@ComponentScan, @PropertySource, @Import •Each @Bean method is evaluated just once! •Example • PA165: Service Tier 9 logo_EMBEDIT_RGB.jpg SPRING AOP • 10 PA165: Service Tier logo_EMBEDIT_RGB.jpg AOP •http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/aop.html PA165: Service Tier 11 logo_EMBEDIT_RGB.jpg TRANSACTION MANAGEMENT • PA165: Service Tier 12 logo_EMBEDIT_RGB.jpg Transactions Management •PlatformTransactionManager –DataSourceTransactionManager –JtaTransactionManager –JpaTransactionManager –HibernateTransactionManager •Declarative approach (controlled with AOP, @Transactional) •Imperative approach (API of PlatformTransactionManager) • •@EnableTransactionManagement •Integration with JPA • PA165: Service Tier 13 logo_EMBEDIT_RGB.jpg Questions •? 14 PA165: Service Tier