【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

相关文章

Java 8 Stream filter流式过滤器详解

《Java8Streamfilter流式过滤器详解》本文介绍了Java8的StreamAPI中的filter方法,展示了如何使用lambda表达式根据条件过滤流式数据,通过实际代码示例,展示了f... 目录引言 一.Java 8 Stream 的过滤器(filter)二.Java 8 的 filter、fi

Java中实现订单超时自动取消功能(最新推荐)

《Java中实现订单超时自动取消功能(最新推荐)》本文介绍了Java中实现订单超时自动取消功能的几种方法,包括定时任务、JDK延迟队列、Redis过期监听、Redisson分布式延迟队列、Rocket... 目录1、定时任务2、JDK延迟队列 DelayQueue(1)定义实现Delayed接口的实体类 (

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

将java程序打包成可执行文件的实现方式

《将java程序打包成可执行文件的实现方式》本文介绍了将Java程序打包成可执行文件的三种方法:手动打包(将编译后的代码及JRE运行环境一起打包),使用第三方打包工具(如Launch4j)和JDK自带... 目录1.问题提出2.如何将Java程序打包成可执行文件2.1将编译后的代码及jre运行环境一起打包2

Java使用Tesseract-OCR实战教程

《Java使用Tesseract-OCR实战教程》本文介绍了如何在Java中使用Tesseract-OCR进行文本提取,包括Tesseract-OCR的安装、中文训练库的配置、依赖库的引入以及具体的代... 目录Java使用Tesseract-OCRTesseract-OCR安装配置中文训练库引入依赖代码实

MySQL 中的服务器配置和状态详解(MySQL Server Configuration and Status)

《MySQL中的服务器配置和状态详解(MySQLServerConfigurationandStatus)》MySQL服务器配置和状态设置包括服务器选项、系统变量和状态变量三个方面,可以通过... 目录mysql 之服务器配置和状态1 MySQL 架构和性能优化1.1 服务器配置和状态1.1.1 服务器选项

Java中对象的创建和销毁过程详析

《Java中对象的创建和销毁过程详析》:本文主要介绍Java中对象的创建和销毁过程,对象的创建过程包括类加载检查、内存分配、初始化零值内存、设置对象头和执行init方法,对象的销毁过程由垃圾回收机... 目录前言对象的创建过程1. 类加载检查2China编程. 分配内存3. 初始化零值4. 设置对象头5. 执行

SpringBoot整合easy-es的详细过程

《SpringBoot整合easy-es的详细过程》本文介绍了EasyES,一个基于Elasticsearch的ORM框架,旨在简化开发流程并提高效率,EasyES支持SpringBoot框架,并提供... 目录一、easy-es简介二、实现基于Spring Boot框架的应用程序代码1.添加相关依赖2.添

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入