【Spring篇】一.spring及springBean的配置

2024-03-28 18:38
文章标签 java 配置 spring springbean

本文主要是介绍【Spring篇】一.spring及springBean的配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

IOC (控制反转)

  • 将控制权交给Spring容器,要使用的时候找spring容器也获取依赖对象

  • 应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护

  • DI (依赖注入) 控制反转的一种实现方式

  • ioc目的:创建对象且组装对象之间的关系。spring容器在初始化的时候会创建一系列对象,同时将对象之间的依赖关系通过注入方式住址起来

    扩展
     q:控制反转,哪些方面的控制被反转了?a:获得依赖对象的过程被反转了。获得对象的过程不是由我们来创建,而是通过spring容器来注入,即依赖注入:由spring容器在运行期间,动态地将某种依赖关系注入对象之中。
    
    文解
     房屋中介	 --   ioc中介		 --   容器找房子		 --   返回对象入住		 --   使用对象
    

Bean配置

1. spring.xml配置 (也可以applicationContext.xml)
  • java项目放src下
  • maven项目放resources下
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--<bean></bean>--><bean>
基础包org.springframework.beansorg.springframework.context
初始化
 // 通过当前相对路径加载spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");// 通过当前绝对路径加载spring容器// ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
	<bean id="student" class="com.wpj.bean.Student"></bean>
 	@Testpublic void showSpringBean(){// 通过当前相对路径加载spring容器ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");// 根据id获取beanStudent student = (Student) ac.getBean("student");System.out.println(student);}

2. Spring注入

在启动spring容器加载bean配置的时候,完成对变量的赋值行为
2.1 设值注入
2.1.1 javaBean
	// 定义beanpublic class Student{//定义属性private String name;private int age;// 提供getter和setterpublic String getName() { return name; }public void setName(String name) { this.name = name; }public int getAge() { return age; }public void setAge(int age) { this.age = age; }}
2.1.2 spring.xml
	<bean id="student" class="com.wpj.bean.student"><property name="name" value="jieKaMi"></property><property name="age" value="22"></property></bean>
2.1.3 test
 	@Testpublic void showSpringBean(){// 通过当前相对路径加载spring容器ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");// 根据id获取beanStudent student = (Student) ac.getBean("student");System.out.println(student);}
2.2 构造注入
2.2.1 javaBean
	public class Student{//定义属性private String name;private int age;// 提供构造方法public Student(){}public Student(String name, int age){this.name = name;this.age = age;}}
2.2.2 spring.xml
	<bean id="student"  class="com.wpj.bean.student"><constructor-arg name="name" value="jieKaMi"></constructor-arg><constructor-arg name="age" value="18"></constructor-arg></bean>
2.2.3 test
 	@Testpublic void showSpringBean(){// 通过当前相对路径加载spring容器ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");// 根据id获取beanStudent student = (Student) ac.getBean("student");System.out.println(student);}

3 bean的配置项

- id		 bean的唯一标识
- class 	 实例对象的全路径类名 (必须有)
	<bean id="student" class="com.wpj.bean.Student"></bean>
-scope	作用域注: 需要在同一个spring容器中常用:scope="prototype"  多例  如果scope=“prototype”则不生效scope="singleton"  单例  默认scope="request"	   每次http请求创建一个实例且仅在当前request实例内scope="session"    每次http请求创建一个实例且仅在当前session实例scope="global session"  基于portlet的web中有效 (portlet定义了global session),如果是在web中,同session
	<!-- spring容器在进行输出prototype的bean对象时,会每次都重新生成一个新的对象给请求方 --><bean id="student" class="com.wpj.bean.Student" scope="prototype"></bean><!-- singleton类型的bean定义从容器启动到第一次被请求而实例化开始,只要容器不销毁或退出,该类型的bean的单一实例就会一直存活 --><bean id="student" class="com.wpj.bean.Student" scope="singleton "></bean>
- constructor-arg  设置要设置全部
- property         要设哪个就设哪个
	<!-- 构造方法注入:时效性最好 --><bean id="student" class="com.wpj.bean.Student"><constructor-arg name="name" value="jieKaMi"></constructor-arg><constructor-arg name="age" value="18"></constructor-arg></bean><!-- set方法注入:灵活性好 --><bean id="student" class="com.wpj.bean.Student"><property name="name" value="jieKaMi"></property><property name="age" value="22"></property></bean>
- lazy-init 懒加载lazy-init="true"	 默认lazy-init="false"
	<!-- 即时加载 容器启动立刻创建对象 消耗资源,有利于提前发现错误 --><bean id="student" class="com.wpj.bean.Student" lazy-init="false"></bean><!-- 懒加载 真正使用对象的时候才创建对象 节省资源,但不利于提前发现错误 --><bean id="student" class="com.wpj.bean.Student" lazy-init="true"></bean>
- 初始化与销毁方法init-method=""destroy-method=""	

这篇关于【Spring篇】一.spring及springBean的配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2