Each object defines its dependencies only through constructor
arguments, arguments to a factory method, or properties that
are set on the object instance after it is constructed or
returned from a factory method.
Applications are built from objects that work together
An object depends on other objects
An object does not know how to construct or locate the objects it
depends on
When an object is created, its dependencies are injected to it
Build enterprise-ready Java applications
Define building blocks
Beans are objects that are instantiated, assembled and managed by
a container
Bean definitions are recipes for creating beans
Containers look at recipes and create beans
Tell the container how to instantiate, configure and assemble
beans
XML-based
<beans ...>
<bean ...>
</bean>
</beans>
Annotation-based
@Component
...
Java-based
@Configuration
...
Singleton (default): only one
Prototype: one per injection (or per getBean()
invocation)
Request: one per HTTP request
Session: one per HTTP session
Indicates that the class contains bean definitions
@Configuration
public class AppConfig {
}
Indicates that the method instantiates, configures and initialises
a new object to be managed by the container
@Configuration
public class AppConfig {
@Bean
public A a() {
return new A();
}
}
Configures component scanning
@Configuration
@ComponentScan(basePackages = "org.afdemp")
public class AppConfig {
...
}
Indicates that the class is a component
@Component
public class A {
...
}
Indicates that the class is a repository (e.g. a DAO)
@Repository
public class A {
...
}
Indicates that the class is a service
@Service
public class A {
...
}
Marks a constructor, a field, a setter method or a config method
as to be autowired
@Component
public class A {
...
@Autowired
private B b;
...
}
Indicates the scope to use for the bean
@Component
@Scope("prototype")
public class A {
...
}
Build web applications
The DispatcherServlet dispatches requests to handlers
Indicates that the class is a controller
@Controller
public class AController {
...
}
Maps web requests onto specific handler classes and/or handler
methods
@Controller
@RequestMapping("/a")
public class AController {
@RequestMapping(path = "/b", method = RequestMethod.GET)
public ... b() {
...
}
}
Indicates that the method parameter should be bound to a web
request parameter
@Controller
public class AController {
@RequestMapping(path = "/b", method = RequestMethod.GET)
public ... b(@RequestParam("x") String x) {
...
}
}
Indicates that the method parameter should be bound to a web
request header
@Controller
public class AController {
@RequestMapping(path = "/b", method = RequestMethod.GET)
public ... b(@RequestHeader("Accept-Encoding") String encoding) {
...
}
}
Indicates that the method parameter should be bound to a URI
template variable
@Controller
public class AController {
@RequestMapping(path = "/b/{c}", method = RequestMethod.GET)
public ... b(@PathVariable String c) {
...
}
}
Indicates that the method return value should be bound to the web
response body
@Controller
public class AController {
@RequestMapping(path = "/b", method = RequestMethod.GET)
@ResponseBody
public String b() {
return "B";
}
}
Indicates that the controller implements a REST API, thus serving
only JSON, XML or custom MediaType content
@Controller + @ResponseBody
@RestController
public class AController {
@RequestMapping(path = "/b", method = RequestMethod.GET)
public String b() {
return "B";
}
}
Create standalone, production-grade Spring-based applications
with minimum fuss
Minimise configuration
Embed the servlet container (Tomcat, Jetty, Undertow)
Generate basic projects that use Spring Boot
Deployed @ https://start.spring.io
Can be accessed with web browsers (e.g. Google Chrome), through
IDEs (e.g. STS), or from the command-line (e.g. cURL)
Source code available @ https://github.com/spring-io/initializr