4.spring的各种IOC注入方式1——set注入

新建实体类com.briup.bean.Student

public class Student {
	private long id;
	private String name;
	private int age;
	//集合
	private List list;
	private Set set;
	private Map map;
	private Properties prop;
	
	public Student(long id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
		System.out.println("有参构造器");
	}
	public Student() {
		System.out.println("in Student()");
	}
	public void init(){
		System.out.println("in init() of Student");
	}
	public void end(){
		System.out.println("in end() of Student");
	}
	
	public void show(){
		System.out.println("--------------list------------");
		for(Object o:list){
			System.out.println(o);
		}
		System.out.println("--------------set------------");
		for(Object o:set){
			System.out.println(o);
		}
		System.out.println("--------------map------------");
		for(Object key:map.keySet()){
			System.out.println(key+" --> "+map.get(key));
		}
		System.out.println("--------------prop------------");
		for(Object key :prop.keySet()){
			System.out.println(key+" --> "+prop.get(key));
		}
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	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;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public Set getSet() {
		return set;
	}
	public void setSet(Set set) {
		this.set = set;
	}
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	public Properties getProp() {
		return prop;
	}
	public void setProp(Properties prop) {
		this.prop = prop;
	}
}

1.set注入

是通过走无参构造器,通过setXxx()方法去注入
set.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
	<!--写法一-->
	<bean name="stu1" class="com.briup.bean.Student">
		<property name="id" value="1"></property>
		<property name="name" value="tom"></property>
		<property name="age" value="20"></property>
	</bean>
	<!--写法二-->
	<bean name="stu2" class="com.briup.bean.Student">
		<property name="id">
			<value>1</value>
		</property>
		<property name="name">
			<value>jack</value>
		</property>
		<property name="age">
			<value>20</value>
		</property>
	</bean>
</beans>

在测试类中,用junit测试

