zoukankan      html  css  js  c++  java
  • spring 05-Spring框架依赖集合注入

    实现普通数组的注入

    定义一个Dept类

    package cn.liang.vo;
    import java.util.List;
    public class Dept {
    	private Integer deptno ;
    	private String dname ; 
    	private String loc ;
    	private List<String> group;
    	
    	public List<String> getGroup() {
    		return group;
    	}
    
    	public void setGroup(List<String> group) {
    		this.group = group;
    	}
    
    	public Integer getDeptno() {
    		return deptno;
    	}
    
    	public void setDeptno(Integer deptno) {
    		this.deptno = deptno;
    	}
    
    	public String getDname() {
    		return dname;
    	}
    
    	public void setDname(String dname) {
    		this.dname = dname;
    	}
    
    	public String getLoc() {
    		return loc;
    	}
    
    	public void setLoc(String loc) {
    		this.loc = loc;
    	}
    
    	@Override
    	public String toString() {
    		return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", group=" + group + "]";
    	}
    	
    }
    

    修改applicationContext.xml文件,进行Dept类对象的定义

    • 在Spring里面吧数组和List作为同等的对待
    • 使用array标签定义groups
    <bean id="dept" class="cn.liang.vo.Dept">
    	<property name="deptno" value="10"/>
    	<property name="dname" value="Development department"/>
    	<property name="loc" value="Guangzhou"/>
    	<property name="groups">
    		<array value-type="java.lang.String">
    			<value>groupOne</value>
    			<value>groupTwo</value>
    		</array>
    	</property>
    </bean>
    
    • 使用list标签定义groups
    <bean id="dept" class="cn.liang.vo.Dept">
    	<property name="deptno" value="10"/>
    	<property name="dname" value="Development department"/>
    	<property name="loc" value="Guangzhou"/>
    	<property name="groups">
    		<list value-type="java.lang.String">
    			<value>groupOne</value>
    			<value>groupTwo</value>
    		</list>
    	</property>
    </bean>
    

    测试程序

    package cn.liang.test;
    import org.apache.log4j.Logger;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.liang.service.IMessageService;
    import cn.liang.vo.Dept;
    import cn.liang.vo.Emp;
    import junit.framework.TestCase;
    public class TestMessageService2 {
    	private static ApplicationContext ctx = null ;
    	static {
    		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    	}
    	@Test
    	public void testDeptConstructor() {
    		Dept dept = (Dept) ctx.getBean("dept",Dept.class) ;
    		Logger.getLogger(TestMessageService.class).info(dept); 
     Logger.getLogger(TestMessageService.class).info(dept.getGroups().getClass().getName()); 
    	}
    }
    

    输出结果

    2018-11-29 17:01:45,747 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, groups=[groupOne, groupTwo]]
    2018-11-29 17:01:45,751 INFO [cn.liang.test.TestMessageService] - java.util.ArrayList
    

    实现set集合的注入

    定义一个Dept类

    private Set<String> groups;
    public Set<String> getGroups() {
    	return groups;
    }
    public void setGroups(Set<String> groups) {
    	this.groups = groups;
    }
    

    修改applicationContext.xml文件,进行Dept类对象的定义

    • 使用set标签定义groups
    <bean id="dept" class="cn.liang.vo.Dept">
    	<property name="deptno" value="10"/>
    	<property name="dname" value="Development department"/>
    	<property name="loc" value="Guangzhou"/>
    	<property name="groups">
    		<set value-type="java.lang.String">
    			<value>groupOne</value>
    			<value>groupTwo</value>
    		</set>
    	</property>
    </bean>
    

    测试程序testDeptConstructor,输出结果

    2018-11-29 17:03:56,386 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, groups=[groupOne, groupTwo]]
    2018-11-29 17:03:56,387 INFO [cn.liang.test.TestMessageService] - java.util.LinkedHashSet
    

    实现Map集合的注入

    定义一个Dept类

    private Map<Integer, String> groups;
    public Map<Integer, String> getGroups() {
    	return groups;
    }
    public void setGroups(Map<Integer, String> groups) {
    	this.groups = groups;
    }
    

    修改applicationContext.xml文件,进行Dept类对象的定义

    • 使用map标签定义groups
    <bean id="dept" class="cn.liang.vo.Dept">
    	<property name="deptno" value="10"/>
    	<property name="dname" value="Development department"/>
    	<property name="loc" value="Guangzhou"/>
    	<property name="groups">
    		<map>
    			<entry key="110" value="GroupsOne"/>
    			<entry key="120" value="GroupsTwo"/>
    		</map>
    	</property>
    </bean>
    

    测试程序testDeptConstructor,输出结果

    2018-11-29 17:13:21,272 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, groups={110=GroupsOne, 120=GroupsTwo}]
    2018-11-29 17:13:21,272 INFO [cn.liang.test.TestMessageService] - java.util.LinkedHashMap
    

    实现Properties的注入

    定义一个Dept类

    private Properties groups;
    public Properties getGroups() {
    	return groups;
    }
    public void setGroups(Properties groups) {
    	this.groups = groups;
    }
    

    修改applicationContext.xml文件,进行Dept类对象的定义

    • 使用props标签定义groups
    <bean id="dept" class="cn.liang.vo.Dept">
    	<property name="deptno" value="10"/>
    	<property name="dname" value="Development department"/>
    	<property name="loc" value="Guangzhou"/>
    	<property name="groups">
    		<props>
    			<prop key="110">GroupsOne</prop>
    			<prop key="120">GroupsTwo</prop>
    		</props>
    	</property>
    </bean>
    

    测试程序testDeptConstructor,输出结果

    2018-11-29 17:17:18,534 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, groups={110=GroupsOne, 120=GroupsTwo}]
    2018-11-29 17:17:18,535 INFO [cn.liang.test.TestMessageService] - java.util.Properties
    

    实现集合之间的引用注入

    定义一个Dept类

    package cn.liang.vo;
    import java.util.List;
    public class Dept {
    	private Integer deptno ;
    	private String dname ; 
    	private String loc ;
    	private List<Emp> emps ;
    	public void setEmps(List<Emp> emps) {
    		this.emps = emps;
    	}
    	public List<Emp> getEmps() {
    		return emps;
    	}
    
    	public Integer getDeptno() {
    		return deptno;
    	}
    
    	public void setDeptno(Integer deptno) {
    		this.deptno = deptno;
    	}
    
    	public String getDname() {
    		return dname;
    	}
    
    	public void setDname(String dname) {
    		this.dname = dname;
    	}
    
    	public String getLoc() {
    		return loc;
    	}
    
    	public void setLoc(String loc) {
    		this.loc = loc;
    	}
    	@Override
    	public String toString() {
    		return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", emps=" + emps + "]";
    	}
    }
    

    修改applicationContext.xml文件,进行Dept类对象的定义

    <bean id="emp" class="cn.liang.vo.Emp">
    	<property name="empno" value="5678"/>
    	<property name="ename" value="SMITH"/>
    	<property name="sal" value="800.00"/>
    </bean>
    <bean id="dept" class="cn.liang.vo.Dept">
    	<property name="deptno" value="10"/>
    	<property name="dname" value="Development department"/>
    	<property name="loc" value="Guangzhou"/>
    	<property name="emps">
    		<list>
    			<ref bean="emp"/>	<!-- 表示引用一个其它定义的bean -->
    			<!-- 描述的是一个内部的Bean,这个Bean只能够被这个Dept类定义使用 -->
    			<bean class="cn.liang.vo.Emp">
    				<property name="empno" value="1234"/>
    				<property name="ename" value="Liang"/>
    				<property name="sal" value="18000.00"/>
    			</bean>
    		</list>
    	</property>
    </bean>
    

    测试程序

    package cn.liang.test;
    import org.apache.log4j.Logger;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.liang.vo.Dept;
    import cn.liang.vo.Emp;
    import junit.framework.TestCase;
    public class TestMessageService2 {
    	private static ApplicationContext ctx = null ;
    	static {
    		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    	}
    	@Test
    	public void testDeptConstructor() {
    		Dept dept = (Dept) ctx.getBean("dept",Dept.class) ;
    		Logger.getLogger(TestMessageService.class).info(dept);
    		Logger.getLogger(TestMessageService.class).info(dept.getEmps().getClass().getName()); 
    	}
    }
    

    输出结果

    2018-11-29 17:29:11,596 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, emps=[Emp [empno=5678, ename=SMITH, sal=800.0, dept=null], Emp [empno=1234, ename=Liang, sal=18000.0, dept=null]]]
    2018-11-29 17:29:11,599 INFO [cn.liang.test.TestMessageService] - java.util.ArrayList
    
  • 相关阅读:
    算法导论--2.2分析算法
    C++对象模型
    算法导论--插入排序
    记一次Chrome冒充QQ浏览器领取奖励之行
    eclipse做界面开发
    eclipse jad 反编译 插件安装
    eclipse下web开发中缓存问题
    eclipse缓存问题
    No more “busy and acquire with NOWAIT”
    ora-00054:resource busy and acquire with nowait specified解决方法
  • 原文地址:https://www.cnblogs.com/liangjingfu/p/10039854.html
Copyright © 2011-2022 走看看