22
Spring “Less” is More Based on the official reference docs (v4.3.2)

Spring Framework Essentials

Embed Size (px)

Citation preview

Page 1: Spring Framework Essentials

Spring“Less” is More

Based on the official reference docs (v4.3.2)

Page 2: Spring Framework Essentials

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

Page 3: Spring Framework Essentials

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

Page 4: Spring Framework Essentials

Spring Projects

Page 5: Spring Framework Essentials

Spring Framework

Page 6: Spring Framework Essentials

Bootstrapping- Configuration

- XML- Java-based

- IoC container init- ApplicationContext

- Wiring- Run-time factory

Page 7: Spring Framework Essentials

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>

Page 8: Spring Framework Essentials

Basic Java Configuration

Page 9: Spring Framework Essentials

Basic Usage// create and configure beans - XMLApplicationContext context = new ClassPathXmlApplicationContext("helloWorldConfig.xml");

// create and configure beans - Java-basedApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);

// retrieve configured instanceHelloWorld greeter = context.getBean("helloWorld", HelloWorld.class);

// use configured instanceString greeting = greeter.greet();

Page 10: Spring Framework Essentials

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- OBJECT_PROPERTY -> object_PROPERTY

Page 11: Spring Framework Essentials

Beans- Essentially - POJO

- But works with convoluted hierarchies- @Bean + @Configuration / @Component- @Lazy- @Scope

- singleton, prototype, request, session- @Profile- …, …, …

Page 12: Spring Framework Essentials

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

Page 13: Spring Framework Essentials

AOP- Crosscutting concerns

- Spring, logging, security, etc.- @Aspect- @Pointcut(“...”)

- E.g.: "execution (* com.company.*.*(..))- JoinPoint

- @Before- @After, @AfterReturning, @AfterThrowing- @Around

Page 14: Spring Framework Essentials

MVC- Model

- @Repository- View

- @Controller + @ResponseBody- @RestController

- @RequestMapping - controller and/or method- @RequestParam, @PathVariable, @RequestBody

- Controller- @Service

Page 15: Spring Framework Essentials

MVC Flow

Page 16: Spring Framework Essentials

Data- JDBC abstractions- Programmatic and declarative transactions

- @Transactional- ORM

- Hibernate, JPA, JDO- OXM

- JAXB, Castor, XMLBeans, JiBX, XStream- JMS

Page 17: Spring Framework Essentials

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

Page 18: Spring Framework Essentials

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...

Page 19: Spring Framework Essentials

Features Configuration- Java-based:

- Configuration: @Configuration- Context: @AnnotationDrivenConfig- Component scan: @ComponentScan(“...”)- AOP: @EnableAspectJAutoProxy- Transactions: @EnableTransactionManagement- MVC: @EnableWebMvc

Page 20: Spring Framework Essentials

Spring Boot- start.spring.io

- Just choose what you need and go- Config automatically according to libraries in

classpath- @SpringBootApplication

- @Configuration- @EnableAutoConfiguration- @ComponentScan

Page 21: Spring Framework Essentials

“You can generally trust Springto do the right thing.”

Page 22: Spring Framework Essentials

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/spri

ng-framework-reference/htmlsingle