当前位置: 首页 > article >正文

《Spring视频教程》(p2)

p2  解耦合发展史、控制反转、依赖注入

 

IOC(控制反转)也可以称之为DI(依赖注入):

控制反转:将创建对象、属性值的方式进行了反转,从new,setXxx()反转为了从springIOC容器getBean()

依赖注入:将属性值注入给了属性,将属性注入给了bean,将bean注入给了ioc容器

 

总结:ioc/di, 无论要什么对象,都可以直接去springioc容器中获取,而不需要自己操作

因此,之后的ioc分为2步:1.先给springioc中存放对象并赋值   2.拿

Test.java

package org.lanqiao.test;import org.lanqiao.entity.Student;
import org.lanqiao.factory.CourseFactory;
import org.lanqiao.newinstance.ICourse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void learnCourseWithFactory() {Student student = new Student();student.learn("java");}public static void learnCourseWithIoC() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//执行从springIOC容器中获取一个id为student的对象Student student = (Student)context.getBean("student");student.learn("htmlCourse");}public static void main(String [] args) {//传统的方式:
//		 Student student = new Student();
//		 student.setStuNo(1);
//		 student.setStuName("zs");
//		 student.setStuAge(23);
//		 System.out.println(student);//		//新的方式
//		//Spring上下文对象:context
//		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//		//执行从springIOC容器中获取一个id为student的对象
//		Student student = (Student)context.getBean("student");
//		System.out.println(student);learnCourseWithIoC();}
}

Stduent.java

package org.lanqiao.entity;import org.lanqiao.factory.CourseFactory;
import org.lanqiao.newinstance.HtmlCourse;
import org.lanqiao.newinstance.ICourse;
import org.lanqiao.newinstance.JavaCourse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Student {private int stuNo;private String stuName;private int stuAge;public int getStuNo() {return stuNo;}public void setStuNo(int stuNo) {this.stuNo = stuNo;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public int getStuAge() {return stuAge;}public void setStuAge(int stuAge) {this.stuAge = stuAge;}@Overridepublic String toString() {return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", stuAge=" + stuAge + "]";}public void learn(String name) {//1. 从自己编写的简单工厂中获取课程
//		ICourse course = CourseFactory.getCourse(name);//course就是根据name拿到的相应的课程//直接从ioc容器中获取//2. 从SpringIOC提供的超级工厂中获取课程ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");ICourse course = (ICourse)context.getBean(name);course.learn();}//学习java课程public void learnJava() {ICourse course = new JavaCourse();course.learn();}//学习html课程public void learnHtml() {ICourse course = new HtmlCourse();course.learn();}	}

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"><!-- 该文件中产生的所有对象,被spring放入了一个称之为spring ioc容器的地方  --><!-- id:唯一标识符     class:指定类型 --><bean id="student" class="org.lanqiao.entity.Student"><!-- property: 该class所代表的类的属性name: 属性名value: 属性值--><property name="stuNo" value="2"></property><property name="stuName" value="ls"></property><property name="stuAge" value="24"></property></bean><bean id="javaCourse" class="org.lanqiao.newinstance.JavaCourse"></bean><bean id="htmlCourse" class="org.lanqiao.newinstance.HtmlCourse"></bean></beans>

 

http://www.lryc.cn/news/2415838.html

相关文章:

  • domodal返回值问题
  • 【芯核架构】听你说你很了解ARM?
  • winrar4.0 注册码 key
  • Spring定时任务的几种实现
  • 心灵指南 刘墉 第三辑 肯定自己 笔记
  • 传说中的800句记7000词
  • 如何汉化vs2010、VS015、vs2017、VS019、v2022
  • 杂记
  • 缓存溢出
  • Linux USB 驱动实验
  • 用户行为分析
  • CodeGear Delphi 2007 for Win32 专业版下载地址及安装、破解方法
  • VBScript基础,变量,函数,流程
  • android布局layout,Android布局(layout)
  • Vmware17.5.1安装CentOS-Stream-9 【超超详细图文安装教程】
  • Django项目实战----接入第三方QQ登录绑定用户
  • 站外SEO应该怎么做?
  • 华为ct3100在entware上安装debian环境且在debian中安装samba过程
  • Blog主流程序整理(转)
  • Ubuntu7.04安装全实录
  • 3ds Max 2019: Advanced Materials 3ds Max 2019:材质高级教程 Lynda课程中文字幕
  • 数据库原理实验二 数据库管理 实验报告
  • 2007年度全国国际旅行社100强名单
  • wpe使用方法(详细)(--新加秒10图)(10号继续可用)
  • 黑莓9520拆机过程
  • 吴宗宪主持妙语
  • 处理器架构 (十四) arm1176 微架构下 各芯片的不同
  • Behance 大神推荐2019 年所有设计领域的最新趋势!
  • 试用 smartdraw 2010 方便快捷的图表工具
  • push notification