spring7: di依赖注入--设值注入

49 篇文章 0 订阅

 

di:就是依赖注入,给属性赋值。

di注入的分类:

        1.设值注入,调用java类中的set方法,给属性赋值。

       2. 构造注入,调用java类中的有参数构造方法,创建对象的同时,给属性赋值。

 

di的语法:

      1. 基于xml的配置文件,在xml中使用标签和属性,完成属性的赋值。

      2.基于注解的方式,使用注解创建对象,给属性赋值。

设值注入(set方法注入)

简单类型的设值注入

package com.atChina.Test;

public class Student {
	private String name;
	private int age;
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd ">

       <!-- 设值注入: 调用类中的set方法完成属性赋值 
                        简单类型: spring中把string和java基本数据类型,称为简单类型
                      简单类型的设值注入:
             <bean id="xx" class="yy">
             		<property name="属性名" value="简单类型的属性值"/>
             		<property name="属性名" value="简单类型的属性值"/>
             		...
             </bean>
        -->
        
        <bean id="student" class="com.atChina.Test.Student">
            <property name="name" value="宋江"/>
            <property name="age" value="20" />
        </bean>
</beans>

引用类型的设值注入:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd ">

       <!-- 设值注入: 调用类中的set方法完成属性赋值 
                        简单类型: spring中把string和java基本数据类型,称为简单类型
            1)简单类型的设值注入:
             <bean id="xx" class="yy">
             		<property name="属性名" value="简单类型的属性值"/>
             		<property name="属性名" value="简单类型的属性值"/>
             		...
             </bean>
             
             2)引用类型的设值注入
               	语法1: 使用ref作为属性
               			<bean id="xx" class="yy">
             				<property name="属性名" ref="bean的id"/>
            		   </bean>
            		   
                                语法2: 使用ref作为子标签
                       <bean id="xx" class="yy">
             				<property name="属性名">
             				    <ref bean="bean的id"/>
             				<property/>
            		   </bean>
        -->
        
        <!-- 使用语法1,给引用类型赋值,ref作为属性 -->
        <bean id="student" class="com.atChina.Test2.Student">
            <property name="name" value="宋江"/>
            <property name="age" value="20" />
            <property name="school" ref="xuexiao"/>
        </bean>
        
        <!-- 使用语法2,ref作为子标签性 -->
        <bean id="student2" class="com.atChina.Test2.Student">
            <property name="name" value="吴用"/>
            <property name="age" value="22" />
            <property name="school">
            	<ref bean="xuexiao"/>
            </property>
        </bean>
        
        <bean id="xuexiao" class="com.atChina.Test2.School">
            <property name="name" value="同济大学"/>
            <property name="address" value="上海市" />
        </bean>
</beans>
package com.atChina.Test2;

public class Student {
	private String name;
	private int age;
	private School school;
	
	public Student(){
		System.out.println("无参数构造方法...");
	}
	
	public void setSchool(School school) {
		this.school = school;
	}

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

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", school=" + school
				+ "]";
	}
}
package com.atChina.Test2;

public class School {
	private String address;
	private String name;

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "School [address=" + address + ", name=" + name + "]";
	}

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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值