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>
发布了363 篇原创文章 · 获赞 970 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/weixin_43691058/article/details/104095274