注解 | 释义 | 说明 | 源码注解说明(包含部分) |
@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 |
@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 |
@Configurable[✘] | 可配置的 | 类级别的注解,与AspectJ注解驱动一起使用 | Marks a class as being eligible for Spring-driven configuration.Typically used with the AspectJ |
备忘,此测试中,只测试注解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();
}
}