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
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. 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
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
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. 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...
20. Spring Boot
- start.spring.io
- Just choose what you need and go
- Config automatically according to libraries in
classpath
- @SpringBootApplication
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan