Spring依赖注入和ioc在spring中的实现方式

目录

一、依赖注入

1.IOC思想

2.什么是依赖注入?

3.实例化对象中有pojo类型属性

二、IOC在Spring中的实现方式

1.获取bean的三种方式

1.1根据bean的id获取

1.2根据bean的类型获取(最常用,因为在IOC容器中,一个类型的bean只需要配置一次就够了)

1.3根据bean的id和类型进行获取

1.4根据类所实现的类的类型获取(通过接口获取)


一、依赖注入

1.IOC思想

IOC:Inversion Of Control  翻译:控制反转

举个例子:

自己做饭:买菜,洗菜,切菜,炒菜~~~~等等步骤和细节都需要自己清楚的知道

点外卖:下单,等,吃,不需要关心创建细节

controller----->Service------>dao

controller依赖于Service,Service依赖于dao

2.什么是依赖注入?

        依赖注入即程序在运行时,希望给某个对象所依赖的对象赋值,就称为依赖注入(需要什么,传递什么,传递的任务由Spring完成

简单来说就是对Controller所依赖的对象进行赋值,依赖哪个对象,spring就对哪个对象进行赋值

注入(赋值)方式由三种:

  1. setter方法注入
  2. 构造方法注入
  3. 注解注入

 示例1:setter注入,通过<property>标签给属性赋值()

基本数据类型的成员属性直接通过value赋值。

对应的bean:

<bean id="person" class="cn.edu.xysf.ssm.entity.Person">
        <property name="name" value="李晶晶"></property>
        <property name="age" value="23"></property>
        <property name="gender" value="女"></property>
    </bean>

测试类:

 @Test
    public void PersonTest(){
        //1。需要实例化IOC容器,子类
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        //2.从IOC容器中拿到想要的对象
       Person person = (Person)context.getBean("person");
        System.out.println(person);
}

运行结果:

示例2:构造器注入

        假如实体类有五个属性,但是只给四个属性赋值,此时可以使用name,name可以为value指定它所赋值的属性

对应的bean:

  <bean id="studentThree" class="com.zh.spring.pojo.Student">
        <constructor-arg value="10002"></constructor-arg>
        <constructor-arg  value="李四"></constructor-arg>
        <constructor-arg  value="男"></constructor-arg>
        <constructor-arg  value="22" name="age"></constructor-arg>
    </bean>
   @Test
    public void testDI02(){
        //获取IOC容器
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
        //获取bean
        Student studentThree = ioc.getBean("studentThree", Student.class);
        System.out.println(studentThree);

    }

注入(赋值)的数据类型有三种:

  1. 基本数据类型和String类型
  2. 普通POJO类型(必须在Spring的配置文件中出现过的bean)
  3. 复杂类型(集合类型)
3.实例化对象中有pojo类型属性

如果Person类中有一个pojo类型的属性,假如是idCard,则在Person类中添加这个POJO属性,然后新建一个IdCard类

 

 添加对应的bean

 <bean name="idCard_1" class="cn.edu.xysf.ssm.entity.IDCard">
        <property name="id" value="123456789"></property>
        <property name="pubDep" value="xxx公安局"></property>
        <property name="vailTime" value="2038-11-09"></property>
    </bean>

运行结果:

二、IOC在Spring中的实现方式

  1. BeanFactory实现:基本实现,spring内部使用的接口,面向spring本身,不对开发人员开放
  2. ApplicationContext实现:BeanFactory的子接口,功能更多,面向开发人员。

以上只是接口,没有具体的实现方法。

ClassPathXMLApplicationContext:类路径下的XML配置文件,推荐使用。

FileSystemXmlApplicationContext:文件系统路径下的XML配置文件,不推荐使用,因为之后的项目会在不同的电脑上运行,配置文件不一定存在于每个电脑上

目前为止spring配置文件的名字可以随便起,但是到了ssm整合时会有固定的名字。

配置文件的作用就是获取IOC容器,底层通过反射和工厂模式实现

1.获取bean的三种方式
 <bean id="studentOne" class="com.zh.spring.pojo.Student"></bean>
1.1根据bean的id获取

        根据bean的id获取时,实例化对象中一定要有无参构造方法,否则会报错(NoSuchMethodException)。

 

 

 @Test
    public void testIOC(){
        //获取IOC容器
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
        //根据id获取bean对象
        Student studentOne = (Student) ioc.getBean("studentOne");
        System.out.println(student);
    }
1.2根据bean的类型获取(最常用,因为在IOC容器中,一个类型的bean只需要配置一次就够了)

        通过类型获取到的对象,返回值就是该类型的实例化对象,

注意:根据类型获取bean时,要求IOC容器中有且只有一个bean,否则会报错(NoUniqueBeanDefinitionException)。

 <bean id="studentOne" class="com.zh.spring.pojo.Student"></bean>
    <bean id="studentTwo" class="com.zh.spring.pojo.Student"></bean>

 

  @Test
    public void testIOC(){
        //获取IOC容器
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
        //根据类型获取
        Student student = ioc.getBean(Student.class);
        System.out.println(student);
    }
1.3根据bean的id和类型进行获取
  @Test
    public void testIOC(){
        //获取IOC容器
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
   
        //根据bean的id和类型进行获取
        Student studentOne = ioc.getBean("studentOne", Student.class);
        System.out.println(studentOne);
    }
1.4根据类所实现的类的类型获取(通过接口获取)
   @Test
    public void testIOC(){
        //获取IOC容器
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    
        //根据接口实获取
        Person person = ioc.getBean(Person.class);
        System.out.println(person);

    }
结论:根据类型来获取bean时,在满足bean唯一性的前提下
其实只是看:【对象 instanceof 指定的类型 】的返回值
只要返回的时true既可以认定和类型匹配,能够获取到
即通过bean的类型、bean所继承的类的类型,bean所实现的类的类型都可以获取到bean

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

simpleHan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值