Spring5核心之一:IOC容器

2024-03-26 15:32

本文主要是介绍Spring5核心之一:IOC容器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

IOC容器

  • 一、什么是IOC:
  • 二、IOC的底层原理:
  • 三、IOC思想基于IOC容器完成,IOC容器底层就是对象工厂。
  • 四、Spring 提供IOC容器实现两种方式:(两个接口)
  • 五、IOC操作之Bean管理:
    • 1、什么是Bean管理:
    • 2、IOC操作之Bean管理两种方式:
      • 1)基于XML配置文件方式:
      • 2)基于注解方式(以UserDao接口以及其实现类和UserService类为例)
    • 3、IOC操作Bean管理(工厂Bean)
    • 4、Bean的生命周期(加上后置处理器后共七步):

一、什么是IOC:

1)控制反转,把创建对象和对象的调用过程交给Spring 管理。
2)使用IOC的目的,为了降低耦合度。

二、IOC的底层原理:

XML解析、工厂模式、反射
IOC底层原理

三、IOC思想基于IOC容器完成,IOC容器底层就是对象工厂。

四、Spring 提供IOC容器实现两种方式:(两个接口)

(1)BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员使用
特点:加载配置文件的时候不会创建对象,在获取(使用)对象才去创建。

(2)ApplicationContext:BeanFactory接口的子接口。提供开发人员使用
特点:在加载配置文件的时候就创建对象

五、IOC操作之Bean管理:

1、什么是Bean管理:

Bean管理指的是两个操作:
Spring创建对象
Spring注入值:手动注入、自动装配

<!--创建对象、自动装配bean标签里面的autowire属性:属性值:byName:根据属性名字注入值,id的值必须与类里面的属性名称一样byType:根据属性的类型注入值。
-->
<bean id="employee" class="tianfei.Spring5.autowire.Employee" autowire="byType"><!--手动装配--><!--<property name="dept" ref="dept"></property>-->
</bean>

2、IOC操作之Bean管理两种方式:

1)基于XML配置文件方式:

首先创建一个User类:

 public class User {private Integer id;private String name;public User() {}public User(Integer id, String name) {this.id = id;this.name = name;}public void setId(Integer id) {this.id = id;}public void setName(String name) {this.name = name;}public void setAddress(String address) {this.address = address;}}

在XML配置文件中配置User对象:

<!--1、配置User对象id:为创建后的对象名,可自取class:为类的全路径-->
<bean id="user" class="tianfei.Spring5.User"><!--2、通过set方法注入属性通过property注入属性name :类里面的属性value :类里面属性对应的值--><property name="id" value="1"></property><property name="name" value="田飞"></property><!--通过有参构造注入属性--><constructor-arg name="id" value="2"></constructor-arg><constructor-arg name="name" value="张三"></constructor-arg><!--通过p名称空间注入属性使用前,需在xml标签属性中加入:xmlns:p="http://www.springframework.org/schema/p"--><bean id="user" class="tianfei.Spring5.User" p:id="1" p:name="李四"></bean>
</bean>

测试类:

public class testDemo {@Testpublic void test() {//1、加载Spring5 的xml配置文件ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");//2、获取配置创建的对象User user = context.getBean("usera", User.class);System.out.println(user);}
}

补充:在XML配置文件中引入外部文件(以jdbc.properties)

 <!--引入外部文件  jdbc.properties需在名称空间中加入:xmlns:context="http://www.springframework.org/schema/context"--><context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" ><property name="driverClassName" value="${prop.driverClassName}"></property><property name="url" value="${prop.url}"></property><property name="username" value="${prop.username}"></property><property name="password" value="${prop.password}"></property></bean>

jdbc.properties配置文件内容如下:

prop.driverClassName=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userdb
prop.username=root
prop.password=tianfei

2)基于注解方式(以UserDao接口以及其实现类和UserService类为例)

 public interface UserDao {public void add();}@Controller(value = "userDaoImpl")public class UserDaoImpl implements UserDao {@Overridepublic void add() {System.out.println("userdao add.......");}public UserDaoImpl() {}}
//value属性可以省略,如果省略则默认创建的对象名为  类名首字母小写  对象
@Service(value = "userService")
public class UserService {//基于注解注入基本类型数据@Value(value = "abc")private String str;//基于注解注入对象类型值
//    @Autowired  //根据类型注入
//    @Qualifier(value = "userDaoImpl")  //根据名称注入 需要和 Autowired 一起使用
//    private UserDao userDao;//    @Resource   //根据类型注入@Resource(name = "userDaoImpl")  //根据名称注入private UserDao userDao;public void add() {System.out.println("service add........");userDao.add();}public void test() {System.out.println("userDao = " + userDao);System.out.println(str);}
}
测试类:
public class test {@Testpublic void test1(){//测试使用注解创建对象//需要在mxl配置文件中配置:<context:component-scan base-package="tianfei.Spring5"></context:component-scan> ,来开启注解扫描组件//不需要添加set方法ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");UserService userService = context.getBean("userService", UserService.class);System.out.println(userService);userService.add();userService.test();}@Testpublic void test2(){//测试使用注解创建对象//不需要添加set方法//使用完全注解ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);UserService userService = context.getBean("userService", UserService.class);System.out.println(userService);userService.add();userService.test();}
}

使用完全注解时需要创建一个类作为配置类:

@Configuration      //作为配置类,代替xml配置文件,进行完全注解开发
@ComponentScan(value = {"tianfei.Spring5"})
public class SpringConfig {
}

3、IOC操作Bean管理(工厂Bean)

Spring有两种Bean:
1)普通Bean:在配置文件中定义bean类型就是返回值类型
2)工厂Bean:在配置文件中定义bean类型和返回值类型可以不一样
第一步:创建一个类,作为工厂bean,实现接口FactoryBean
第二步:实现接口中得方法,在实现的方法中定义返回的bean类型

    public class MyBean implements FactoryBean<User> {//用来改变返回对象(返回的对象可以和创建的对象不一样)@Overridepublic User getObject() throws Exception {return new User(1,"张三");}@Overridepublic Class<?> getObjectType() {return null;}@Overridepublic boolean isSingleton() {return false;}
}

创建 MyBean.xml文件添入:

<!--创建类的对象-->
<bean id="myBean" class="tianfei.Spring5.factorybean.MyBean"></bean>

测试类:

   public class test {@Testpublic void testMyBean() {//测试 FactoryBean 工厂ApplicationContext context = new ClassPathXmlApplicationContext("MyBean.xml");Book book = context.getBean("myBean",Book.class);System.out.println(book);}
}

4、Bean的生命周期(加上后置处理器后共七步):

(1)通过无参构造器创建bean实例(无参构造)
(2)为 bean的属性注入值或引用其他bean(set方法)
(3)把 bean 实例传递给 bean 后置处理器方法 postProcessBeforeInitialization
(4)调用 bean的初始化方法(需要自行配置初始化方法)
(5)把 bean 实例传递给 bean 后置处理器方法 postProcessAfterInitialization
(6)获取 bean实例对象
(7)当容器关闭时,调用 bean的销毁方法(需要自行配置销毁方法)

这篇关于Spring5核心之一:IOC容器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/848965

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2