Sobes.tech
Junior — Middle

Explain what the concept of a bean in Java is and how it is used within applications.

sobes.tech AI

Answer from AI

A Bean in Java is an object managed by a container, such as in Spring or Java EE. Beans typically represent application components with specific configuration and lifecycle. They are used for inversion of control (IoC) and dependency injection (DI), which simplifies application creation and testing.

Example of using a bean in Spring:

@Component
public class UserService {
    // business logic
}

// In another class
@Autowired
private UserService userService;

Here, UserService is a bean that is automatically created and injected by the container.