SpringIOC

一、基本使用流程代码实现

1.1 导包

图片说明

1.2 在src下配置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">
    //<!--配置资源对象-->
    <bean id="myService" class="com.service.impl.MyServiceImpl2"></bean>
</beans>

在xml中配置<id,class>的键值对,class对应具体Service的相对路径,当需要对现有的Service维护更新时,只需要在xml文件中修改class的值而不需要对源代码进行修改

1.3 在代码中获取Spring容器中的对象

//获取Spring容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml"); //配置文件名
final MyService myService = (MyService) ac.getBean("myService"); //"myService"为<id,class>键值对中的id
myService.testService(); //调用Service中的方法

二、SpringIOC创建对象的三种方式

在学习了SpringIOC的基本使用流程后,我们使用IOC解耦层与层之间的逻辑关系,但是我们发现,对象由以前我们自己根需求在代码中直接new创建为从Spring容器中获取,也就说对象的创建由Spring容器来创建,我们直接获取使用即可,那么,如果我们需要一个带有指定的初始化数据的对象,如何
让Spring容器对象帮我们创建呢?

2.1 通过构造器方式

  1. 无参构造器:Spring容器默认使用无参构造方式创建对象
  2. 有参构造器:
//<!--无参构造-->
<bean id="student" class="com.pojo.Student"></bean>
//<!--有参构造-->
<bean id="student01" class="com.pojo.Student">
    <constructor-arg index="0" name="name" type="java.lang.String" value="张三"></constructor-arg>
    <constructor-arg index="1" name="age" type="java.lang.Integer" value="25"></constructor-arg>
</bean>

index:形参的下标
name:形参的名字
type:形参类型(全路径)
value:形参的值
注意:xml文件中对象的所有构造方法都会在创建Spring容器时执行

2.2 通过属性注入方式

相当于先创建一个空对象,然后调用set方法完成属性的赋值

<bean id="student02" class="com.pojo.Student">
    <property name="name" value="李四"></property>
    <property name="age" value="22"></property>
</bean>

name:形参的名字
value:形参的值
注意:必须提供对象的set方法

2.3 通过工厂模式

传统方案:我们自己创建工厂,然后调用工厂创建对象的方法,获取生产的对象。
IOC方案:Spring容器创建工厂,并自动调用其创建的工厂的生产对象的方法,生产的对象直接存储在Spring容器中,我们直接从Spring容器中获取对象。

//<!--通过工厂创建对象-->
//<!--动态工厂-->
<bean id="studentFactory" class="com.pojo.StudentFactory"></bean>
<bean id="student03" factory-bean="studentFactory" factory-method="newIntance"></bean>
//<!--静态工厂-->
<bean id="student04" class="com.pojo.StudentStaticFactory" factory-method="newIntance"></bean>

1.动态工厂:
class:动态工厂全路径
factory-bean:动态工厂id
factory-method:动态工厂方法
2.静态工厂:
class:静态工厂全路径
factory-method:静态工厂方法

三、IOC的依赖注入DI

3.1 对象之间的依赖关系

我们在设计类对象时,会在类中声明其他类类型的属性,来调用其他类的资源完成当前类的功能处理,比如A类中声明B属性,在A类中就可以直接调用B
类的资源完成A类的功能开发,但是A对象被创建时,其B属性必须有值,否则空指针异常,我们将此种也就是A和B的关系称为对象之间的依赖关系(A依赖B).

3.2 依赖责任链

对象之间项目依赖形成的一条链式依赖关系.
D d = new D();
C c = new C(d);
B b = new B(c);
A a = new A(b);
A<---B<----C<----D

3.3 解决方案及代码实现

解决:让Spring容器根据对象之间的依赖关系,将依赖责任链上的所有对象全部配置为Bean对象,并且根据依赖关系完成对象之间的组装,将组装好的对象返回给用户使用。
代码实现

//<!--依赖注入-->
//<!--构造器方式-->
<bean id="student05" class="com.pojo.Student01">
    <constructor-arg index="0" name="name" type="java.lang.String" value="王五"></constructor-arg>
    <constructor-arg index="1" name="age" type="java.lang.Integer" value="30"></constructor-arg>
    <constructor-arg index="2" name="teacher" type="com.pojo.Teacher" ref="tea"></constructor-arg>
    //属性值为某个对象时,用ref赋值,值为该Bean对象的id
</bean>
<bean id="tea" class="com.pojo.Teacher">
    <property name="name" value="黎老师"></property>
    <property name="age" value="21"></property>
</bean>
//<!--属性注入方式-->
<bean id="student06" class="com.pojo.Student01">
    <property name="name" value="赵六"></property>
    <property name="age" value="28"></property>
    <property name="teacher" ref="tea"></property> //属性值为某个对象时,用ref赋值,值为该Bean对象的id
</bean>

ref:属性值为某个对象时,用ref赋值,值为该Bean对象的id

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
06-08 12:16
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务