这也是一种设计模式吧,学习一下
@Autowired
一、可以使用@Autowired注解注入Spring提供的解析依赖性接口(ClassPathXmlApplicationContext、AnnotationApplicationContext、BeanFactory、Envieonment、ResourceLoader、ApplicatonEventPublisher、MessageSource)
二、可以对数组(集合)类型的属性进行注入,注入的是ApplicationContext中所有符合该类型或者是该类型的子类的(List<String>,则会把所有String类型的Bean注入List集合中)。
Key:Bean的Id。
Value:Bean的对象。
如果希望数组有序
1、Bean实现org.springframework.core.Ordered接口
2、@Order注解(只针对List)
InterfaceImplTwo添加@Order(value=1)
InterfaceImplOne添加@Order(value=2),先取出Two的实例,再取出One的实例。
@Autowired是由Spring BeanPostProcessor处理的,所以不能在配置类中使用它,也就是说要注入集合类型的属性,这些属性的值只能是通过xml或者在配置类中使用@Bean注解加载。
注入List<BeanInterface>代码:
public interface InjectInterface {
}
@Component
public class InterfaceImplOne implements InjectInterface {
}
@Component
public class InterfaceImplTwo implements InjectInterface {
}
@Configuration
@ComponentScan("injectList")
public class InterfaceConfig {
}
@Component(value="invoker")
public class InterfaceInvoker {
private List<InjectInterface> list;
@Autowired
public void setList(List<InjectInterface> list) {
this.list = list;
}
public void say(){
if(null!=list){
for (int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}else{
System.out.println("list为null");
}
}
}
@Test
public void test(){
ApplicationContext ac=new AnnotationConfigApplicationContext(InterfaceConfig.class);
InterfaceInvoker ii=ac.getBean("invoker",InterfaceInvoker.class);
ii.say();
}
结果:
injectList.InterfaceImplOne@39f46204
injectList.InterfaceImplTwo@5b4f1255
注入Map类型<String,InjectInterface>类型属性(和List相似)
代码:
@Component(value="invoker")
public class InterfaceInvoker {
private Map<String,InjectInterface> map;
@Autowired
public void setMap(Map<String, InjectInterface> map) {
this.map = map;
}
public void say(){
if(map!=null&&map.size()>0){
for (Entry<String,InjectInterface> ii: map.entrySet()) {
System.out.println(ii.getKey()+" "+ii.getValue());
}
}
}
}
order注解只针对list有效,对map无效。这个好像是和map的存储方式有关吧?记得map好像内部的存储位置是无序的?
这里面提到的接口,个人认为不是应该在扫描时已经完成实例化了么?为什么还要再类中作为变量加入注解呢?
@Autowired
是由Spring BeanPostProcessor处理的,所以不能在自己的BeanPostProcessor或BeanFactoryPostProcessor类型应用这些注解,这些类型必须通过XML或者Spring的@Bean注解加载
@Autowired
2.可以用于装配key为String的Map
3.如果希望数组有序,可以让bean实现org.springframework.core.Ordered 接口或使用的@Ordered注解
@Autowired
1.可以通过添加注解给需要该类型的数组的字段或方法,已提供ApplicationContext中的所有特定类型的bean
@Autowired,可以用于注解那些众所周知的解析依赖性接口,比如:BeanFactory,ApplicationContext,Environment,ResourceLoader,ApplicationWventPublisher,and MessageSource
@Autowired注解
该注解一般可以使用在setter方法、构造器和成员变量上。
不可以在BeanPostProcessor或者BeanFactoryPostProcessor类型使用@Autowired注解,因为@Autowired注解是由Spring中的BeamPostProcessor处理的,这些类型可以通过配置xml或者@Bean注解来加载。
@Autowired注解可以装配List或者Map,其中这些集合中数据为需要装配的对象,map中的String为Bean的id。
@Autowired-5
@Autowired-4
@Autowired-3
@autowired
可以通过Autowired为Set和key为String的map自动注入ApplicationContext中所有符合的。可以利用@Order注解使数组有序(只对list生效,map无效)。
@Order注解只对数组有效
@Autowired新用法
不能使用@Autowired的情况
autowired注解数组和map
@Autowired 注意事项
注解为 Map 类型的 bean 其 Key 的类型是 String
一、可以使用@Autowired注解注入Spring提供的解析依赖性接口(ClassPathXmlApplicationContext、AnnotationApplicationContext、BeanFactory、Envieonment、ResourceLoader、ApplicatonEventPublisher、MessageSource)
二、可以对数组(集合)类型的属性进行注入,注入的是ApplicationContext中所有符合该类型或者是该类型的子类的(List<String>,则会把所有String类型的Bean注入List集合中)。
Key:Bean的Id。
Value:Bean的对象。
如果希望数组有序
1、Bean实现org.springframework.core.Ordered接口
2、@Order注解(只针对List)
InterfaceImplTwo添加@Order(value=1)
InterfaceImplOne添加@Order(value=2),先取出Two的实例,再取出One的实例。
@Autowired是由Spring BeanPostProcessor处理的,所以不能在配置类中使用它,也就是说要注入集合类型的属性,这些属性的值只能是通过xml或者在配置类中使用@Bean注解加载。
注入List<BeanInterface>代码:
public interface InjectInterface {
}
@Component
public class InterfaceImplOne implements InjectInterface {
}
@Component
public class InterfaceImplTwo implements InjectInterface {
}
@Configuration
@ComponentScan("injectList")
public class InterfaceConfig {
}
@Component(value="invoker")
public class InterfaceInvoker {
private List<InjectInterface> list;
@Autowired
public void setList(List<InjectInterface> list) {
this.list = list;
}
public void say(){
if(null!=list){
for (int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}else{
System.out.println("list为null");
}
}
}
@Test
public void test(){
ApplicationContext ac=new AnnotationConfigApplicationContext(InterfaceConfig.class);
InterfaceInvoker ii=ac.getBean("invoker",InterfaceInvoker.class);
ii.say();
}
结果:
injectList.InterfaceImplOne@39f46204
injectList.InterfaceImplTwo@5b4f1255
注入Map类型<String,InjectInterface>类型属性(和List相似)
代码:
@Component(value="invoker")
public class InterfaceInvoker {
private Map<String,InjectInterface> map;
@Autowired
public void setMap(Map<String, InjectInterface> map) {
this.map = map;
}
public void say(){
if(map!=null&&map.size()>0){
for (Entry<String,InjectInterface> ii: map.entrySet()) {
System.out.println(ii.getKey()+" "+ii.getValue());
}
}
}
}
@Autowired由Spring BeanPostProcessor处理,所以不能在自己的BeanPostProcessor或BeanFactoryPostProcessor中应用@Autowired注解,否则会产生循环依赖
@Autowired注解可用于注解
@Autowired的注意事项
@Autowired在数组等集合类中的使用
使用@Autowired注解,给数组的字段或者方法注入依赖
@Autowired注解 是由 Spring 提供的。
@Autowired解析众所周知的依赖性接口。
Autowired使用注意事项
Autowired使用方法2,通过给数组自动装配,可把当前容器中所有该类型的bean添加到数组中
若是map,key(String)是beanid,value是指定的类型的实例
@order注解可以指定一个参数,参数的意义是该实例在数组中的顺序(不是索引值)