Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Spring Framework Essentials

407 views

Published on

A short introduction to Spring Framework and the main points of interest for the Spring application developer

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Spring Framework Essentials

  1. 1. Spring “Less” is More Based on the official reference docs (v4.3.2)
  2. 2. What is it? - Application framework - Inversion of Control (IoC) container - Dependency Injection (DI) - “Ask the framework for the resource” - Many additional (optional) modules - AOP, Web MVC, Data, Security, Mobile, ... - “Alternative” to Java EE - Actually, on top and ahead of it
  3. 3. Why is it?.. - Historically, greatly eased application development - Java EE implementations has since caught up nicely - Not held back by standardization committees - Advanced features not yet in the Java EE spec - No need for a full application server - Servlet container is enough - Highly modular and granular DI
  4. 4. Spring Projects
  5. 5. Spring Framework
  6. 6. Bootstrapping - Configuration - XML - Java-based - IoC container init - ApplicationContext - Wiring - Run-time factory
  7. 7. Basic XML Configuration <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="helloOtherWorldConfig.xml"/> <bean id="helloWorld" class="com.world.HelloWorld" scope="prototype" lazy-init="true"> <property name="param1" value="21"/> <property name="param2" ref="helloOtherWorld"/> </bean> </beans>
  8. 8. Basic Java Configuration
  9. 9. Basic Usage // create and configure beans - XML ApplicationContext context = new ClassPathXmlApplicationContext("helloWorldConfig.xml"); // create and configure beans - Java-based ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class); // retrieve configured instance HelloWorld greeter = context.getBean("helloWorld", HelloWorld.class); // use configured instance String greeting = greeter.greet();
  10. 10. Naming - Each bean must have a unique identifier - If none provided in configuration - names are camel-cased - Derived similar to JSON convention: - objectProperty -> objectProperty - ObjectProperty -> objectProperty - OBJECTPROPERTY -> oBJECTPROPERTY
  11. 11. Beans - Essentially - POJO - But works with convoluted hierarchies - @Bean + @Configuration / @Component - @Lazy - @Scope - singleton, prototype, request, session - @Profile - …, …, …
  12. 12. Beans Wiring - @Autowired - Inferred - Can be advised - Constructor-based DI for mandatory dependencies - Usually - prefer this method - Setter-based DI for optional dependencies - Useful for cases of circular dependencies
  13. 13. AOP - Crosscutting concerns - Spring, logging, security, etc. - @Aspect - @Pointcut(“...”) - E.g.: "execution (* com.company.*.*(..)) - JoinPoint - @Before - @After, @AfterReturning, @AfterThrowing - @Around
  14. 14. MVC - Model - @Repository - View - @Controller + @ResponseBody - @RestController - @RequestMapping - controller and/or method - @RequestParam, @PathVariable, @RequestBody - Controller - @Service
  15. 15. MVC Flow
  16. 16. Data - JDBC abstractions - Programmatic and declarative transactions - @Transactional - ORM - Hibernate, JPA, JDO - OXM - JAXB, Castor, XMLBeans, JiBX, XStream - JMS
  17. 17. Testing - Run application with a different context - Bean mocks - Alternate implementations - Compatible with standard runners - @RunWith(...) - E.g.: SpringJUnit4ClassRunner.class - @ContextConfiguration(...) - Pass the requested context configurations
  18. 18. Features Configuration - XML: - Context: <context:annotation-config/> - Component scan: <context:component-scan base-package=”...”/> - AOP: <aop:aspectj-autoproxy/> - Transactions: <tx:annotation-driven/> - MVC: <mvc:annotation-driven/> - Beans can be configured manually in XML - Lots of hard (and brittle) work...
  19. 19. Features Configuration - Java-based: - Configuration: @Configuration - Context: @AnnotationDrivenConfig - Component scan: @ComponentScan(“...”) - AOP: @EnableAspectJAutoProxy - Transactions: @EnableTransactionManagement - MVC: @EnableWebMvc
  20. 20. Spring Boot - start.spring.io - Just choose what you need and go - Config automatically according to libraries in classpath - @SpringBootApplication - @Configuration - @EnableAutoConfiguration - @ComponentScan
  21. 21. “You can generally trust Spring to do the right thing.”
  22. 22. Additional Resources ●http://springtutorials.com/spring-ecosystem ●http://start.spring.io ●https://spring.io/projects ●https://spring.io/guides ●http://docs.spring.io/spring/docs/current/sprin g-framework-reference/htmlsingle

×