先看如下:
@Autowired
private List<BeanInterface> list;
这是一个类的成员变量,当被@Autowired应用后,代表该list会自动去找Beaninterface相关的bean并加载进来。
通过如下案例便知。
申明一个接口:
package multibean;
public interface BeanInterface {
}
接口实现类1:
package multibean;
import org.springframework.stereotype.Component;
@Component
public class BeanImplOne implements BeanInterface {
}
接口实现类2:
package multibean;
import org.springframework.stereotype.Component;
@Component
public class BeanImplTwo implements BeanInterface {
}
一个调用者:
package multibean;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanInvoker {
@Autowired
private List<BeanInterface> list;
public void say() {
if(list!=null) {
for(BeanInterface bean : list) {
System.out.println(bean.getClass().getName());
}
}
else System.out.println("The list is null!");
}
}
下面我用的是《单元测试》的方式启动的,可以google一下,你也可以不用改方法。
package com.tutorialspoint;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
public class UnitTestBase {
private ClassPathXmlApplicationContext context;
private String springXmlpath;
public UnitTestBase() {}
public UnitTestBase(String springXmlpath) {
this.springXmlpath = springXmlpath;
}
@Before
public void before() {
if (StringUtils.isEmpty(springXmlpath)) {
//classpath相当于src
springXmlpath = "classpath*:spring-*.xml";
}
try {
//[,\\s]正则表达式,意思是以,和空格,tab作为分隔符,分成字符串数组
context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
context.start();
} catch (BeansException e) {
e.printStackTrace();
}
}
@After
public void after() {
context.destroy();
// System.out.println("after");
}
@SuppressWarnings("unchecked")
protected <T extends Object> T getBean(String beanId) {
try {
return (T)context.getBean(beanId);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
protected <T extends Object> T getBean(Class<T> clazz) {
try {
return context.getBean(clazz);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
}
package com.tutorialspoint;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import multibean.BeanInvoker;
import services.InjectionService;
import services.InjectionServiceImpl;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase{
public TestInjection() {
super("classpath:spring-injection.xml");
// TODO Auto-generated constructor stub
}
@Test
public void test() throws IOException {
// InjectionService service = super.getBean("injectionServiceImpl");
InjectionService service = super.getBean(InjectionServiceImpl.class);
service.save("Hello MVC");
}
@Test
public void testMultiBean() {
//方式一,通过获得类
// BeanInvoker invoker = super.getBean(BeanInvoker.class);
//方式二,通过获得Bean的名字
BeanInvoker invoker = super.getBean(BeanInvoker.class);
invoker.say();
}
}
xml:配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.tutorialspoint,dao,services,multibean"></context:component-scan>
<!-- <bean id="beanAnnotation" class="com.tutorialspoint.BeanAnnotation"></bean>-->
</beans>
执行结果是:
multibean.BeanImplOne
multibean.BeanImplTwo
为了验证最初说的正确性,我们把BeanImplTwo的Component注释掉,则运行结果是:
multibean.BeanImplOne
说明@Autowired注解会自动装配泛型里面相关的bean。