1. 程式人生 > >spring ioc---基於註解的配置(beans包@Autowired,@Qualifier,@Required,@Value)

spring ioc---基於註解的配置(beans包@Autowired,@Qualifier,@Required,@Value)

org.springframework.beans.factory.annotation包中的註解
註解 釋義 說明 原始碼註解說明(包含部分)
@Autowired 自動裝配

1,通常在屬性和setter方法上使用

2,註解值required,某些情況可取代@Required,預設值是true.

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities. This is an alternative to the JSR-330 javax.inject.Inject

annotation, adding required-vs-optional semantics.

@Qualifier

限定符

限定注入的bean值

This annotation may be used on a field or parameter as

a qualifier forcandidate beans when autowiring. It may

also be used to annotate other custom annotations that

can then in turn be used as qualifiers.

@Required 必需的

1,必須注入標註的依賴項

2,通常使用在setter方法上

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.

@Value 賦值

1,接收String型別,依據標註的型別自動轉換

2,標註的屬性可沒有setter方法

Annotation at the field or method/constructor parameter level that indicates a default value expression for the affected argument.
@Lookup[✘] 查詢 DI中方法注入依賴bean時使用,標記的預設值即依賴的bean名稱

An annotation that indicates 'lookup' methods, to be overridden by the container to redirect them back to the org.springframework.beans.factory.BeanFactory for a getBean call. This is essentially an annotation-based version of the XML lookup-method attribute, resulting in the same runtime arrangement.

@Configurable[✘] 可配置的 類級別的註解,與AspectJ註解驅動一起使用

Marks a class as being eligible for Spring-driven configuration.Typically used with the AspectJ AnnotationBeanConfigurerAspect.

備忘,此測試中,只測試註解autowired,qualifier和required,及value.


類物件

package siye;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("member")
public class MemberCls
{


	// 使用value,可以沒有setter方法.
	@Value("jack")
	private String name;
	@Value("34")
	private int age;
	@Value("0")
	private int sex;


	// autowired,qualifier和required的組合使用
	// 在屬性的級別上.
	@Autowired(required = true)
	@Qualifier("arrayList")
	private ArrayList<?> list;
	public void setList(ArrayList<?> list)
	{
		this.list = list;
	}
	public ArrayList<?> getList()
	{
		return list;
	}

	// autowired,qualifier和required的組合使用.
	// 在setter方法的級別上
	private String description;
	// @Qualifier("desc")
	@Autowired
	@Required
	public void setDescription(@Qualifier("desc") String description)
	{
		this.description = description;
	}
	public String getDescription()
	{
		return description;
	}


	@Override
	public String toString()
	{
		return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
}

配置類

package siye;
import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("siye")
public class ConfigClass
{
	@Bean("arrayList")
	@SuppressWarnings("serial")
	public ArrayList<?> getList()
	{
		return new ArrayList<Integer>()
		{
			{
				add(0);
				add(1);
				add(2);
			}
		};
	}


	@Bean("desc")
	public String getDesc()
	{
		return new String("this's_test_class");
	}
}

測試類

package siye;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class UnitTest
{
	public static void main(String[] args)
	{
		AnnotationConfigApplicationContext context =
				new AnnotationConfigApplicationContext();
		context.register(ConfigClass.class);
		context.refresh();

		MemberCls obj = context.getBean("member", MemberCls.class);
		// autowired,qualifier和required在屬性級別上
		System.out.println(obj.getList());
		// autowired,qualifier和required在setter方法級別上
		System.out.println(obj.getDescription());
		// 驗證value註解
		System.out.println(obj);

		context.close();
	}
}