Spring框架|DI---Set方法注入


一、Set方法注入

	<bean id="p" class="com.gql.entity.Person">
		<property name="name" value="大力"></property>
		<property name="age" value="21"></property>
		<property name="s" ref="stu"></property>
	</bean>
	<bean id="stu" class="com.gql.di.Student"></bean>

成功通过Set方法注入对象。
在这里插入图片描述

  • 优势
    创建对象时没有明确的限制,可以直接使用默认构造函数。
  • 弊端
    如果有某个成员必须有值,使用Set方法无法保证一定注入(获取对象时set方法不一定执行)。

(1)property标签

标签的属性说明
name指定注入时所调用的set方法名称
value提供基本类型和String类型的数据。
ref指定其他的bean类型数据。(指在Spring的IoC核心容器中出现过的bean对象)

(2)测试Set方法注入

package com.gql.di;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gql.entity.Person;

/**
 * 类说明:
 *		测试依赖注入
 * @guoqianliang1998.
 */
public class Demo {

	@Test
	public void testSet(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p = (Person)ac.getBean("p");
		System.out.println(p.getName()+","+p.getAge()+","+p.getS());	
	}
}

(3)Person和Student实体类

Person实体类

package com.gql.entity;

import java.util.ArrayList;
import java.util.List;

import com.gql.di.Student;

/**
 * 类说明: 
 * 		实体类Person 
 * @guoqianliang1998.
 */
public class Person {
	private String name;
	private Integer age;
	private Student s;
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Person(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public void init() {
		System.out.println("初始化对象...");

	}

	public void destroy() {
		System.out.println("销毁对象...");
	}

	public Student getS() {
		return s;
	}

	public void setS(Student s) {
		this.s = s;
	}
	
}

Student实体类

package com.gql.di;
/**
 * 类说明:
 *		学生实体类
 * @guoqianliang1998.
 */
public class Student {
	private String name;
	private Integer age;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

(4)XML配置

applicationContext_Di.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="p" class="com.gql.entity.Person">
		<property name="name" value="大力"></property>
		<property name="age" value="21"></property>
		<property name="s" ref="stu"></property>
	</bean>
	<bean id="stu" class="com.gql.di.Student"></bean>
</beans>

applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<import resource="com/gql/di/applicationContext_Di.xml"/>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hudie.

不要打赏!不要打赏!不要打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值