Home > Uncategorized > Spring 3 Annotation: @Autowired and @Inject

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.

Reference: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: