Spring入门总结(三)Autowired的配置的补充

上一篇讲到了Autowired注解List

这篇作为注解Map的一个补充

先看:

@Autowired
    private Map<String, BeanInterface> map;

当map被注解了之后,map的key和value又会是什么呢?

看下面一个案列:
申明一个接口:

package multibean;

public interface BeanInterface {

}

接口实现一: 

package multibean;

import org.springframework.stereotype.Component;

@Component
public class BeanImplOne implements BeanInterface {

}

接口实现二:

package multibean;

import org.springframework.stereotype.Component;

@Component
public class BeanImplTwo implements BeanInterface {

}

接口调用者:

package multibean;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BeanInvoker {
	@Autowired
	private List<BeanInterface> list;
	@Autowired
	private Map<String, BeanInterface> map;
	public void say() {
		if(list!=null && list.size()>0) {
			System.out.println("list : ");
			for(BeanInterface bean : list) {
				System.out.println(bean.getClass().getName());
			}
		}
		else System.out.println("The list is null!");
		
		if(map!=null && map.size() > 0) {
			System.out.println("map : ");
			for(Map.Entry<String, BeanInterface> entry : map.entrySet()) {
				System.out.println(entry.getKey() + "  " + entry.getValue().getClass().getName());
			}
		}
		else System.out.println("The map is null!");
	}
	
	
}

 下面是《单元测试》:不会的可以跳过,重点看结果。

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>

运行的结果是:

list : 
multibean.BeanImplOne
multibean.BeanImplTwo
map : 
beanImplOne  multibean.BeanImplOne
beanImplTwo  multibean.BeanImplTwo

这样看来,貌似key是bean的id value是bean的实例。

value是bean的实例很容易理解,但是key到底是不是bean的id呢?

接下来继续验证;

假设是bean的id,那么我们通过Component修改它的名字时候,输出一定会变。

于是将实现接口二改为

ackage multibean;

import org.springframework.stereotype.Component;

@Component("dfs")
public class BeanImplTwo implements BeanInterface {

}

最后输出的结果是: 

list : 
multibean.BeanImplOne
multibean.BeanImplTwo
map : 
beanImplOne  multibean.BeanImplOne
dfs  multibean.BeanImplTwo

结果显而易见,key正是bean的id。

猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/81461138