Upload
edward-goikhman
View
63
Download
2
Embed Size (px)
Citation preview
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 - 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();
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
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 Springto 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/spri
ng-framework-reference/htmlsingle