Spring学习③__Bean管理

目录

  • IOC接口
    • ApplicationContext 详解
      • IOC操作Bean管理
        • 基于xml方式
          • 基于xml方式创建对象
          • 基于xml方式注入属性
            • 使用set方法进行注入
            • 通过有参数的构造进行注入
            • p 名称空间注入(了解)
          • 基于xml方式注入其他类型属性
          • xml 注入数组类型属性

IOC接口

  • IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
  • Spring提供IOC容器实现的两种方式:
    • ApplicationContext :BeanFactory接口的子接口,提供更多更强大的功能,一般由开发人员使用 在这里插入图片描述
    • BeanFactory :IOC容器基本实现,是Spring里面内部使用接口,不提供开发人员进行使用在这里插入图片描述
    • BeanFactory 在加载配置文件的时候,它不会把里面的对象创建,它只会加载我们的配置文件,当在获取的时候它才会创建对象。什么时候用的时候才开始创建对象
    • ApplicationContext 在加载配置文件的时候,就会把配置文件对象进行创建

ApplicationContext 详解

在这里插入图片描述

  • FileSystemXmlApplicationContext : 配置文件为系统盘的文件
  • ClassPathXmlApplicationContext : 配置为类路径下的文件

IOC操作Bean管理

  • Bean管理指的是两个操作
    • ① Spring创建对象 :Spring通过Xml解析再通过工厂模式来进行创建对象
    • ② Spring 注入属性 :Spring可以使用类似于Set对象的操作对对象或者字注入值
  • Bean管理操作有两种方式
    • ①基于xml配置文件方式实现
    • ②基于注解方式实现
基于xml方式
基于xml方式创建对象

在这里插入图片描述
① 在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
② 在bean标签有很多属性,介绍常用的属性

  • id属性 : 获取对象中的唯一的标识
  • class属性 :类全路径(包类路径)
  • name属性 :name的作用与id一样,name属性可以添加特殊符号
    ③ 创建对象的时候,默认也是执行无参的构造方法完成对象的创建
基于xml方式注入属性

DI : 依赖注入,就是注入属性

使用set方法进行注入

在这里插入图片描述
在spring配置文件配置对象创建,配置属性注入(使用Set方式注入)
在这里插入图片描述

通过有参数的构造进行注入

在这里插入图片描述
在spring配置文件配置对象创建,配置属性注入(使用有参数构造注入)
在这里插入图片描述

p 名称空间注入(了解)

使用 p 名称空间注入,可以简化基于 xml 配置方式

<?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  //添加 p 名称空间在配置文件中
http://www.springframework.org/schema/beans/spring-beans.xsd">//进行属性注入,在 bean 标签里面进行操作<bean id="book" class="com. atguigu. spring5.Book" p:bname="九阳神功"
p: bauthor="无名氏">×/bean></beans>
基于xml方式注入其他类型属性
  1. 字面量
  • null 值
<property name="address"><null/>
</property> 
  • 属性值包含特殊符号
<!--属性值包含特殊符号1 把<>进行转义 &lt; &gt;2 把带特殊符号内容写到 CDATA
-->
<property name="address"><value><![CDATA[<<南京>>]]></value>
</property>
  1. 注入属性-外部 bean (外部调用对象的方法)
    • 创建两个类 ,service 类和 dao 类
    • 在 service 调用 dao 里面的方法
    • 在 spring 配置文件中进行配置
public class UserService {//创建UserDao类型属性,生成set方法private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void add() {System.out.println("service add...............");userDao.update();}
}
public class UserDao {public void update() {System.out.println("dao update...........");}
}
    • 通过springxml配置操作
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--1 service和dao对象创建--><bean id="userService" class="com.atguigu.spring5.service.UserService"><!--注入userDao对象name属性:类里面属性名称ref属性:创建userDao对象bean标签id值--><property name="userDao" ref="userDaoImpl"></property></bean><bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>
</beans>
@Test
public void testBean1() {//1 加载spring配置文件ApplicationContext context =new ClassPathXmlApplicationContext("bean2.xml");//2 获取配置创建的对象UserService userService = context.getBean("userService", UserService.class);userService.add();}
  1. 注入属性-内部 bean (内部属性包含对象)

① 一对多关系:部门和员工一个部门有多个员工,一个员工属于一个部门部门是一,员工是多
②在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

部门类

//部门类
public class Dept {private String dname;public void setDname(String dname) {this.dname = dname;}@Overridepublic String toString() {return "Dept{" +"dname='" + dname + '\'' +'}';}
}

员工类

//员工类
public class Emp {private String ename;private String gender;//员工属于某一个部门,使用对象形式表示private Dept dept;//生成dept的get方法public Dept getDept() {return dept;}public void setDept(Dept dept) {this.dept = dept;}public void setEname(String ename) {this.ename = ename;}public void setGender(String gender) {this.gender = gender;}public void add() {System.out.println(ename+"::"+gender+"::"+dept);}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--内部bean--><bean id="emp" class="com.atguigu.spring5.bean.Emp"><!--设置两个普通属性--><property name="ename" value="lucy"></property><property name="gender" value=""></property><!--设置对象类型属性--><property name="dept"><bean id="dept" class="com.atguigu.spring5.bean.Dept"><property name="dname" value="安保部"></property></bean></property></bean>
</beans>
    @Testpublic void testBean2() {//1 加载spring配置文件ApplicationContext context =new ClassPathXmlApplicationContext("application.xml");//2 获取配置创建的对象Emp emp = context.getBean("emp", Emp.class);emp.add();}

在这里插入图片描述

  1. 注入属性-级联赋值 (都可以达到注入的作用)

第一种方法 直接赋值一个对象

<!--级联赋值--> 
<bean id="emp" class="com.atguigu.spring5.bean.Emp"><!--设置两个普通属性--><property name="ename" value="lucy"></property><property name="gender" value=""></property><!--级联赋值--><property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.atguigu.spring5.bean.Dept"><property name="dname" value="财务部"></property>
</bean>

第二种方法 赋值里面的一个属性值
//生成dept的get方法

public Dept getDept(){
return dept:
}
<!--级联赋值--> 
<bean id="emp" class="com.atguigu.spring5.bean.Emp"><!--设置两个普通属性--><property name="ename" value="lucy"></property><property name="gender" value=""></property><!--级联赋值--><property name="dept" ref="dept"></property><property name="dept.dname" value="技术部"></property>
</bean> 
<bean id="dept" class="com.atguigu.spring5.bean.Dept"><property name="dname" value="财务部"></property>
</bean>
xml 注入数组类型属性
  1. 注入数组类型属性
    创建类,定义数组、list、map、set 类型属性,生成对应 set 方法
package com.tde.example.Entity;import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;public class Student {//1 数组类型属性private String[] courses;//2 list集合类型属性private List<String> list;//3 map集合类型属性private Map<String, String> maps;//4 set集合类型属性private Set<String> sets;public void setSets(Set<String> sets) {this.sets = sets;}public void setCourses(String[] courses) {this.courses = courses;}public void setList(List<String> list) {this.list = list;}public void setMaps(Map<String, String> maps) {this.maps = maps;}public void test() {System.out.println(Arrays.toString(courses));System.out.println(list);System.out.println(maps);System.out.println(sets);}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--1 集合类型属性注入--><bean id="stu" class="com.atguigu.spring5.collectiontype.Stu"><!--数组类型属性注入--><property name="courses"><array><value>java课程</value><value>数据库课程</value></array></property><!--list类型属性注入--><property name="list"><list><value>张三</value><value>小三</value></list></property><!--map类型属性注入--><property name="maps"><map><entry key="JAVA" value="java"></entry><entry key="PHP" value="php"></entry></map></property><!--set类型属性注入--><property name="sets"><set><value>MySQL</value><value>Redis</value></set></property></bean>
</beans>
@Test
public void testCollection1() {ApplicationContext context =new ClassPathXmlApplicationContext("bean5.xml");Stu stu = context.getBean("stu", Stu.class);stu.test();
}

在这里插入图片描述
2. 在集合里面设置对象类型值

<!--创建多个 course 对象--> 
<bean id="course1" class="com.atguigu.spring5.collectiontype.Course"><property name="cname" value="Spring5 框架"></property>
</bean> 
<bean id="course2" class="com.atguigu.spring5.collectiontype.Course"><property name="cname" value="MyBatis 框架"></property>
</bean>
<!--注入 list 集合类型,值是对象--><property name="courseList"><list><ref bean="course1"></ref><ref bean="course2"></ref></list>
</property>
//学生所学多门课程
private List<Course> courseList;
public void setCourseList(List<Course> courseList) {this.courseList = courseList;
}

在这里插入图片描述

  1. 把集合注入部分提取出来
  • 在 spring 配置文件中引入名称空间 util
  • 使用 util 标签完成 list 集合注入提取

Book类

package com.atguigu.spring5.collectiontype;import java.util.List;public class Book {private List<String> list;public void setList(List<String> list) {this.list = list;}public void test() {System.out.println(list);}
}
<?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:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><!--1 提取list集合类型属性注入--><util:list id="bookList"><value>易筋经</value><value>九阴真经</value><value>九阳神功</value></util:list><!--2 提取list集合类型属性注入使用--><bean id="book" class="com.atguigu.spring5.collectiontype.Book"><property name="list" ref="bookList"></property></bean>
</beans>
@Test
public void testCollection2() {ApplicationContext context =new ClassPathXmlApplicationContext("bean6.xml");Book book1 = context.getBean("book", Book.class);Book book2 = context.getBean("book", Book.class);book1.test();book2.test();System.out.println(book1);System.out.println(book2);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/191537.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

机器人制作开源方案 | 智能快递付件机器人

一、作品简介 作者&#xff1a;贺沅、聂开发、王兴文、石宇航、盛余庆 单位&#xff1a;黑龙江科技大学 指导老师&#xff1a;邵文冕、苑鹏涛 1. 项目背景 受新冠疫情的影响&#xff0c;大学校园内都采取封闭式管理来降低传染的风险&#xff0c;导致学生不能外出&#xff0c…

原来机械硬盘比内存慢10万倍

我们都知道机械硬盘的速度很慢&#xff0c;内存的速度很快&#xff0c;那么不同存储器之间的差距到底有多大呢&#xff1f; 我们先来看一幅图&#xff1a; CPU访问寄存器的时间是0.3纳秒&#xff0c;访问L1高速缓存的时间是1纳秒&#xff0c;访问L2高速缓存的时间是4纳秒… 秒…

二维码智慧门牌管理系统升级解决方案:门牌聚合,让管理更便捷!

文章目录 前言一、传统门牌管理系统的瓶颈二、地图门牌聚合展示的优势三、地图门牌聚合展示的实现方法四、智慧门牌管理系统的未来发展 前言 随着城市的发展和建设&#xff0c;对于地址信息的管理变得越来越重要。而智慧门牌管理系统作为管理地址信息的重要工具&#xff0c;其…

Selenium自动化测试框架

一.Selenium概述 1.1 什么是框架? 框架&#xff08;framework&#xff09;是一个框子——指其约束性&#xff0c;也是一个架子——指其支撑性。是一个基本概念上的 结构用于去解决或者处理复杂的问题。 框架是整个或部分系统的可重用设计&#xff0c;表现为一组抽象构件及…

盘点60个Python各行各业管理系统源码Python爱好者不容错过

盘点60个Python各行各业管理系统源码Python爱好者不容错过 学习知识费力气&#xff0c;收集整理更不易。 知识付费甚欢喜&#xff0c;为咱码农谋福利。 源码下载链接&#xff1a;https://pan.baidu.com/s/1VdAFp4P0mtWmsA158oC-aA?pwd8888 提取码&#xff1a;8888 项目名…

java:IDEA中的Scratches and Consoles

背景 IntelliJ IDEA中的Scratches and Consoles是一种临时的文件编辑环境&#xff0c;用于写一些文本内容或者代码片段。 其中&#xff0c;Scratch files拥有完整的运行和debug功能&#xff0c;这些文件需要指定编程语言类型并且指定后缀。 举例&#xff1a;调接口 可以看到…

GCD:异步同步?串行并发?一文轻松拿捏!

GCD 文章目录 GCD进程线程进程与线程的关系进程与线程的区别 任务&#xff08;执行的代码&#xff09;队列线程与队列的关系 队列任务**同步执行任务&#xff08;sync&#xff09;**辅助方法**异步执行任务&#xff08;async)**总结栅栏任务迭代任务 队列详细属性QoSAttributes…

ARDUINO UNO 12颗LED超酷流水灯效果

效果代码&#xff1a; #define t 30 #define t1 20 #define t2 100 #define t3 50 void setup() { // set up pins 2 to 13 as outputs for (int i 2; i < 13; i) { pinMode(i, OUTPUT); } } /Effect 1 void loop() { effect_1(); effect_1(); effect_…

【Linux】第十七站:进程创建与进程终止

文章目录 一、进程创建1.fork函数2.写时拷贝3.批量化创建多个进程 二、进程终止1.进程退出场景2.进程退出的方法&#xff08;1&#xff09;exit和return&#xff08;2&#xff09;_exit和exit 一、进程创建 1.fork函数 在linux中fork函数时非常重要的函数&#xff0c;它从已存…

深信服AC密码认证(外部认证:LDAP认证)

拓扑图 搭建好自己AD域服务器&#xff0c;我搭建的服务器域名叫做liyanlongyu.com&#xff0c;如何搭建这里我就不做演示了 一.在AC中添加自己AD域服务器 二.添加LDAP认证策略 验证&#xff1a; 未认证发现&#xff0c;无法上网 点击网页&#xff0c;弹出认证页面 认证后&…

【C++】容器string的常用成员函数接口

目录 string - C Reference 1 容量相关 1.1 size/length 1.2 capacity 1.3 resize 1.4 reserve 1.5 empty 2 运算符重载 2.1 operator 2.2 operator[] 2.3 operator&#xff08;非成员函数&#xff09; 2.4 operator 2.5 operator>> && operator<…

数学建模 | 灰色预测原理及python实现

目录 一、灰色预测的原理 二、灰色预测的应用及python实现 一、灰色预测的原理 灰色预测是以灰色模型为基础&#xff0c;灰色模型GM(n,h)是微分方程模型&#xff0c;可用于描述对象做长期、连续、动态的反应。其中&#xff0c;n代表微分方程式的阶数&#xff0c;h代表微分方…