Spring中的Bean配置 (4)——依赖注入方式

在这里插入图片描述
Spring中的Bean配置 (1)——内容提要
Spring中的Bean配置 (2)—— IOC和DI
Spring中的Bean配置 (3)——在Spring的IOC容器里配置Bean(通过全类名(反射))——基于XML文件的方式
Spring中的Bean配置 (4)——依赖注入方式
Spring中的Bean配置 (5)——字面值
Spring中的Bean配置 (6)——引用其他Bean
Spring中的Bean配置 (7)——注入参数详解:null值和级联属性
Spring中的Bean配置 (8)—— 集合属性
Spring中的Bean配置 (9)—— XML 配置里的 Bean自动装配
Spring中的Bean配置 (10)—— 继承 Bean 配置和依赖 Bean 配置
Spring中的Bean配置 (11)——Bean的作用域
Spring中的Bean配置 (12)——使用外部属性文件
Spring中的Bean配置 (13)—— Spring表达式语言:SpEl
Spring中的Bean配置 (14)——IOC容器中Bean的生命周期
Spring中的Bean配置 (15)——在Spring的IOC容器里配置Bean(通过工厂方法创建Bean)——基于XML文件的方式
Spring中的Bean配置 (16)——在Spring的IOC容器里配置Bean(通过FactoryBean)——基于XML文件的方式

Spring中的Bean配置 (17)——在Spring的IOC容器里配置Bean——基于注解的方式来配置Bean

一, 属性注入

属性注入即通过setter方法注入Bean的属性值或依赖的对象

属性注入使用元素,使用name属性指定Bean的属性名称value 属性或子节点指定属性值

属性注入是实际应用中最常用的注入方式

在这里插入图片描述

二,构造方法注入

  • 通过构造方法注入Bean的属性值或依赖的对象,它保证了Bean实例在实例化后就可以使用。
  • 构造器注入在在这里插入图片描述元素里声明,在这里插入图片描述中没有name属性

按索引匹配入参

在这里插入图片描述

按类型匹配入参

在这里插入图片描述

如下代码示例 HelloWord.java

package com.atguigu.spring.beans;

public class HelloWord {
	
	private String userName;
	private String number;
	private String place;
	private int address;
	
	public HelloWord(String number, String place, int address) {
		
		super();
		this.number = number;
		this.place = place;
		this.address = address;
		
	}

	public HelloWord(String userName, String number, String place) {
		
		super();
		this.userName = userName;
		this.number = number;
		this.place = place;
		
	}

	@Override
	public String toString() {
        
		return "HelloWord [userName=" + userName + ", number=" + number + ", place=" + place + ", address=" + address
				+ "]";
	
    }
	
}


application.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 
   class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器。
     id:标识容器中的bean,id唯一
 -->
 
<bean id="beans" class="com.atguigu.spring.beans.HelloWord">
<!-- 按 索引匹配入参-->
<constructor-arg value="zlj" ></constructor-arg>
<constructor-arg value="1233"></constructor-arg>
<constructor-arg value="fz"></constructor-arg>
</bean>


<bean id="bean" class="com.atguigu.spring.beans.HelloWord">
<!-- 按 类型匹配入参-->
<constructor-arg value="zlj" type="java.lang.String"></constructor-arg>
<constructor-arg value="1233" type="java.lang.String"></constructor-arg>
<constructor-arg value="12" type="int"></constructor-arg>
</bean>
</beans>

按索引匹配入参主函数运行代码 Main.java

package com.atguigu.spring.beans;

import java.applet.AppletContext;

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

public class Main {

	public static void main(String[] args) {

		//1.创建Spring的IOC容器对象
		//ApplicationContext代表IOC容器
   ApplicationContext applicationContext=new        ClassPathXmlApplicationContext("applicationtext.xml");
   
   //2.从IOC容器中获取Bean实例
   //利用id定位到IOC容器中的bean
   HelloWord helloWord=(HelloWord) applicationContext.getBean("beans");
   
   //3.调用hello方法
  System.out.println(helloWord);
		
	}

}


按类型匹配入参主函数运行代码示例 Main.java

package com.atguigu.spring.beans;

import java.applet.AppletContext;

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

public class Main {

	public static void main(String[] args) {

		//1.创建Spring的IOC容器对象
		//ApplicationContext代表IOC容器
   ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationtext.xml");
   
   //2.从IOC容器中获取Bean实例
   //利用id定位到IOC容器中的bean
   HelloWord helloWord=(HelloWord) applicationContext.getBean("bean");
   
   //3.调用hello方法
  System.out.println(helloWord);
		
	}

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值