读书人

[IoC]六 详解@Autowired、@Qualifier和

发布时间: 2012-12-19 14:13:14 作者: rapoo

[IoC]6 详解@Autowired、@Qualifier和@Required

A、@Autowired

org.springframework.beans.factory.annotation.Autowired

public @interface Autowired

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.

标注一个构造函数,字段,setter方法或者配置方法,让它通过spring的依赖注入方法自动装填。

?

Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.

任何一个给定的bean类只能有一个构造函数可以携带这个注释,表示这个类作为一个spring的bean使用时,这个构造器将用于自动装配。这个构造函数不是必须是public。

?

Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.

在一个bean创建之后,任何方法被调用之前,这个字段被注入。这个字段不是必须是public。

?

Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.

配置方法它可以是任意名称和有任意多个参数。这个方法的任何一个参数都将使用spring容器中匹配的bean来自动装填。Bean属性的setter方法也是有效的,它是普通的配置方法的一个特例。配置方法不是必须是public。

?

In the case of multiple argument methods, the 'required' parameter is applicable for all arguments.

当有多个参数的方法情况下,'required'将适用于所有参数。

?

In case of a Collection or Map dependency type, the container will autowire all beans matching the declared value type. In case of a Map, the keys must be declared as type String and will be resolved to the corresponding bean names.

Collection或Map依赖类型情况,容器将自动装配bean匹配声明的值类型,这个Map的key必须是String,Map的values必须是已知的类型。

?

Declares whether the annotated dependency is required.

Defaults to true.

声明这个注释依赖是否需要,默认是true。

public class Foo { @Autowired private Bar[] bars;}?

<beans> <context:annotation-config/> <bean id="foo" /> <bean id="bar1" value="bar1" /> </bean> <bean id="bar2" value="bar2" /> </bean></beans>?

Foo foo = (Foo) ctx.getBean("foo");Bar[] bars=foo.getBars();System.out.println(bars.length);

@Autowired(required=false)private Bar bar;

org.springframework.beans.factory.annotation.Qualifier

public @interface Qualifier

This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.

@Qualifier用于注释一个字段或参数,当自动绑定时它作为候选bean的限定器。它也可以用于自定义的限定器注释。

?

public class Foo { @Autowired @Qualifier(value="bar1") private Bar bar;}?

<beans> <context:annotation-config/> <bean id="foo" /> <bean value="bar2" /> </bean> <bean id="bar1" value="bar1" /> </bean></beans>?

org.springframework.beans.factory.annotation.Required

public @interface Required

Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value.

标注一个方法(通常是一个JavaBean的setter方法)是@Required,也就是,这个setter方法必须定义为通过一个值来依赖注入。

读书人网 >编程

热点推荐