新手详细讲解:spring自动注入,将list、set、map集合当作一个bean元素看待,Junit测试
自动注入和之前的注入基本类型数据过程很相似,只不过是把数据集合当作一个bean元素看待。
1.首先看一下谷咕咕这次例子用到的项目结构
用到SomeValue.java,TestCase2.java,BascValue.xml这三个文件,其他的文件和这次不想关
2.SomeValue.java
这里用了多种不同类型的数据演示。同时添加get,set,toSting方法
package Value;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class BasicValue {
private String name;
private int age;
private List<String> city;
private Set<String> hobby;
private Map<String,Double> map;
private Properties properties;
public BasicValue() {
System.out.println("BasicValue()");
}
public void setProperties(Properties properties) {
this.properties=properties;
}
public void setMap(Map<String,Double> map) {
this.map=map;
}
public void setCity(List<String> city) {
this.city=city;
}
public void setHobby(Set<String> hobby) {
this.hobby=hobby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "BasicValue [name=" + name + ", age=" + age +",city"+city+ ",hobby"+hobby+",map"+map+",properties"+properties+"]";
}
}
3.BascValue.xml
然后我在BascValue.xml申明bean
id=“bas2” 一会再TestCase2.java中要对应他的id
class=“Value.BasicValue”对应BasicValue.java的包名+类名,因为我们是对BasicValue注入
property标签中的属性ref中的值对应不同util的id值,达到一一注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<util:set id="hobby2">
<value>小吃</value>
<value>看书</value>
<value>羽毛球</value>
<value>逛街</value>
</util:set>
<util:map id="mapBasic">
<entry key="金牌" value="105"></entry>
<entry key="银牌" value="105"></entry>
<entry key="铜牌" value="105"></entry>
</util:map>
<util:properties id="properBasic">
<prop key="admin">root</prop>
<prop key="password">123</prop>
</util:properties>
<bean id="bas2" class="Value.BasicValue">
<property name="hobby" ref="hobby2"></property>
<property name="map" ref="mapBasic"></property>
<property name="properties" ref="properBasic"></property>
</bean>
</bean>
4.TestCase2.java
然后我们写一个测试类来测试。
看到这么多代码别怕,应为有用的就Test5()
因为谷咕咕也在学习,所以懒了就没有删除其他的测试类,抱歉啦!
要是想看清把其他的Test()删掉也可以。
package test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import IOC2.A;
import IOC2.Rest;
import SpringScope.HelloBean;
import SpringScope.PsersonBean;
import Value.BasicValue;
import Value.Page;
import Value.SomeValue;
import Value.SomeValue2;
import Web.UserServlet;
public class TestCase2 {
private ApplicationContext ac;
private final static String XML="BasicValue.xml";
@Before
public void init() {
// ApplicationContext
ac=new ClassPathXmlApplicationContext(XML);
}
/**
* 测试自动装配
*/
// @Test
public void test1() {
Rest r=ac.getBean("rest",Rest.class);
System.out.println(r);
}
// @Test
public void test2() {
BasicValue basic=ac.getBean("basic",BasicValue.class);
System.out.println(basic);
}
// @Test
public void test3() {
SomeValue some=ac.getBean("some", SomeValue.class);
System.out.println(some);
}
// @Test 测试引用凡是的注入
public void test4() {
BasicValue basic2=ac.getBean("bas",BasicValue.class);
System.out.println(basic2);
}
@Test
public void test5() {
BasicValue basic3=ac.getBean("bas2",BasicValue.class);
System.out.println(basic3);
}
// @Test
public void test6() {
SomeValue2 some2=ac.getBean("some2",SomeValue2.class);
System.out.println(some2);
}
/**
* 老师的作业读取config。properties中的page=3
*/
@Test
public void test7() {
Page page=ac.getBean("page2",Page.class);
System.out.println(page);
}
}
通过截图我们看到数据已经注入,有些是null 的是因为谷咕咕没有注入值
只要看谷咕咕画圈的就可以了,其他的是谷咕咕写的其他的bean在spring启动时创建是写的无参构造器中输出的,便于自己学习。