Junior — Middle
Can you explain what beans are in the context of Java EE or Spring?
sobes.tech AI
Answer from AI
In the context of Java EE and Spring, a "bean" is a managed object whose lifecycle is controlled by a container (such as the Spring IoC container or a Java EE container). Beans typically represent application components such as services, DAOs, controllers, etc.
In Spring, beans are created and configured through annotations (@Component, @Service, @Repository, @Controller) or XML configuration. The container is responsible for creating instances, dependency injection, and managing the lifecycle.
Example of a simple Spring bean:
@Component
public class UserService {
public void createUser(String name) {
System.out.println("User created: " + name);
}
}
Here, UserService is a bean that can be injected into other components via @Autowired.