SpringIOC

SpringIOC

  • 开发第一个Spring程序(IOC)
  • springIOC发展史
  • IOC控制反转/依赖注入
  • 图解三种方式的区别
    • 依赖注入的三种方式
    • 自动装配(只适用于 ref类型 )
    • 使用注解定义bean
    • Value与< Value >注入方式的区别

开发第一个Spring程序(IOC)

  1. ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml") ;
  2. //执行从springIOC容器中获取一个 id为student的对象
  3. Student student = (Student)conext.getBean("student") ;
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <beans xmlns="http://www.springframework.org/schema/beans"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  8. <bean id="student" class="org.cduck.entity.Student">
  9. <property name="stuNo" value="1"></property>
  10. <property name="stuName" value="zs"></property>
  11. <property name="stuAge" value="21"></property>
  12. </bean>
  13. </beans>

可以发现,springioc容器 帮我们new了对象,并且给对象赋了值,我们再需要使用该对象时,只需要去IOC容器中“拿”。
在这里插入图片描述

springIOC发展史

  1. 1.
  2. Student student = new Student();
  3. student.setXxx();
  4. 2.
  5. 简单工厂
  6. 3.
  7. ioc(超级工厂)

IOC控制反转/依赖注入

IOC(控制反转)也可以称之为DI(依赖注入):
控制反转:将 创建对象、属性值 的方式 进行了翻转,从new、setXxx() 翻转为了 从springIOC容器getBean()
依赖注入:将属性值 注入给了属性,将属性 注入给了bean,将bean注入给了ioc容器;

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

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

图解三种方式的区别

在这里插入图片描述

依赖注入的三种方式

1.set注入:通过调用setXxx()赋值。

赋值,默认使用的是 set方法();
依赖注入底层是通过反射实现的。









2.构造器注入:通过构造方法赋值

需要注意:如果 的顺序 与构造方法参数的顺序不一致,则需要通过type或者index或name指定。







3.p命名空间注入
引入p命名空间
xmlns:p=“http://www.springframework.org/schema/p”
在这里插入图片描述




注意:
简单类型:
p:属性名=“属性值”
引用类型(除了String外):
p:属性名-ref=“引用的id”
注意多个 p赋值的时候 要有空格。

示例:
注入各种集合数据类型: List Set map

package org.cduck.entity;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AllCollectionType {


private List listEList;
private String[] array;
private Set setESet;
private Map mapEMap;
public List getListEList() {

return listEList;
}
public void setListEList(List listEList) {

this.listEList = listEList;
}
public String[] getArray() {

return array;
}
public void setArray(String[] array) {

this.array = array;
}
public Set getSetESet() {

return setESet;
}
public void setSetESet(Set setESet) {

this.setESet = setESet;
}
public Map getMapEMap() {

return mapEMap;
}
public void setMapEMap(Map mapEMap) {

this.mapEMap = mapEMap;
}
public AllCollectionType(List listEList, String[] array, Set setESet, Map mapEMap) {

super();
this.listEList = listEList;
this.array = array;
this.setESet = setESet;
this.mapEMap = mapEMap;
}

public AllCollectionType() {

}
@Override
public String toString() {

String tempString=””;
for (String str : array) {

tempString+=str+”,”;
}

return “listEList=” + listEList + “\n array=” + Arrays.toString(array) + “\n setESet=”
+ setESet + “\n mapEMap=” + mapEMap ;
}


}





足球1
篮球1
乒乓1

  1. <property name="array">
  2. <array>
  3. <value>足球2</value>
  4. <value>篮球2</value>
  5. <value>乒乓2</value>
  6. </array>
  7. </property>
  8. <property name="setESet">
  9. <set>
  10. <value>足球3</value>
  11. <value>篮球3</value>
  12. <value>乒乓3</value>
  13. </set>
  14. </property>
  15. <property name="mapEMap">
  16. <map>
  17. <entry>
  18. <key>
  19. <value>foot</value>
  20. </key>
  21. <value>足球4</value>
  22. </entry>
  23. <entry>
  24. <key>
  25. <value>bas</value>
  26. </key>
  27. <value>篮球4</value>
  28. </entry>
  29. <entry>
  30. <key>
  31. <value>pp</value>
  32. </key>
  33. <value>乒乓4</value>
  34. </entry>
  35. </map>
  36. </property>
  37. </bean>

给对象类型赋值null :

  1. <property name="name" >
  2. <null/> -->注意 没有<value>
  3. </property>

赋空值 “” :

  1. <property name="name" >
  2. <value></value>
  3. </property>

在ioc中定义bean的前提:该bean的类 必须提供了 无参构造

自动装配(只适用于 ref类型 )

自动装配:

  1. <bean ... class="org.lanqiao.entity.Course" autowire="byName|byType|constructor|no" >

byName本质是byId
byName: 自动寻找:其他bean的id值=该Course类的属性名
byType: 其他bean的类型(class) 是否与 该Course类的ref属性类型一致 (注意,此种方式 必须满足:当前Ioc容器中 只能有一个Bean满足条件 )
constructor: 其他bean的类型(class) 是否与 该Course类的构造方法参数 的类型一致;此种方式的本质就是byType

可以在头文件中 一次性将该ioc容器的所有bean 统一设置成自动装配:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. ...
  3. default-autowire="byName">

自动装配虽然可以减少代码量,但是会降低程序的可读性,使用时需要谨慎。

使用注解定义bean

  1. <context:component-scan base-package="org.lanqiao.dao">
  2. </context:component-scan>

Spring在启动的时候,会根据base-package在 该包中扫描所有类,查找这些类是否有注解@Component(“studentDao”),如果有,则将该类 加入spring Ioc容器。
注意:要在xml引入命名空间
在这里插入图片描述
在这里插入图片描述
@Component细化:

dao层注解:@Repository
service层注解:@Service
控制器层注解:@Controller

Value与< Value >注入方式的区别

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,99人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringIOC容器

    1.什么是IOC IOC(Inversion of Control):其思想翻转资源获取的方向,传统的资源查找方法要求组件向容器发起请求查找资源,作为回应,容器适时

    相关 SpringIOC机制

    IoC IoC(控制反转):本来是由应用程序管理的对象之间的依赖关系,现在交给了容器管理,这就叫控制反转,Spring的IoC容器主要使用DI(注入)方式实现的,不需要主

    相关 SpringIOC/DI

    spring笔记 高内聚低耦合: 工厂模式:通过第三方的类产生我们需要的产品(对象),用来解耦合 简单工厂: 1. 提供产品接口,之后让所有产品实现该接口 2.

    相关 SpringIOC 容器

    spring是可以解决对象创建以及对象之间依赖关系的一种框架。   通过添加模块,添加不同功能 1. Spring Core  spring的核心功能: IOC容器

    相关 SpringIOC

    什么是IOC > 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式