[Spring] @Autowired, @Resource, @inject
Overview
In this Spring Framework tutorial, you’ll demonstrate how to use annotations related to dependency injection, namely the @Resource, @Inject, and @Autowired annotations. These annotations provide classes with a declarative way to resolve dependencies:
@Autowired
ArbitraryClass arbObject;
As opposed to instantiating them directly (the imperative way):
ArbitraryClass arbObject = new ArbitraryClass();
Two of the three annotations belong to the Java extension package: javax.annotation.Resource and javax.inject.Inject. The @Autowired annotation belongs to the org.springframework.beans.factory.annotation package.
Each of these annotations can resolve dependencies either by field injection or by setter injection. You will use a simplified, but practical example to demonstrate the distinction between the three annotations, based on the execution paths taken by each annotation.
The examples will focus on how to use the three injection annotations during integration testing. The dependency required by the test can either be an arbitrary file or an arbitrary class.
댓글남기기