新手详细讲解:spring注入int、String、list、 set 、map数据,junit测试。

新手:spring注入基本类型数据

1.首先看一下谷咕咕这次例子用到的项目结构
用到SomeValue.java,TestCase2.java,BascValue.xml这三个文件,其他的文件和这次不想关。
新手详细讲解:spring注入int、String、list、 set 、map数据,junit测试。_第1张图片
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=“basic” 一会再TestCase2.java中要对应他的id
class=“Value.BasicValue”对应BasicValue.java的包名+类名,因为我们是对BasicValue注入


<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">
    		<bean id="basic" class="Value.BasicValue">
    		
    		<property name="name" value="张三">property>
    		<property name="age" value="23">property>
    		<property name="city">
    		<list>
    			<value>安庆value>
    			<value>连云港value>
    			<value>安庆value>
    			<value>安庆value>
    		list>
    		property>
    		<property name="hobby">
    		<set>
    			<value>小吃value>
    			<value>看书value>
    			<value>羽毛球value>
    			<value>逛街value>
    		set>
    		property>
    		<property name="map">
    		<map>
    			<entry key="金牌" value="105">entry>
    			<entry key="银牌" value="105">entry>
    			<entry key="铜牌" value="105">entry>
    		map>
    		property>
    		<property name="properties">
    		<props>
    		<prop key="admin">123prop>
    		<prop key="password">123prop>
    		props>
    		property>
    		bean>
bean>    		

4.TestCase2.java
然后我们写一个测试类来测试。
看到这么多代码别怕,应为有用的就Test2()
因为谷咕咕也在学习,所以懒了就没有删除其他的测试类,抱歉啦!
要是想看清把其他的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);
	}
	
	
	
	
}

最后我们看一下测试的结果
只要看谷咕咕画圈的就可以了,其他的是谷咕咕写的其他的bean在spring启动时创建是写的无参构造器中输出的,便于自己学习。
新手详细讲解:spring注入int、String、list、 set 、map数据,junit测试。_第2张图片
对了要说一下,这里谷咕咕用的事Junit测试
新手详细讲解:spring注入int、String、list、 set 、map数据,junit测试。_第3张图片
@Test怎么出现?
先打@然后再按alt+/键可以选择@Test
别嫌谷咕咕唠叨,我只是个菜鸟,记录一下。

你可能感兴趣的:(ssm)