Spring
“Less” is More
Based on the official reference docs (v4.3.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
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
Spring Projects
Spring Framework
Bootstrapping
- Configuration
- XML
- Java-based
- IoC container init
- ApplicationContext
- Wiring
- Run-time factory
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>
Basic Java Configuration
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();
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
Beans
- Essentially - POJO
- But works with convoluted hierarchies
- @Bean + @Configuration / @Component
- @Lazy
- @Scope
- singleton, prototype, request, session
- @Profile
- …, …, …
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
AOP
- Crosscutting concerns
- Spring, logging, security, etc.
- @Aspect
- @Pointcut(“...”)
- E.g.: "execution (* com.company.*.*(..))
- JoinPoint
- @Before
- @After, @AfterReturning, @AfterThrowing
- @Around
MVC
- Model
- @Repository
- View
- @Controller + @ResponseBody
- @RestController
- @RequestMapping - controller and/or method
- @RequestParam, @PathVariable, @RequestBody
- Controller
- @Service
MVC Flow
Data
- JDBC abstractions
- Programmatic and declarative transactions
- @Transactional
- ORM
- Hibernate, JPA, JDO
- OXM
- JAXB, Castor, XMLBeans, JiBX, XStream
- JMS
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
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...
Features Configuration
- Java-based:
- Configuration: @Configuration
- Context: @AnnotationDrivenConfig
- Component scan: @ComponentScan(“...”)
- AOP: @EnableAspectJAutoProxy
- Transactions: @EnableTransactionManagement
- MVC: @EnableWebMvc
Spring Boot
- start.spring.io
- Just choose what you need and go
- Config automatically according to libraries in
classpath
- @SpringBootApplication
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
“You can generally trust Spring
to do the right thing.”
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

Spring Framework Essentials

  • 1.
    Spring “Less” is More Basedon the official reference docs (v4.3.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.
    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.
  • 5.
  • 6.
    Bootstrapping - Configuration - XML -Java-based - IoC container init - ApplicationContext - Wiring - Run-time factory
  • 7.
    Basic XML Configuration <?xmlversion="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.
  • 9.
    Basic Usage // createand 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.
    Naming - Each beanmust 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.
    Beans - Essentially -POJO - But works with convoluted hierarchies - @Bean + @Configuration / @Component - @Lazy - @Scope - singleton, prototype, request, session - @Profile - …, …, …
  • 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.
    AOP - Crosscutting concerns -Spring, logging, security, etc. - @Aspect - @Pointcut(“...”) - E.g.: "execution (* com.company.*.*(..)) - JoinPoint - @Before - @After, @AfterReturning, @AfterThrowing - @Around
  • 14.
    MVC - Model - @Repository -View - @Controller + @ResponseBody - @RestController - @RequestMapping - controller and/or method - @RequestParam, @PathVariable, @RequestBody - Controller - @Service
  • 15.
  • 16.
    Data - JDBC abstractions -Programmatic and declarative transactions - @Transactional - ORM - Hibernate, JPA, JDO - OXM - JAXB, Castor, XMLBeans, JiBX, XStream - JMS
  • 17.
    Testing - Run applicationwith a different context - Bean mocks - Alternate implementations - Compatible with standard runners - @RunWith(...) - E.g.: SpringJUnit4ClassRunner.class - @ContextConfiguration(...) - Pass the requested context configurations
  • 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.
    Features Configuration - Java-based: -Configuration: @Configuration - Context: @AnnotationDrivenConfig - Component scan: @ComponentScan(“...”) - AOP: @EnableAspectJAutoProxy - Transactions: @EnableTransactionManagement - MVC: @EnableWebMvc
  • 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.
    “You can generallytrust Spring to do the right thing.”
  • 22.