WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Basic · question 16 of 100

Explain the use of the @Autowired annotation in Spring.?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

The ‘@Autowired‘ annotation in the Spring Framework is used to automatically wire beans or components together with their required dependencies. It marks a constructor, method, or field in a class to be autowired with a Spring bean, eliminating the need for XML configuration files or manual dependency injection implementations.

When ‘@Autowired‘ is used in a constructor, Spring automatically injects a suitable bean instance as a constructor argument, resolving the dependency by type. For example:

@Service
public class MyService {

    private final MyRepository myRepository;

    @Autowired
    public MyService(MyRepository myRepository) {
        this.myRepository = myRepository;
    }

    // ...
}

In this example, the ‘@Autowired‘ annotation is used on the constructor of the ‘MyService‘ class to inject an instance of ‘MyRepository‘. Spring will automatically find a suitable bean of type ‘MyRepository‘ and pass it as a constructor argument.

If two beans of the same type are available, Spring will look for a primary bean or a bean with a specific name using the ‘@Qualifier‘ annotation. For example:

@Service
public class MyService {

    private final MyRepository primaryRepository;
    private final MyRepository secondaryRepository;

    @Autowired
    public MyService(@Qualifier("primaryMyRepository") MyRepository primaryRepository, 
                     @Qualifier("secondaryMyRepository") MyRepository secondaryRepository) {
        this.primaryRepository = primaryRepository;
        this.secondaryRepository = secondaryRepository;
    }

    // ...
}

In this case, two beans of type ‘MyRepository‘ are available, but Spring will look for beans named "primaryMyRepository" and "secondaryMyRepository" using the ‘@Qualifier‘ annotation.

‘@Autowired‘ can also be used on a field or a method:

@Service
public class MyService {

    @Autowired
    private MyRepository myRepository;

    // ...

    @Autowired
    public void setMyRepository(MyRepository myRepository) {
        this.myRepository = myRepository;
    }

    // ...
}

Both of these approaches will inject an instance of ‘MyRepository‘ into the ‘MyService‘ bean, but constructor injection is generally recommended for better readability and immutable dependencies.

The ‘@Autowired‘ annotation can also be used together with other annotations, such as ‘@Transactional‘ or ‘@Value‘, to specify additional behavior for the dependency injection.

In summary, the ‘@Autowired‘ annotation in the Spring Framework is a powerful tool for easily managing dependencies between beans and components, allowing for better decoupling and simplified configuration.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics