【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编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis