Mastering Spring 5.0
上QQ阅读APP看书,第一时间看更新

Container managed beans

Instead of a class creating its own dependencies, in the earlier example, we looked at how the Spring IoC container can take over the responsibility of managing beans and their dependencies. The beans that are managed by the container are called Container Managed Beans.

Delegating the creation and management of beans to the container has many advantages. Some of them are listed as follows:

  • Since classes are not responsible for creating dependencies, they are loosely coupled and testable. This leads to good design and fewer defects.
  • Since the container manages the beans, a few hooks around the beans can be introduced in a more generic way. Cross-cutting concerns, such as logging, caching, transaction management, and exception handling can be woven around these beans using Aspect-Oriented Programming (AOP). This leads to more maintainable code.