面试问题浓缩总结 面试问题浓缩总结
  • Go
  • Java
  • C/C++
  • JavaScript/HTML
  • MySQL
  • Redis
  • MongoDB
  • 操作系统
  • 计算机网络
  • spring全家桶
  • mybatis
  • 中间件
  • 软件相关
  • 系统相关
  • 算法
  • 数据结构
  • 设计模式
  • CMU硕士经典100题
  • 剑指offer
  • 重点手撕代码
  • 程序员面试金典
  • 3月
  • 4月
  • 智力题
  • 业务问题
  • 一些技术
  • 安全相关
APP下载 (opens new window)
GitHub (opens new window)
  • Go
  • Java
  • C/C++
  • JavaScript/HTML
  • MySQL
  • Redis
  • MongoDB
  • 操作系统
  • 计算机网络
  • spring全家桶
  • mybatis
  • 中间件
  • 软件相关
  • 系统相关
  • 算法
  • 数据结构
  • 设计模式
  • CMU硕士经典100题
  • 剑指offer
  • 重点手撕代码
  • 程序员面试金典
  • 3月
  • 4月
  • 智力题
  • 业务问题
  • 一些技术
  • 安全相关
APP下载 (opens new window)
GitHub (opens new window)
  • spring全家桶

    • IOC
      • Spring IOC的四种注入方式
        • 接口注入
        • setter方法注入
        • 构造器注入
        • 注解注入
    • AOP
    • spring MVC
    • springSecurity
    • 面试问题
    • 循环依赖
    • 事务
    • 启动流程和配置
    • 注解相关
  • mybatis

  • 中间件

  • 框架
  • spring全家桶
小游
2021-03-23

IOC

IoC全称Inversion of Control,直译为控制反转

它不是什么技术,而是一种设计思想。在spring中,IOC就意味着我们把设计好的对象交给容器控制,而不是直接在内部控制。

说的简单点就是通过spring的容器,我们可以直接获取这个对象,而不用关心怎么创建和销毁(spring会自动帮我们创建和管理对象)

# Spring IoC的初始化过程

Spring IoC的初始化过程

# Spring IOC的四种注入方式

  1. 接口注入
  2. setter方法注入
  3. 构造方法注入
  4. 注解方式注入

# 接口注入

使用反射来获取对象并强制转换为某个对象

public class ClassA {  
  private InterfaceB clzB;  
  public void doSomething() { 
      // 这里获取class对象
    Ojbect obj = Class.forName(Config.BImplementation).newInstance();  
      // 这里我们对接口进行转换
    clzB = (InterfaceB)obj;
      // 这里执行方法
    clzB.doIt();   
  }  
1
2
3
4
5
6
7
8
9
10

# setter方法注入

其实就是写一个类,里面提供了set方法,然后我们到bean里面进行配置,这里我只说一下关键的配置部分

/**
 * setter方式注入Bean
 */
public void setHelloService(HelloService helloService) {
    this.helloService = helloService;
}
@Override
public void selfIntroduction() {
    // 向大家打招呼
    helloService.sayHello("大家好!");
}
1
2
3
4
5
6
7
8
9
10
11

然后配置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声明:
        该bean类似于javaConfig中的@Bean注解;
        用于创建bean的类通过class属性来指定,并且需要使用全限定的类名。
        通过id指定bean的ID。如果不显示指定,默认使用class的全限定名进行命名。
        eg:
        HelloServiceImpl#0,其#0是一个计数器的形式,
        用来区分相同类型的其他bean。
        使用自动化命名很方便,但是没有多少实际用处,还是建议自己给bean显示设定ID。
    -->
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>
    <!-- setter注入bean -->
    <bean id="selfIntroductionService" class="com.jpeony.spring.setter.SelfIntroductionServiceImpl">
        <property name="helloService" ref="helloService"/>
    </bean>
 
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

最后我们就可以使用了

@Test
public void testBean() {
    // 根据spring配置文件创建应用上下文
    ApplicationContext context =
    new ClassPathXmlApplicationContext("applicationContext-Setter-Bean.xml");
    // 从容器中获取bean
    SelfIntroductionService selfIntroductionService
    = (SelfIntroductionService) context.getBean("selfIntroductionService");
    // 调用自我介绍
    selfIntroductionService.selfIntroduction();
}
1
2
3
4
5
6
7
8
9
10
11

# 构造器注入

和setter注入差不多,但是我们不使用set方法,而是使用构造器来实现

import com.tgb.spring.dao.UserDao;    
    
    public class UserManagerImpl implements UserManager{    
        
        private UserDao userDao;    
        
        //使用构造方式赋值    
        public UserManagerImpl(UserDao userDao) {    
            this.userDao = userDao;    
        }    
        
        @Override    
        public void addUser(String userName, String password) {    
        
            userDao.addUser(userName, password);    
        }    
    } 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

然后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"    
             xmlns:aop="http://www.springframework.org/schema/aop"    
             xmlns:tx="http://www.springframework.org/schema/tx"    
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd    
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd    
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">    
        
        <!-- 使用spring管理对象的创建,还有对象的依赖关系 -->    
        <bean id="userDao4Mysql" class="com.tgb.spring.dao.UserDao4MysqlImpl"/>    
        
        <bean id="userDao4Oracle" class="com.tgb.spring.dao.UserDao4OracleImpl"/>    
            
        <bean id="userManager" class="com.tgb.spring.manager.UserManagerImpl">    
            <!-- (1)userManager使用了userDao,Ioc是自动创建相应的UserDao实现,都是由容器管理-->    
            <!-- (2)在UserManager中提供构造函数,让spring将UserDao实现注入(DI)过来 -->    
            <!-- (3)让spring管理我们对象的创建和依赖关系,必须将依赖关系配置到spring的核心配置文件中 -->    
        
            <constructor-arg ref="userDao4Oracle"/>    
        </bean>    
            
    </beans>   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 注解注入

@Autowired  
private UserDAO userDAO;  
1
2

参考:

  1. Spring IOC介绍与4种注入方式 - 知乎 (zhihu.com) (opens new window)
  2. 【Spring基础】IOC使用Setter依赖注入_yhl_jxy的博客-CSDN博客 (opens new window)
编辑 (opens new window)
上次更新: 2021/04/10, 14:27:26
AOP

AOP→

Theme by Vdoing | Copyright © 2021-2021 小游
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式