Spring 3 Annotation: @Autowired and @Inject
@Autowired and @Inject
As expected, you can apply the @Autowired annotation to “traditional” setter methods. You can also apply the annotation to methods with arbitrary names and/or multiple arguments. You can apply @Autowired to constructors and fields. It is also possible to provide all beans of a particular type from the ApplicationContext by adding the annotation to a field or method that expects an array of that type. The same applies for typed collections. Even typed Maps can be autowired as long as the expected key type is String. The Map values will contain all beans of the expected type, and the keys will contain the corresponding bean names.
You can also use @Autowired for interfaces that are well-known resolvable dependencies: BeanFactory, ApplicationContext, ResourceLoader, ApplicationEventPublisher, and MessageSource. These interfaces and their extended interfaces, such as ConfigurableApplicationContext or ResourcePatternResolver, are automatically resolved, with no special setup necessary.
JSR 330’s @Inject annotation can be used in place of Spring’s @Autowired in the examples below. @Inject does not have a required property unlike Spring’s @Autowire annotation which has a required property to indicate if the value being injected is optional. This behavior is enabled automatically if you have the JSR 330 JAR on the classpath.