首页IT科技bean在spring中的两种管理方式(学习笔记——Spring管理第三方bean;Spring中Bean的作用域;Spring中Bean的生命周期;Spring中bean的后置处理器;Spring中自动装配)

bean在spring中的两种管理方式(学习笔记——Spring管理第三方bean;Spring中Bean的作用域;Spring中Bean的生命周期;Spring中bean的后置处理器;Spring中自动装配)

时间2025-06-04 01:49:14分类IT科技浏览3766
导读:2023-01-17...

2023-01-17

一           、Spring管理druid步骤

(1)导入jar包

(2)编写db.properties配置文件

在“模块名.src.main.resources           ”下创建“db.properties                 ”

#里面存放的数据格式为key=value db.driverClassName=com.mysql.cj.jdbc.Driver db.url=jdbc:mysql://localhost:3306/db220106?serverTimezone=UTC db.username=用户名 db.password=密码

(3)编写applicationContext.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载外部属性文件db.properties--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- 装配数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${db.driverClassName}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean> </beans>

二                 、Bean的作用域

1      、语法:

在bean标签中加一个“scope      ”属性即可           。

2           、四个作用域

(1)singleton(默认值)

单例(在容器中只有一个对象)

创建容器对象时           ,spring创建对象

(2)prototype

多例(在容器中有多个对象)

(3)request:请求域

当前请求有效                 ,离开请求域失效

当前请求:URL不变即为当前请求

(4)session:会话域

当前会话      ,当前浏览器不关闭不更换即为当前会话                 。

三                、Spring中Bean的生命周期

1      、生命周期的过程:

①通过构造器或工厂方法创建bean实例

②为bean的属性设置值和对其他bean的引用

③调用bean的初始化方法

④bean可以使用了

⑤当容器关闭时           ,调用bean的销毁方法

四      、bean的后置处理器

1                、作用:在调用初始化方法前后对bean进行额外的处理      。

2           、实现:

(1)实现BeanPostProcessor接口

(2)重写方法

①postProcessBeforeInitialization(Object,String):在bean的初始化之前执行

②postProcessAfterInitialization(Object,String):在bean的初始化之后执行

(3)注意

装配后置处理器会为每个bean均装配                ,不能为局部bean装配后置处理器

五      、Spring中自动装配

1                 、Spring中提供两种装配方式

(1)自动装配

(2)手动装配

2           、Spring自动装配语法及规则

在bean标签中添加属性:Autowire即可

(1)byName

对象中属性与容器中的beanId进行匹配      ,如果属性名与beanId数值一致      ,则自动装配成功

(2)byType

对象中属性类型与容器中class进行匹配                ,如果唯一匹配则自动装配成功

①匹配0个:未装配

②匹配多个:会报错

(3)基于XML方式的自动装配           ,只能装配“非字面量           ”数值

声明:本站所有文章      ,如无特殊说明或标注                 ,均为本站原创发布           。任何个人或组织           ,在未征得本站同意时,禁止复制、盗用                 、采集                 、发布本站内容到任何网站、书籍等各类媒体平台                。如若本站内容侵犯了原著者的合法权益                 ,可联系我们进行处理      。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
电脑管家加速器有用吗(电脑管家LOL升级加速功能是什么?)