11.18笔记(Spring IoC容器)

Spring IoC容器

Spring容器即体现了IoC原理
Spring容器通过读取配置元数据负责对Beans实例化、配置和装配
配置元数据可以用XML、Java注解或Java代码来描述

Spring IoC容器工作视图
在这里插入图片描述

ApplicationContext是BeanFactory的一个子接口

//1.实例化Spring容器
ApplicationContext ac=new ClassPathXmlApplicationContext(“applicationContext.xml”);

区别

BeanFactory提供了配置框架和基本功能,ApplicationContext则添加了更多的企业特定功能
ApplicationContext是BeanFactory的一个子接口,同时也是它的一个完整的超集
实例Bean的载入方式不同:
BeanFactory延迟载入所有Bean,直到getBean()方法调用时才被创建
ApplicationContext启动后载入所有单实例Bean。通过预载入单实例Bean,确保当需要时可以使用

依赖注入
依赖注入指对象之间的依赖关系
Spring容器的工作就是创建Bean并注入他所依赖的Bean对象
Spring中的注入方式主要有2种:构造器注入和Setter方法注入

setter方法注入

<!-- 1.setter方法注入 -->
		<bean id="student" class="com.spring.Student">
		<!-- 通过property中value属性或者value子元素都可以实现向某一个属性注入值 -->
		<property name="sno" value="117333440114">
		<!-- <value>117333440114</value> -->
		<!-- <null></null>对某个属性赋值为空 -->
		</property>
		<property name="name" value="赖文卓"></property>
		<property name="age" value="21"></property>
		<!-- ref属性可以指定参考对象 ,它的值时改对象在容器中id元素对应的值-->
		<property name="jc" ref="javaCourse"></property>
		</bean>

构造方法注入

<!-- 2.构造方法注入 -->
		<!-- 默认情况下按照构造方法顺序依次注入 -->
		<bean id="student1" class="com.spring.Student">
		<!-- 通过index属性指定注入的顺序,从0开始 -->
		
		<constructor-arg value="lwz" index="1"></constructor-arg>
		<constructor-arg value="117333440114" index="0"></constructor-arg>
		<constructor-arg value="21" index="2"></constructor-arg>
		<constructor-arg  name="jc" ref="javaCourse" index="3"></constructor-arg>
		<!-- 通过type属性指定注入顺序 -->
		
		<!-- <constructor-arg value="117333440114" type="java.lang.String" ></constructor-arg>
		<constructor-arg value="lwz" type="java.lang.String" ></constructor-arg>
		<constructor-arg value="21" type="int" ></constructor-arg> -->
		
		</bean>

实体类

import java.util.List;
import java.util.Map;

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

public class Student {
	private String sno;
	private String name;
	private int age;
	private JavaCourse jc;
	private List<Profession> ps;
	private Map<String, Profession> pf;

	public Student() {

	}

	public Student(String sno, String name, int age, JavaCourse jc, List<Profession> ps, Map<String, Profession> pf) {
		super();
		this.sno = sno;
		this.name = name;
		this.age = age;
		this.jc = jc;
		this.ps = ps;
		this.pf = pf;
	}





	public String getSno() {
		return sno;
	}

	public void setSno(String sno) {
		this.sno = sno;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

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

	public JavaCourse getJc() {
		return jc;
	}

	public void setJc(JavaCourse jc) {
		this.jc = jc;
	}

	public List<Profession> getPs() {
		return ps;
	}

	public void setPs(List<Profession> ps) {
		this.ps = ps;
	}
	

	public Map<String, Profession> getPf() {
		return pf;
	}

	public void setPf(Map<String, Profession> pf) {
		this.pf = pf;
	}

//	@Override
//	public String toString() {
//		return "Student [sno=" + sno + ", name=" + name + ", age=" + age + ", jc=" + jc + ", ps=" + ps + ", pf=" + pf
//				+ "]";
//	}
	
	@Override
	public String toString() {
		return "Student [sno=" + sno + ", name=" + name + ", age=" + age + ", jc=" + jc + "]";
	}
	

	// 学习课程(一)
	public void studyJavaCourse() {
		Course course = new JavaCourse();
		course.getCourse();

	}



	public void studyHtmlCourse() {
		Course course = new HtmlCourse();
		course.getCourse();

	}

	// 学习课程(二),通过调用课程工厂对象实现选课功能
	public void studyCourseByFactory(String name){
		CourseFactory cf=new CourseFactory();
		cf.getCourse(name);
//		System.out.println(course);
	}
	//学习课程(三),通过IOC方式实现
	public void studyCourseByIOC(String name){
		 //1.实例化Spring容器
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		/*
		 * 2.获取对象方式
		 * getBean("bean元素的id值")
		 * getBean(*.class);
		 * getBean("bean元素的id值",*.class);
		 */
		JavaCourse jc=(JavaCourse)ac.getBean(name);
		System.out.println(name);
//		JavaCourse jc=(JavaCourse)ac.getBean(JavaCourse.class);
//		JavaCourse jc=(JavaCourse)ac.getBean(name,JavaCourse.class);
		jc.getCourse();
	}

}

测试类

public class TestDI {

	public static void main(String[] args) {
		//1.实例化Spring容器
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		//使用setter方法注入
		Student student= (Student) ac.getBean("student");
		System.out.println(student);
		//使用构造方法注入
		Student student1= (Student) ac.getBean("student1");
		System.out.println(student1);

	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值