Spring IOC入门原理及简单举例

什么是IOC

Inversion Of Control 控制反转或反向控制
控制反转:改变了对象的获取方式,之前编码方式采用new关键字方式获取对象,IOC中采用容器创建对象之后并注入进来使用,只要修改配置就可以改变对象关系,降低了耦合度。

Spring IOC

可以理解为以下两个作用:

  1. 管理组件对象
    从实例化,初始化,释放资源,销毁四个方面对组件对象进行管理
  2. 维护对象关系
    采用注入方式建立对象关系,依赖注入(Dependency Injection),控制反转。DI两种方式:set注入,构造器注入(具体举例见代码)

Spring IOC实现流程

1.搭建Spring IOC开发环境
– 引入相关jar文件
在这里插入图片描述
–src下创建xml配置文件
在这里插入图片描述
2.定义bean
class=“包名.类名”

package org.oracle.test;

public class Computer {
	
	private String cpu;
	private String hdd;
	private String mainbord;
	
	public void show(){
		System.out.println("----电脑配置----");
		System.out.println("cpu:"+cpu);
		System.out.println("hdd:"+hdd);
		System.out.println("mainbord:"+mainbord);
	}
	
	public void setCpu(String cpu) {
		this.cpu = cpu;
	}
	public void setHdd(String hdd) {
		this.hdd = hdd;
	}
	public void setMainbord(String mainbord) {
		this.mainbord = mainbord;
	}	
}
package org.oracle.test;

public class Phone {
	
	private String cpu;
	private String ram;
	
	public Phone(String cpu,String ram){
		this.cpu = cpu;
		this.ram = ram;
	}
	
	public void show(){
		System.out.println("----手机配置---");
		System.out.println("cpu:"+cpu);
		System.out.println("ram:"+ram);
	}
}

package org.oracle.test;

public class Student {
	
	private Computer c;
	private Phone p;
	
	public void show(){
		c.show();
		p.show();
	}

	public void setC(Computer c) {
		this.c = c;
	}

	public void setP(Phone p) {
		this.p = p;
	}
}

Spring的配置文件:

<bean id="p1" class="org.oracle.test.Computer">
    <!-- 信息set注入 -->
    <property name="cpu" value="枭龙"></property>
    <property name="hdd" value="索尼"></property>
    <property name="mainbord" value="华硕"></property>
</bean>


<bean id="p2" class="org.oracle.test.Phone">
    <!-- 构造器注入 -->
    <constructor-arg index="0" value="高通"></constructor-arg>
    <constructor-arg index="1" value="8G"></constructor-arg>
</bean>

<bean id="s1" class="org.oracle.test.Student">
    <!-- set注入 -->
    <property name="c" ref="p1"></property>
    <property name="p" ref="p2"></property>
</bean>

3.实例化Spring容器(ApplicationContext)
4.使用时ac.getBean(“id值”),尽量不要用强转

package org.oracle.test;

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

public class TestComputer {
	
	public static void main(String[] args) {
		
	    ApplicationContext ac = new
	    		ClassPathXmlApplicationContext("applicationContext.xml");
	    Computer c = ac.getBean("p1",Computer.class);
		c.show();
	}

}

Spring IOC

Spring对bean对象的创建管理:

主要有三种方法:构造方法,工厂静态方法和对象工厂方法

package oracle.oracle;

public class test01 {
	
	private String a;
	public test01(){
		a="构造方法";
		
	}
	public void show(){
		System.out.println(a);
	}
}

package oracle.oracle;

public class test02 {

	public static String test(){
		String a="静态工厂方法";
		return a;
	}
	
}
package oracle.oracle;

public class test03 {

	private String name;
	private int age;
	
	public test03(String name,int age){
		this.name=name;
		this.age=age;
	}
	
	public String show(){
		return name+":"+age;
	}
}

Spring的配置文件

<!-- 采用构造方法 -->
<bean id="t1" class="oracle.oracle.test01">
</bean>

<!-- 采用静态工厂方法 -->
<bean id="t2" class="oracle.oracle.test02" factory-method="test">
</bean>

<bean id="t3" class="oracle.oracle.test03">
	<constructor-arg index="0" value="张三"></constructor-arg>
	<constructor-arg index="1" value="25"></constructor-arg>
</bean>

<!-- 采用对象工厂方法 -->
<bean id="t4" factory-bean="t3" factory-method="show">
</bean>
package oracle.oracle;

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

public class test04_test {
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		String a=(String)ac.getBean("t4");
		System.out.println(a);
	}
}

Spring创建Bean对象的控制

a.控制对象的创建方式(使用范围)
  在<bean>元素中使用scope属性控制
  scope可以支持singleton和prototype,默认singleton
  <bean scope="singleton">该组件在Spring容器里只有一个bean对象
  <bean scope="prototype">该组件在Spring容器里每次ac.getBean时
都会返回一个新对象

b.控制对象的初始化方法
  利用<bean>元素的init-method指定
  当创建对象后自动执行init-method指定的方法

c.指定对象的销毁方法
  利用<bean>元素的destroy-method指定
  满足两个条件才有效:
    --组件对象为单例模式
    --调用AbstractApplicationContext对象的close方法

d.控制单例对象的创建时机
  在默认情况下,单例对象是Spring容器创建时实例化,可以使用<bean>
元素的lazy-init="true"属性将创建时机推迟到getBean方法调用时

下一篇整理IOC的注解版,提前祝看到这里的你新年快乐!

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值