`
endual
  • 浏览: 3507837 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring复习-自定义类注入,list set map自定义类注入

 
阅读更多

我们对类中的参数注入的时候,有的时候,注入的是我们自己定义的类,而不是java基本的类型。

如下

 

 

Home.java是我们自己定义的类

 

package com.endual.bean;

public class Home {

	private String address ;
	private int    phone  ;
	
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getPhone() {
		return phone;
	}
	public void setPhone(int phone) {
		this.phone = phone;
	}
	
	
	
}
 

People.java

 

package com.endual.bean;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class People {

	private Home home;
	private Home[] homes;
	private List<Home> lists;
	private Map<Integer,Home> maps;
	private Set<Home> sets;
	
	public Home getHome() {
		return home;
	}
	public void setHome(Home home) {
		this.home = home;
	}
	public Home[] getHomes() {
		return homes;
	}
	public void setHomes(Home[] homes) {
		this.homes = homes;
	}
	public List<Home> getLists() {
		return lists;
	}
	public void setLists(List<Home> lists) {
		this.lists = lists;
	}
	public Map<Integer, Home> getMaps() {
		return maps;
	}
	public void setMaps(Map<Integer, Home> maps) {
		this.maps = maps;
	}
	public Set<Home> getSets() {
		return sets;
	}
	public void setSets(Set<Home> sets) {
		this.sets = sets;
	}

	

	

}

 

 

配置文件的配置

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!-- - Middle tier application context definition for the image database. -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">




	<bean id="people" class="com.endual.bean.People">

		<property name="home">
			<bean id="people_home" class="com.endual.bean.Home">
				<property name="address" value="浙江工商大学people_home" />
				<property name="phone" value="123123123" />
			</bean>
		</property>

		<property name="homes">
			<list>
				<bean id="people_homes1" class="com.endual.bean.Home">
					<property name="address" value="浙江工商大学people_homes1" />
					<property name="phone" value="123123123" />
				</bean>

				<bean id="people_homes2" class="com.endual.bean.Home">
					<property name="address" value="浙江工商大学people_homes2" />
					<property name="phone" value="123123123" />
				</bean>

				<bean id="people_homes3" class="com.endual.bean.Home">
					<property name="address" value="浙江工商大学people_homes3" />
					<property name="phone" value="123123123" />
				</bean>
			</list>
		</property>
		
	<property name="lists">
			<list>
				<bean id="lists1" class="com.endual.bean.Home">
					<property name="address" value="浙江工商大学lists1" />
					<property name="phone" value="123123123" />
				</bean>

				<bean id="lists2" class="com.endual.bean.Home">
					<property name="address" value="浙江工商大学lists2" />
					<property name="phone" value="123123123" />
				</bean>

				<bean id="lists3" class="com.endual.bean.Home">
					<property name="address" value="浙江工商大学lists3" />
					<property name="phone" value="123123123" />
				</bean>
			</list>
		</property>
		
		
		<property name="maps">
		
			<map key-type="int">
			    <entry key="1">
			        <bean id="maps1" class="com.endual.bean.Home">
						<property name="address" value="浙江工商大学maps1" />
						<property name="phone" value="123123123" />
					</bean>
			    </entry>
			    
			    <entry key="2">
			        <bean id="maps2" class="com.endual.bean.Home">
						<property name="address" value="浙江工商大学maps1" />
						<property name="phone" value="123123123" />
					</bean>
			    </entry>
			    
			    <entry key="3">
			        <bean id="maps3" class="com.endual.bean.Home">
						<property name="address" value="浙江工商大学maps1" />
						<property name="phone" value="123123123" />
					</bean>
			    </entry>
			</map>
		</property>
		
		<property name="sets">
			  <set>
			    <bean id="set1" class="com.endual.bean.Home">
				   <property name="address" value="浙江工商大学set1" />
				   <property name="phone" value="123123123" />
			   </bean>
	
				<bean id="set2" class="com.endual.bean.Home">
					<property name="address" value="浙江工商大学set2" />
					<property name="phone" value="123123123" />
				</bean>
	
				<bean id="set3" class="com.endual.bean.Home">
					<property name="address" value="浙江工商大学set3" />
					<property name="phone" value="123123123" />
				</bean>
			  </set>
		</property>
	</bean>
</beans>
 

 

测试 

 

 

package com.endual.main;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.endual.bean.Home;
import com.endual.bean.People;

public class MainRun {

	public static void main(String[] args) {
//		ClassPathXmlApplicationContext factory=new ClassPathXmlApplicationContext("bean.xml");
		ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");
		People hello = (People)context.getBean("people");
	
		Home home = hello.getHome() ;
		System.out.println(home.getAddress());

		Home[] homes = hello.getHomes() ;
		System.out.println(homes[1].getAddress());
		
		List<Home> lists = hello.getLists() ;
		System.out.println(lists.get(0).getAddress());
		
		Map<Integer,Home> maps = hello.getMaps() ;
		System.out.println(maps.get(1).getAddress());
		
		Set<Home> sets = hello.getSets() ;
		System.out.println(sets.iterator().next().getAddress());
		
	}

}

 

 

打印结果:

 

 

浙江工商大学people_home
浙江工商大学people_homes2
浙江工商大学lists1
浙江工商大学maps1
浙江工商大学set1
 

 

 

感受:

感觉到,其他自定义类的书写和我们一般的是一样的,就是用bean作为我们的参数的一个整体

 

 

 

 

分享到:
评论

相关推荐

    List<Map>转化为List工具类

    一般使用springjdbc、hibernate的sql查询,库获取到的数据都是List&lt;Map, Object&gt;&gt;结果集,如果我们要转化为JavaBean,则需要做一系列的map.get(),然后obj.set()。 此工程中就是解决List&lt;Map, Object&gt;&gt;转化为...

    thymeleaf-extras-springsecurity-3.0-master.zip

    As with normal Spring EL expressions, Thymeleaf allows you to access a series of objects from them including the context variables map (the #vars object). In fact, you are allowed to surround your ...

    Spring_集合(List_Map_Set)_自动装配

    Spring实现集合(List_Map_Set)_自动装配,适合初学者熟悉集合类型的装配。

    spring-data-redis支持批量操作

    public List&lt;V&gt; pipelineGet(Set&lt;K&gt; keys){ return rt.opsForPipeline().get(keys); } public void pipelineSet(Map,V&gt; valueMap){ redisTemplate.opsForPipeline().set(valueMap); } public void ...

    一步步实现Spring框架(二)XML注入

    实现了XML注入Bean,为bean注入bean,构造注入,Map,List,Set,Property 注入

    Spring (bean怎样注入值)学习实例

    实例主要讲述了,Spring的Xml(list,set,map)怎样进行注入值

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    spring-redis-aop:redis客户端集成spring实现aop方式

    spring-redis-aop redis客户端集成spring实现aop方式,方便在程序中使用...所有注解过的方法是否使用缓存,在选择存储类型的时候,可以选择String或者Map形势(Set和List还没 有实现,可以自行实现)使用还是很方便的哈

    spring-batch-redis:Redis的Spring Batch扩展

    Spring Batch Redis 基于Redis的ItemReader和ItemWriter实现。 资料类型 Spring Batch Redis支持两种数据类型:密钥转储和密钥值。 KeyDump KeyDump存储密钥,以秒为单位的TTL及其二进制表示形式(字节数组)。 ...

    Spring的一些配置信息(date map and so on)

    Spring的一些配置信息(date map and so on) Spring Date Map List Set Properties 自己写了一个属性编辑器,主要对Date型数据进行操作

    spring.doc

    拓展spring为类中的属性赋值: 40 小结: 47 面向接口编程: 47 4 面向切面编程 52 4.1 代理模式 52 代理模式拓展: 52 4.1.1 JDK动态代理 58 JDK动态代理拓展: 59 4.1.2 CGLIB做代理 66 CGLIB动态代理拓展: 68 ...

    java面试宝典

    65、Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别 17 66、HashMap和Hashtable的区别 17 67、说出ArrayList,Vector, LinkedList的存储性能和特性 17 68、java中有几...

    Java代码实现依赖注入

    模仿Spring实现一种基于xml配置文件的依赖注入机制。文件中将实现3中注入,一是单值注入,包括int,float,double,char等,也...二是Java容器注入,包括List,Set,Map三种容器的注入,最后一种是java bean对象注入。

    05spring4_di.rar

    &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.spring...-- p命名空间注入属性依然要设置set方法 --&gt; 风清扬" p:age="230"/&gt; &lt;!--c命名空间注入要求有对应参数的构造方法 --&gt; &lt;/beans&gt;

    springDay1

    spring构造参数,set方法,类对象属性的注入,还有数组,list,map,properties对象的依赖注入。

    springmybatis

    public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserAge()...

    redis与spring的整合

    只是理解一下redis与spring整合的大概步骤,包括String,list,set,map格式的值

    spring 高性能 代码

    spring的良好的扩展性,集成度,IOC,AOP事务,已经是项目的基础条件. 整个项目只使用了spring 没有struts,没有hibernate //就极简而言,一个数据库只需要一个Service,就可以查询这个数据库的任意一张表 //以下是我的...

    动力节点老杜推荐Java学习路线

    学习Java集合框架,包括List、Set、Map等数据结构的使用和常见操作。 深入理解异常处理机制,学会使用try-catch语句和自定义异常。 学习Java的多线程编程,掌握线程的创建、同步和通信等技术。 学习Java的IO编程,...

    cms后台管理

    Jeecms是基于Spring注解,在自定义标签时对于实体类和dao service等注意注解的问题。 五 自定义标签及使用自己创建的表的实现过程 下面是我自己定义的标签mycontent_list 首先,在数据库里创建了一个jc_...

Global site tag (gtag.js) - Google Analytics