– dependency injection (DI)
– aspect-oriented programming (AOP)
Springs fundamental missing is to simplify Java development:
– lightweight development with POJOs
– declarative programming with aspects and common conventions
– loose coupling with DI and interface orientation
– eliminate boilerplate code with aspects and templates
Keywords: POJO, Bean, Dependency Injection, Wiring, Loose coupling, Boilerplate code, Cross-cutting concerns
No need to implement or extend, POJOs (usually) have no indication of using Spring.
2. Dependency Injection
Dependency Injection makes code simpler, easier to understand and test. Object is not anymore responsible for referencing to other objects, dependencies are used for that.
Class is highly coupled to Task:
Task is injected (constructor injection) = loose coupling:
It doesn’t matter what kind of task our class gets as long as it implements Task interface.
Wiring = creating associations between application components (commonly via XML or annotations)
XML based configuration (ClassPathXmlApplicationContext):
or Java based configuration (AnnotationConfigApplicationContext):
Application context loads bean definitions and wires them together.
AOP enables capturing functionality that is used throughout the application in reusable components. E.g. logging, transactions management, security,… These system services are called cross-cutting concerns, because they cut across multiple components. This introduces two levels of complexity, business logic is separated and objects remain POJOs.
E.g. logging before and after a method execution, see elements:
Keywords: Advice, Pointcut,
Eliminating boilerplate code
Spring seeks to eliminate boilerplate code with templates. e.g. JdbcTemplate, SimpleJdbcTemplate
We configure beans in XML or Java files. In Spring object lives in containers. Container creates them, wires, configures,…
There is no single container, but they can be categorised in two distinct types:
– BeanFactory (often too low-level)
– ApplicationContext (preferred)
Some application contexts:
– load application context from XML
– load application context from Java class
…now you can retrieve beans with getBean() method
Beans life: you may want to take advantage of the opportunities, during it’s life, to customize how a bean is created.
– Core Container: Manages how the beans are created, configured, managed. All spring modules are build on top of Core Container.
– AOP Module: Serves as the basis for developing our own aspects
– Data Access: Abstracting away boilerplate code. Also, ORM tools e.g. Hibernate, JPA,…
– Web and Remoting: Building web applications where we separate UI logic from the application logic.
– Testing: Collection of mock object implementations for writing unit tests.