@Autowired
- 可以使用@Autowired注解那些众所周知的解析依赖性接口,比如:BeanFactory,ApplicationContext,Environment,ResourceLoader,ApplicationEventPublisher,and MessageSource
- 可以通过添加注解给需要该类型的数组的字段或方法,以提供ApplicationContext中的所有特定类型的bean
- 可以用于装配key为String的Map
- 如果希望数组有序,可以让bean实现org.springframework.core.Ordered接口或使用的@Order注解
@Autowired使用时需要注意的点
- @Autowired是由Spring BeanPostProcessor处理的,所以不能在自己的BeanPostProcessor或BeanFactoryPostProcessor类型应用这些注解,这些类型必须通过XML或者Spring的@Bean注解加载
数组及Map的自动注入
- 例子:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1f029ba527991e2782d9633bae0da708.png)
package com.torey.beanannotation.multibean;
public interface BeanInterface {
}
package com.torey.beanannotation.multibean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(value = 2)
@Component
public class BeanInterfaceOne implements BeanInterface {
}
package com.torey.beanannotation.multibean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(value = 1)
@Component
public class BeanInterfaceTwo implements BeanInterface {
}
package com.torey.beanannotation.multibean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class BeanInvoker {
@Autowired
private List<BeanInterface> list;
@Autowired
private Map<String,BeanInterface> map;
public void say(){
if (null!=list&&0!=list.size()) {
System.out.println("list===");
for (BeanInterface bean : list) {
System.out.println(bean.getClass().getName());
}
}else {
System.out.println("List<BeanInterface> 没有被注入");
}
System.out.println("map===");
if (null!=map&&0!=map.size()) {
for (Map.Entry<String, BeanInterface> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue().getClass().getName());
}
}else {
System.out.println("map 没有被注入");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.torey.beanannotation"/>
</beans>
package com.torey.springtest.bean;
import com.torey.beanannotation.injection.service.InjectionService;
import com.torey.beanannotation.multibean.BeanInvoker;
import com.torey.springtest.base.UnitTestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase {
public TestBeanAnnotation(){
super("classpath:spring-beanannotation.xml");
}
@Test
public void testMultBean(){
BeanInvoker invoker= (BeanInvoker) super.getBean("beanInvoker");
invoker.say();
}
}
根据慕课网 moocer老师的spring课程 整理