【Spring】框架学习笔记(Ioc 篇)

6 篇文章 1 订阅

IOC:控制反转(依赖注入)

控制的内容:指的是对象的创建,即谁来控制对象的创建。传统的应用程序对象的创建是由程序本身来控制,使用框架后由 Spring 容器来创建。(由传统的对象管理对象->统一由专门的 Spring 容器管理对象,使得开发人员可以集中于应用的开发,避免重复更改源码,降低了程序的耦合度:解耦。

反转的内容:反转指的是程序本身不去创建对象,而变为被动接收对象。而正转指程序创建对象。

依赖注入其实就是控制反转从另一个角度的说法,也就是由容器动态地将某种依赖关系的目标对象实例注入到应用系统中的各个关联的组件之中

总的来说,即以前是由程序本身控制对象的创建变为由程序被动接收 Spring 容器创建对象的过程。

//示例:
public class HelloWorld {
  private String message;
  public void setMessage(String message){
	  this.message  = message;
  }
  public void getMessage(){
	  System.out.println("Spring : " + message);
  }
}


//测试类
public class MainApp {
  public static void main(String[] args) {
          //获取上下文,解析beans.xml生成管理相应的bean对象
	      ApplicationContext context = 
	             new ClassPathXmlApplicationContext("beans.xml");
          //不用直接创建对象,而是调用getBean方法,获取Spring容器中已实例化的bean
	      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
	      obj.getMessage();
	   }
}

//bean配置文件
<?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-3.0.xsd">

   <!--bean是java对象,由spring管理和创建;id,name都不能重复,使得IOC容器中能查到该ID/name的唯一的bean-->

   <bean id="helloWorld" class="com.service.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

Ioc 作为一种编程思想,使得主动编程变为被动接收(包括但不限于创建对象,创建对象只是获取资源的一种方式)。Ioc 的实现是通过 Ioc 容器实现的。(Ioc容器即 BeanFactory)

而使用 Ioc 来创建对象有三种方式:

//beans.xml配置文件
//1、通过无参的构造方法来创建
<bean id="*****" class="*****">
       <property name="*****" value="*****"/>
   </bean>
/*2、通过有参的构造方法来创建:
a)根据参数的下标来设置 */

<bean id="*****" class="*****">
       <!--index指构造方法参数下标-->
       <constructor-arg index="0" value="****"/>
   </bean>

//b)根据参数名称来设置

<bean id="*****" class="*****">
       <constructor-arg name="****" value="****"/>
   </bean>

//根据参数类型来设置

<bean id="*****" class="*****">
       <constructor-arg type="****" value="****"/>
   </bean>
//3、通过构造工厂方法来创建

//a)静态工厂

public class UserFactory{
    public static User function(String name){
        return new User(name);
    }
}    

<bean id="*****" class="*****" factory-method="function">
       <!--index指构造方法参数下标-->
       <constructor-arg index="0" value="****"/>
   </bean>

//b)动态工厂

public class UserDynamicFactory{
    public User function(String name){
        return new User(name);
    }
}    

<bean id="userFactory" class="*****"/>
<!-- factory-bean 引用动态工厂的ID -->
<bean id="*****" factory-bean="userFactory" factory-method="function">
       <constructor-arg index="0" value="****"/>
   </bean>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值