Video in progress
What is a bean? How is it different from a regular Java object?
A bean is an object that the Spring container creates and manages. The class itself is ordinary; the difference is that Spring creates it, wires it and tracks its lifecycle.
📖 Answer
In code there's no difference — a bean is also an object created from a regular class. The only difference is who created it. If you write new Foo(), that's a plain object and Spring knows nothing about it. If Spring creates it through @Component or @Bean, it's a bean.
Because it's a bean:
- its dependencies get injected;
@Transactional,@Async,@Cacheablework (through a proxy);@PostConstruct/@PreDestroyget called;- it has a scope (default is singleton, one instance per context).
A mistake juniors make a lot:
OrderService s = new OrderService(); // Spring doesn't know about it
s.save(order); // @Transactional doesn't work, the @Autowired fields inside are null
Don't new a class that Spring manages yourself — get it injected.
Sign up to mark studied questions and save your progress.