Spring之依赖注入

什么是DI(dependency import)?

依赖注入,给对象的属性赋值

package cn.java.di5;
/**
 * @Title:  Cat.java
 * @Package cn.java.di5
 * @author: Matthew
 * @date: 2019年3月7日 下午3:48:33
 */

public class Cat {
	private String name;//猫名
	private Integer age;//年龄
	private Float weight;//体重
	
	

	/**
	 * @param name
	 * @param age
	 * @param weight
	 */
	public Cat(String name, Integer age, Float weight) {
		super();
		this.name = name;
		this.age = age;
		this.weight = weight;
		System.out.println("有参构造方法");
	}
	public Cat() {
		System.out.println("无参构造方法");
	}
	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", weight=" + weight + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		System.out.println("调用了set方法");
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Float getWeight() {
		return weight;
	}
	public void setWeight(Float weight) {
		this.weight = weight;
	}
	
}

第一种通过构造方法赋值

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<!-- 
		通过构造器给对象赋值
	 -->
	<bean id="cat1" class="cn.java.di5.Cat">
		<constructor-arg index="0" type="java.lang.String" value="tom"></constructor-arg>
		<constructor-arg index="1" type="java.lang.Integer" value="10"></constructor-arg>
		<constructor-arg index="2" type="java.lang.Float" value="2.5"></constructor-arg>
	</bean>
</beans>
package cn.java.di5;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Description:单例与多例
 * @Title:  Window.java
 * @Package cn.java.ioc1
 * @author: Matthew
 * @date: 2019年3月6日 下午6:01:58
 */

public class Window {

	public static void main(String[] args) {
//		YellowMouseWolf yellow = new YellowMouseWolf();
//		1.启动框架(context代表spring容器)
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//		2.获取spring容器中创建的对象(通过id值获得)
		Cat cat = (Cat)context.getBean("cat1");
		System.out.println(cat);
	}
}

结果是
Ji…鸡蛋生了
Ji…我是小鸡还没长大
有参构造方法
Cat [name=tom, age=10, weight=2.5]

第二种通过get/set方法赋值

    <!--
    	通过set方法赋值  
    -->
    <bean id="cat2" class="cn.java.di5.Cat">
    	<property name="name" value="jack"></property>
    	<property name="age" value="20"></property>
    	<property name="weight" value="5.2"></property>
    </bean>
package cn.java.di5;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Description:单例与多例
 * @Title:  Window.java
 * @Package cn.java.ioc1
 * @author: Matthew
 * @date: 2019年3月6日 下午6:01:58
 */

public class Window {

	public static void main(String[] args) {
//		YellowMouseWolf yellow = new YellowMouseWolf();
//		1.启动框架(context代表spring容器)
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//		2.获取spring容器中创建的对象(通过id值获得)
		Cat cat = (Cat)context.getBean("cat2");
		System.out.println(cat);
	}
}

结果是:
Ji…鸡蛋生了
Ji…我是小鸡还没长大
有参构造方法
无参构造方法
调用了set方法
Cat [name=jack, age=20, weight=5.2]

在这里插入图片描述

复杂的依赖注入

通过DI中的get/set方式给对象的list、set、map、properties属性赋值
这里介绍String、自定义类Panda、List、Set、Map、Properties;这里没有定义泛型,所以默认泛型为object

package cn.java.di1;
/**
 * @Title:  Panda.java
 * @Package cn.java.di1
 * @author: Matthew
 * @date: 2019年3月7日 下午4:17:57
 */

public class Panda {
	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	@Override
	public String toString() {
		return "Panda [name=" + name + ", age=" + age + "]";
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

package cn.java.di1;
/**
 * @Title:  Person.java
 * @Package cn.java.di1
 * @author: Matthew
 * @date: 2019年3月7日 下午4:16:38
 */

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

public class Person {
	private String name;
	private Panda pet;
	
	private Set  set;
	private Map  map;
	private Properties props;
	private List list;
	
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", pet=" + pet + ", set=" + set + ", map=" + map + ", properties=" + props
				+ ", list=" + list + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Panda getPet() {
		return pet;
	}
	public void setPet(Panda pet) {
		this.pet = pet;
	}
	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 getProps() {
		return props;
	}
	public void setProps(Properties props) {
		this.props = props;
	}
	
}

注意:以下为真正的注入方式

1.给list属性赋值
2.给set属性赋值
3.给map属性赋值
4.给properties属性赋值

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
   <bean id="smallPanda" class="cn.java.di1.Panda">
   		<property name="name" value="功夫熊猫"></property>
   		<property name="age" value="20"></property>
   </bean>
   
   <bean id="person" class="cn.java.di1.Person">
   		<property name="name" value="王二麻子"></property>
   		<property name="pet" ref="smallPanda"></property>
   		<property name="list">
   			<list>
   				<value>list1</value>
   				<value>中国</value>
   				<ref bean="smallPanda"/>
   			</list>
   		</property>
   		<property name="set">
   			<set>
   				<value>set1</value>
   				<value>china</value>
   				<ref bean="smallPanda"/>
   			</set>
   		</property>
   		<property name="map">
   			<map>
   				<entry key="name" value="李四"></entry>
   				<entry key="age" value="10"></entry>
   				<entry>
   					<key>
   						<value>weight</value>
   					</key>
   					<value>23.3</value>
   				</entry>
   			</map>
   		</property>
   		<property name="props">
   			<props>
   				<prop key="driver">com.mysql.jdbc.Driver</prop>
   				<prop key="url">jdbc:oracle:@thin:localhost:1521:orcl</prop>
   			</props>
   		</property>
   </bean>
</beans>
package cn.java.di1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Description:单例与多例
 * @Title:  Window.java
 * @Package cn.java.ioc1
 * @author: Matthew
 * @date: 2019年3月6日 下午6:01:58
 */

public class Window {

	public static void main(String[] args) {
//		YellowMouseWolf yellow = new YellowMouseWolf();
//		1.启动框架(context代表spring容器)
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//		2.获取spring容器中创建的对象(通过id值获得)
		Person p1 = (Person)context.getBean("person");
		System.out.println(p1);
	}
}

结果为
Person [name=王二麻子, pet=Panda [name=功夫熊猫, age=20],
set=[set1, china, Panda [name=功夫熊猫, age=20]],
map={name=李四, age=10, weight=23.3},
properties={url=jdbc:oracle:@thin:localhost:1521:orcl, driver=com.mysql.jdbc.Driver},
list=[list1, 中国, Panda [name=功夫熊猫, age=20]]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值