Lesson 2 · Spring Core

Java Configuration

Defining beans in plain Java with @Configuration and @Bean — and the CGLIB proxy trick that makes it behave.

What you'll be tested on Where @Bean goes, how a bean gets its name, how beans depend on each other, how to combine several config classes — and the big one: why calling one @Bean method from another still returns the singleton. That last point is a near-guaranteed question.

Three ways to define beans

Spring reads bean definitions from three kinds of configuration metadata:

Use Java config when you don't own the class (e.g. a DataSource from a library) — you can't put @Component on someone else's code, but you can write a @Bean method that constructs it.

The basic shape

@Configuration
public class AppConfig {

  @Bean
  public AccountRepository accountRepository() {
    return new JdbcAccountRepository(dataSource());
  }

  @Bean
  public TransferService transferService() {
    // depend on another bean by calling its @Bean method
    return new TransferService(accountRepository());
  }

  @Bean
  public DataSource dataSource() { return ...; }
}
The bean's name is the method name By default the bean id is the @Bean method name — here transferService, accountRepository, dataSource. Override with @Bean("myName").

Two ways to wire dependencies between beans

1. Call the other @Bean method (as above): new TransferService(accountRepository()).

2. Declare it as a method parameter — Spring injects the matching bean as the argument:

@Bean
public TransferService transferService(AccountRepository repo) {
  return new TransferService(repo);   // repo is injected by type
}

Both are correct. Parameter injection is cleaner when the dependency lives in a different config class.

The proxy magic — the question everyone gets wrong

Look again at method 1. transferService() calls accountRepository(), and accountRepository() is also called on its own. In plain Java that's two separate JdbcAccountRepository objects. But beans are singletons — so which is it?

Answer: exactly one instance Because the class is @Configuration, Spring wraps it in a CGLIB proxy subclass. Every call to a @Bean method is intercepted: if the singleton already exists, the proxy returns the existing one instead of running the method again. So inter-bean calls honour singleton scope.
"Full" — @Configuration"Lite" — @Bean on a plain @Component
Class is proxied?Yes (CGLIB)No
Inter-bean method call returns…the shared singletona new object each call
Use whenbeans reference each other by method callsimple, independent factory methods
Consequence to remember A @Configuration class is proxied by CGLIB, so it must not be final, and neither can its @Bean methods — same CGLIB rule you met in the Container drill.

Combining multiple configuration classes

Real apps split config across several classes. Three ways to bring them together:

Getting a bean out of the context

ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

TransferService a = ctx.getBean(TransferService.class);          // by type
TransferService b = (TransferService) ctx.getBean("transferService"); // by name
TransferService c = ctx.getBean("transferService", TransferService.class); // by name + type
Primary source — read this
Spring Framework 5.3 — Core: Java-based Container Configuration

Read "Basic Concepts: @Bean and @Configuration" and "Using the @Configuration annotation" — the section on inter-bean dependencies is exactly the proxy point above.

Check yourself

Answer before you're sure. The proxy question is the one to nail.

Then drill the book In your practice book's Container section, the config-focused questions are around Q16, Q22–23 (@Import, @Configuration/@Bean, the @Profile+@Bean snippet). Do those now, plus re-run the Container Drill.
I'm your teacher — ask me anything. The "full vs lite" proxy distinction trips a lot of people up — if it's fuzzy, ask me to walk an example through it. Say "continue" for Lesson 3 (component scanning, stereotypes & @Autowired).
← Lesson 1 · IoC Container Lesson 3 · Component scanning → (coming next)