SPRING CONCEPTS

BeanFactory and ApplicationContext

Spring Concepts / BeanFactory and ApplicationContext

BeanFactory is the basic bean-container contract; ApplicationContext extends it with application facilities.

Understand the idea

Stage 19 adds an Application information page. Spring injects its existing ApplicationContext into our controller. BeanFactory factory = context copies that reference; it creates no container. Both typed lookups return the same singleton ProductService and count 30 products. ApplicationContext also offers resource access: getResource locates the bundled product file. Use constructor injection for ordinary application dependencies; this page deliberately inspects container APIs. The distinction is not simply lazy versus eager creation.

A concrete example

BeanFactory factory = context;
factory.getBean(ProductService.class);

In the application

See the independently runnable examples in Stage 19.

Assigning an existing context to a BeanFactory variable does not create another container.

See it in a working stage

Related concepts

Read the official documentation ↗
Java 21 · Spring Boot 4.1.1 · Edupoly