	@Test
	public void ioc_set() {
		try {
			String[] path = {"com/briup/ioc/set/set.xml"};
			ApplicationContext container = new ClassPathXmlApplicationContext(path);//加载xml文件
			Student stu=(Student) container.getBean("stu1");//与bean标签中的name值相同
			//Student stu=(Student) container.getBean("stu2");
			System.out.println(stu);
			System.out.println(stu.getId());
			System.out.println(stu.getName());
			System.out.println(stu.getAge());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

结果:在这里插入图片描述
在这里插入图片描述

知识点1:测试默认是单例模式还是非单例模式

spring是如何管理我们配置的对象?
默认情况下,spring通过单例模式管理对象,通过相同一个对象名字,多次在容器中拿对象,每一次拿到的都是相同的对象

通过配置,可以让spring使用非单例模式管理对象
通过相同一个对象名字多次在容器中拿对象,每一次拿到的都是不同的对象(非单例)
 在bean标签里面加入属性**scope="prototype"**
@Test
public void ioc_set() {
		try {
			String[] path = {"com/briup/ioc/set/set.xml"};
			ApplicationContext container = new ClassPathXmlApplicationContext(path);
			//验证单例模式和非单例模式
			//构造器只走了一次,地址相同,默认是单例模式
			System.out.println(container.getBean("stu1"));
			System.out.println(container.getBean("stu1"));
			System.out.println(container.getBean("stu1"));
		} catch (Exception e) {
			e.printStackTrace();
		}
}

结果:
在这里插入图片描述
默认情况下,走了一次无参构造,地址相同,默认是单例模式

<bean name="stu2" class="com.briup.bean.Student" scope="prototype">
	<property name="id">
		<value>1</value>
	</property>
	<property name="name">
		<value>jack</value>
	</property>
	<property name="age">
		<value>20</value>
	</property>
</bean>
@Test
public void ioc_set() {
		try {
			String[] path = {"com/briup/ioc/set/set.xml"};
			ApplicationContext container = new ClassPathXmlApplicationContext(path);
			//验证单例模式和非单例模式
			//构造器只走了一次,地址相同,默认是单例模式
			System.out.println(container.getBean("stu2"));
			System.out.println(container.getBean("stu2"));
			System.out.println(container.getBean("stu2"));
		} catch (Exception e) {
			e.printStackTrace();
		}
}

在这里插入图片描述
地址不一样,走了三次无参构造

知识点2:property标签中对于value和ref的用法

<bean name="stu" class="com.briup.bean.Student">
	<property name="name" value="tom"></property>
     <property name="student" ref="stu1"></property>
</bean>
bean中的name属性是随便起的,class属性值的值表示要配置的哪一个类的对象
property中的name属性表示一个方法,表示setStudent, setName
value属性代表基本类型,可以直接赋值,部分还可以相互转换
ref属性表示调用setStudent()这个方法要传进去的对象的变量名

新建实体类com.briup.bean.Teacher

public class Teacher {
	private long id;
	private String name;
	private Student student;//调用Student类
	public Teacher() {}
	public Teacher(Student student) {
		this.student = student;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
}
<bean name="stu" class="com.briup.bean.Student">
	<property name="id">
		<value>1</value>
	</property>
	<property name="name">
		<value>jack</value>
	</property>
	<property name="age">
		<value>20</value>
	</property>
</bean>
<bean name="teacher" class="com.briup.bean.Teacher">
	<property name="id" value="1"></property>
	<property name="name" value="zz"></property>
	<property name="student" ref="stu"></property>
</bean>

测试类中

@Test
public void ioc_set() {
		try {
			String[] path = {"com/briup/ioc/set/set.xml"};
			ApplicationContext container = new ClassPathXmlApplicationContext(path);
			Teacher t = (Teacher) container.getBean("teacher");
			System.out.println(t);
			System.out.println(t.getId());
			System.out.println(t.getName());
			System.out.println(t.getStudent());
		} catch (Exception e) {
			e.printStackTrace();
		}
}

结果:
在这里插入图片描述

知识点3:起别名

可以给对象起一个别名,别名和student真正的name名字代表的是同一个对象
<bean name="stu" class="com.briup.bean.Student">
	<property name="id">
		<value>1</value>
	</property>
	<property name="name">
		<value>jack</value>
	</property>
	<property name="age">
		<value>20</value>
	</property>
</bean>
<bean name="teacher" class="com.briup.bean.Teacher">
	<property name="id" value="1"></property>
	<property name="name" value="zz"></property>
	<property name="student" ref="stu"></property>
</bean>
<alias name="stu" alias="s1"/>
<alias name="teacher" alias="t"/>

测试类中

@Test
public void ioc_set() {
		try {
			String[] path = {"com/briup/ioc/set/set.xml"};
			ApplicationContext container = new ClassPathXmlApplicationContext(path);
			Teacher t = (Teacher) container.getBean("teacher");
			Teacher t1 = (Teacher) container.getBean("t");
			System.out.println(t==t1);
		} catch (Exception e) {
			e.printStackTrace();
		}
}

结果:true
表示是同一个对象

知识点4:可以给一个类配置多个对象,对象的名字要不同

<bean name="stu1" class="com.briup.bean.Student"></bean>
<bean name="stu2" class="com.briup.bean.Student"></bean>
<bean name="stu3" class="com.briup.bean.Student"></bean>

测试类中

@Test
public void ioc_set() {
		try {
			String[] path = {"com/briup/ioc/set/set.xml"};
			ApplicationContext container = new ClassPathXmlApplicationContext(path);
			System.out.println(container.getBean("stu1"));
			System.out.println(container.getBean("stu2"));
			System.out.println(container.getBean("stu3"));
		} catch (Exception e) {
			e.printStackTrace();
		}
}

结果:
在这里插入图片描述

知识点5:向一个对象中注入集合List,Set,Map,Properties

collection.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
        <bean name="student" class="com.briup.bean.Student">
        	<property name="list">
        		<list>
        			<value>杨幂</value>
        			<value>郑爽</value>
        			<value>赵丽颖</value>
        		</list>
        	</property>
        	<property name="set">
        		<set>
        			<value>马云</value>
        			<value>马化腾</value>
        			<value>李彦宏</value>
        		</set>
        	</property>
        	<property name="map">
        		<map>
        			<entry key="1" value="姜涛"></entry>
        			<entry key="2" value="胖娘"></entry>
        			<entry key="3" value="黑v"></entry>
        		</map>
        	</property>
        	<property name="prop">
        		<props>
        			<prop key="a">医生</prop>
        			<prop key="b">护士</prop>
        			<prop key="c">老师</prop>
        		</props>
        	</property>
        </bean>  
</beans>

测试

@Test
public void ioc_collection() {
	try {
		String[] path = {"com/briup/ioc/collection/collection.xml"};
		ApplicationContext container = new ClassPathXmlApplicationContext(path);
		Student stu=(Student) container.getBean("student");
		System.out.println(stu);
		stu.show();
		System.out.println("---------------");
		System.out.println(stu.getList().getClass());//ArrayList
		System.out.println(stu.getSet().getClass());//LinkedHashSet
		System.out.println(stu.getMap().getClass());//LinkedHashMap
		System.out.println(stu.getProp().getClass());//Properties
		/*	
			class java.util.ArrayList
			class java.util.LinkedHashSet
			class java.util.LinkedHashMap
			class java.util.Properties
		*/
	} catch (Exception e) {
		e.printStackTrace();
	}
}

结果:
在这里插入图片描述
测试默认集合类型
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值