【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

相关文章

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c