详谈对Spring IOC的理解

本文深入解析了Spring框架中的IOC控制反转和DI依赖注入原理,详细介绍了如何使用XML配置和注解方式实现bean的管理和依赖注入,包括配置示例和运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、IOC控制反转和DI依赖注入
1.控制反转,字面可以理解为:主动权的转移,原来一个应用程序内的对象是类通过new去主动创建并实例化的,对对像创建的主动权在程序代码中。程序不仅要管理业务逻辑也要管理对的象创建和依赖关系。这是很累的,也跟软件工程 “低耦合高内聚” 的概念不十分符合。
在这里插入图片描述
有了spring的ioc容器之后,对象的实例化和依赖关系管理都由IOC容器进行统一管理,主体类只要依赖ioc容器就够了,需要啥,容器会给他注入进去,也就是只要声明对象不用再主动去new,ioc容器帮忙把相应的对象注入到声明对象中,使其变成实例化对象。(类似主体类提供一个躯体,ioc容器把灵魂注入进去,使其变成一个生命体,激活他),这样创建对象的主动权就转移交接了。
在这里插入图片描述
二、使用xml配置方式实现IOC
1.在ioc容器中配置了dao实现类和service类的bean,在容器加载的时候就会实例化这些bean到内存中。(bean.xml配置如下)

<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  ">  
<!-- BookDao Bean -->  
<bean id="bookDao" class="com.study.DaoImpl.BookDaoImpl"></bean>  
<!-- BookService Bean-->  
<bean id="bookService" class="com.study.Service.BookService">   
<!-- BookService中的声明了BookDao对象,通过ref属性将BookDao的bean注入到对象中 -->    <property name="bookDao" ref="bookDao"/>  
</bean>
</beans>

2.service类中需要用
到dao类的实例(正常情况下需要new一个dao类对象),但是用ioc容器接管后只需要声明dao接口对象即可,然后写一个dao对象的set方法。(要注入的对象必须要有set方法,否则将报错 Bean property ‘bookDao’ is not writable or has an invalid setter method)因为spring注入是根据反射机制实现的,他在反射注入的时候会调用该方法名的set方法,如果set方法写错,或者根本没写,那么注入就会失败。(BookService类如下)
public class BookService {
private BookDao bookDao;
public BookService() {
System.out.println(“BookService实例化”);
}
public void setBookDao(BookDao bookDao) {
System.out.println(“BookService属性初始化装配成功”);
this.bookDao = bookDao;
}
public void storeBook(String bookname){
System.out.println(“图书上架”);
System.out.println(bookDao.addBook(bookname));
}
}
如上代码:BookSerivce类需要用到BookDao对象,但是却没有new对象,只有一个set方法,这个set方法就是ioc容器注入的入口(必不可少)
3. 此处我们用ApplicationContext作为容器,初始化配置文件,然后从容器中根据id名取出容器中已经帮我们实例化好的对象。

public class TestDmeo {
 BookService bookService;
  @Test
   public void testStoreBook(){
     System.out.println("容器初始化");
       ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
      bookService = (BookService) app.getBean("bookService");//将对象注入到声明好的BookService对象中。(bookService就是配置文件中的id)
      bookService.storeBook("Spring MVC"); 
      }
     }

在这里插入图片描述
4.dao类和实现类如下:
接口类:

public interface BookDao {
  public String addBook(String BookName);
   }

实现类:

public class BookDaoImpl implements BookDao {
 public BookDaoImpl() { 
 System.out.println("BookDao实例化");
  } 
  public String addBook(String BookName) {
    return BookName+"添加成功";
     }
   }

5.运行测试结果:
在这里插入图片描述
6.大体思路如下图:
在这里插入图片描述
程序中除了初始化容器用了new对象,其余的基本没有new的存在。
二、注解方式配置IOC
注解配置方式目的和xml配置的目的一样,都是为了实现bean的创建。常用的注解如下:
@Component 在类定义之前添加@Component注解,他会被spring容器识别,并转为bean。
@Repository 对Dao实现类进行注解 (特殊的@Component)
@Service 用于对业务逻辑层进行注解, (特殊的@Component)
@Controller 用于控制层注解 , (特殊的@Component)
装配注解如下:
@Autowired 默认按照类型装配注入,想按照名称来装配的话要结合@Quapfier(“name”)一起使用,使用@Autowired注解可以不用set方法。@Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个
@Quapfier(“name”) 中的name是bean的名字,也就是id,和@Autowired可以作为限定专配对象的名称
@Resource 默认按照名称装配注入,当找不到对应名成的bean的时候就按照类型匹配,如果还是找不到的话就会报错,@Autowired是spring提供的,@Resource是javaee提供,使用@Resource可以减少对spring的依赖
范例:
1.例子同上,只是配置bean的方式从xml文件中转移到了代码中,用注解体现。
在这里插入图片描述
2.除了把配置文件中变成对应的注解外,另外一个区别在于,bean.xml文件中的修改,需要做如下,配置才能够使注解生效
在这里插入图片描述
context的配置有如下方法:
1.仅扫描特定包下的特定类:
<context:component-scan base-package=“com.study” resource-pattern=“Service/B*.class”/>
这是扫描Service包下B开头的所有类。
2.使用<context:include-filter …/>和<context:exclude-filter …/>配置那些需要和不需要的扫描的包在这里插入图片描述

<!-- 容器扫描包下的注解配置组件 -->  
<context:component-scan base-package="com.study" use-default-filters="false">    
<context:include-filter type="aspectj" expression="com.study.Service.*"/> <!-- 模糊过滤 -->    
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/><!-- 过滤指定的注解 -->    
<context:include-filter type="assignable" expression="com.study.Service.BookService"/><!-- 过滤指定的类或接口,路径要完整,如果是接口的话,所有派生类都会被过滤 -->
<context:include-filter type="regex" expression="com.*"/>
</context:component-scan>

<context:exclude-filter …/>要配在<context:include-filter …/>的后面。
对标阿里P6架构师
获取更多学习资料,可以扫码进群,或关注公众号。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值