【第5章】spring命名空间和数据源的引入

文章目录

  • 前言
  • 一、命名空间
    • 1. 引入
    • 2. util
    • 3. p
    • 4. context
  • 二、数据源
    • 1.pom
    • 2. jdbc.properties
    • 3. dataSource.xml
      • 3.1 util
      • 3.2 context
    • 4. springContext.xml
    • 5. 使用
  • 总结


前言

这一章承接上一章内容,主要有关于对命名空间的使用和数据源配置。


一、命名空间

1. 引入

<?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"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

2. util

util主要是关于对集合类参数简化,spring bean通过类似于引入外部bean的形式使用

<!--util:list、map、set-->
<util:list id="bookList"><value>2024-03-29</value><value>2024-03-30</value><value>2024-03-31</value>
</util:list>
<util:map id="bookMap"><entry><key><value>Sean Brdiy</value></key><value>This is good idea!</value></entry><entry><key><value>version</value></key><ref bean="version"></ref></entry>
</util:map>
<util:set id="bookSet"><value>1</value><value>2</value><value>3</value><value>4</value><value>5</value>
</util:set>
<bean id="book8" class="org.example.di.Book"><property name="upgradeDate"><ref bean="bookList"></ref></property><property name="readerComments"><ref bean="bookMap"></ref></property><property name="pageSize"><ref bean="bookSet"></ref></property>
</bean>
<util:properties id="mysql" location="classpath:jdbc.properties"></util:properties>

3. p

p命名空间主要是简化property标签,防止属性过多,让结构看上去复杂

<!--p命名空间-->
<bean id="book9" class="org.example.di.Book" p:bName="java编程思想" p:author="Bruce Eckel"p:master-ref="version" p:upgradeDate-ref="bookList" p:readerComments-ref="bookMap" p:pageSize-ref="bookSet">
</bean>

4. context

context主要用于扫描指定包路径下的spring bean和开启注解及加载配置文件

<context:component-scan base-package="org.example"></context:component-scan>
<context:annotation-config></context:annotation-config>
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

二、数据源

1.pom

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.22</version>
</dependency>

2. jdbc.properties

jdbc_driverClassName=com.mysql.cj.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
jdbc_username=root
jdbc_password=123456a?

3. dataSource.xml

dataSource.xml只配置数据库相关bean

3.1 util

<util:properties>加载文件会以单例bean的形式存在,可以添加额外配置

<?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/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"><!--引入外部文件--><util:properties id="mysql" location="classpath:jdbc.properties"><prop key="timeOut">1000</prop><prop key="dealy">1000</prop></util:properties><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="#{mysql.jdbc_driverClassName}"></property><property name="url" value="#{mysql.jdbc_url}"></property><property name="username" value="#{mysql.jdbc_username}"></property><property name="password" value="#{mysql.jdbc_password}"></property></bean>
</beans>

3.2 context

<context:property-placeholder>将配置添加到上下文环境变量中

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--引入外部文件--><context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc_driverClassName}"></property><property name="url" value="${jdbc_url}"></property><property name="username" value="${jdbc_username}"></property><property name="password" value="${jdbc_password}"></property></bean>
</beans>

4. springContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--引入外部文件--><import resource="classpath:dataSource.xml"></import>
</beans>

5. 使用

ApplicationContext context=new ClassPathXmlApplicationContext("springContext.xml");
try {//数据源DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class);System.out.println(dataSource.getConnection());
} catch (SQLException e) {throw new RuntimeException(e);
}

总结

回到顶部
官方网站
官方文档
视频学习

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

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

相关文章

中国移动传关停8元保号套餐?或是5G成本带来的压力所致

日前有网友发现希望使用中国移动的保号套餐&#xff0c;却发现已无法办理&#xff0c;媒体对此多有报道&#xff0c;这意味着中国移动的套餐业务发生了重大变动&#xff0c;如此做或许在于5G成本上涨带来的压力促使它不得不提高套餐的门槛。 中国移动已建成最多的5G基站&#x…

java的正则表达式校验,包含了中国几乎所有运营商手机号码的校验格式

时间2024年4月14日22:25:00 代码 String PHONE_REGEX "^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\\d{8}$";解释 这个Java代码段定义了一个常量 PHONE_REGEX&#xff0c;它包含了一个正则表达式&#xff0c;用于匹配中国大陆的手机号码。下面是对这…

服务器主机关机重启告警

提取时间段内系统操作命名&#xff0c;出现系统重启命令&#xff0c;若要出现及时联系确认 重启命令&#xff1a; reboot / init 6 / shutdown -r now&#xff08;现在重启命令&#xff09; 关机命令&#xff1a; init 0 / shutdown -h now&#xff08;关机&#…

uniCloud联表查询方式举例

联查表&#xff1a; 1. 在shema中配置外键&#xff1a; 2.在前端使用&#xff1a; <unicloud-db v-slot:default"{data, loading, error, options}" :options"formData" collection"opendb-news-articles,uni-id-users" //这里这么写 fi…

浅述python中NumPy包

NumPy&#xff08;Numerical Python&#xff09;是Python的一种开源的数值计算扩展&#xff0c;提供了多维数组对象ndarray&#xff0c;是一个快速、灵活的大数据容器&#xff0c;可以用来存储和处理大型矩阵&#xff0c;支持大量的维度数组与矩阵运算&#xff0c;并针对数组运…

json-c库交叉编译时报错

json-c库交叉编译时报错 关注点错误出现的场景错误描述解决办法为啥?原因分析解决步骤总结 怎么看出来的?哦!get新知识 关注点 看文章前,我先告诉你重点,给我死死盯准文章里的EM: 3 &#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&…

[leetcode] 705. 设计哈希集合

不使用任何内建的哈希表库设计一个哈希集合&#xff08;HashSet&#xff09;。 实现 MyHashSet 类&#xff1a; void add(key) 向哈希集合中插入值 key 。 bool contains(key) 返回哈希集合中是否存在这个值 key 。 void remove(key) 将给定值 key 从哈希集合中删除。如果哈希…

WebKit的使用技巧

WebKit是一个开源的浏览器引擎&#xff0c;广泛应用于多种浏览器和应用程序中&#xff0c;用于渲染网页和处理用户界面。在使用WebKit时&#xff0c;有一些技巧和注意事项可以帮助开发者更高效地利用这个强大的工具。 1. 理解WebKit的架构&#xff1a;WebKit由多个模块组成&am…

数据可视化高级技术Echarts(堆叠柱状图)

目录 一.如何实现 二.代码展示 1.stack名称相同&#xff08;直接堆叠&#xff09; 2. stack名称不相同&#xff08;相同的堆叠&#xff0c;不同的新生成一列&#xff09; 一.如何实现 数据堆叠&#xff0c;同个类目轴上系列配置相同的 stack 值可以堆叠放置。即在series中…

【示例】MySQL-4类SQL语言-DDL-DML-DQL-DCL

前言 本文主要讲述MySQL中4中SQL语言的使用及各自特点。 SQL语言总共分四类&#xff1a;DDL、DML、DQL、DCL。 SQL-DDL | Data Definition Language 数据定义语言&#xff1a;用来定义/更改数据库对象&#xff08;数据库、表、字段&#xff09; 用途 | 操作数据库 # 查询所…

LeetCode 128.最长连续数列

目录 题目描述 方法一 思路&#xff1a; 代码&#xff1a; 方法二 思路&#xff1a; 代码: 题目描述 给定一个未排序的整数数组 nums &#xff0c;找出数字连续的最长序列&#xff08;不要求序列元素在原数组中连续&#xff09;的长度。 请你设计并实现时间复杂度为 O(n)…

playwright: context添加了“has_touch“:True的值导致页面点击事件失效

问题 录制脚本的时候页面上的可以点击&#xff0c;然后debug的时候不可点击&#xff0c;经过排查发现是在创建context时&#xff0c;browser.new_context()中添加了"has_touch":True&#xff0c;导致pc版本的web部分点击事件失效 解决方法 "has_touch"的…

SMS垃圾短信识别项目

注意&#xff1a;本文引用自专业人工智能社区Venus AI 更多AI知识请参考原站 &#xff08;[www.aideeplearning.cn]&#xff09; 项目背景 随着数字通信的快速发展&#xff0c;垃圾短信成为了一个普遍而烦人的问题。这些不请自来的消息不仅打扰了我们的日常生活&#xff0c;…

从零全面认识 多线程

目录 1.基本概念 2.创建线程方式 2.1直接建立线程 2.2实现Runnable接口 3.3实现Callable接口 3.4 了解Future接口 Future模式主要角色及其作用 3.5实例化FutureTask类 3.实现线程安全 3.1定义 3.2不安全原因 3.3解决方案 3.4volatile与synchronized区别 3.5Lock与…

【Linux】命名管道的创建方法基于命名管道的两个进程通信的实现

一、匿名管道和命名管道的区别 匿名管道由pipe函数创建并打开。 命名管道由mkfifo函数创建&#xff0c;打开用open FIFO&#xff08;命名管道&#xff09;与pipe&#xff08;匿名管道&#xff09;之间唯一的区别在它们创建与打开的方式不同&#xff0c;一但这些工作完成之后&am…

创建线程池的例子

public class ExecutorTest {public static void main(String[] args) {//创建线程池的5种方式&#xff1a; // Executors.newFixedThreadPool();//创建固定线程数的线程池 // Executors.newSingleThreadExecutor();//创建单线程的线程池 // Executors.ne…

Geeker-Admin:基于Vue3.4、TypeScript、Vite5、Pinia和Element-Plus的开源后台管理框架

Geeker-Admin&#xff1a;基于Vue3.4、TypeScript、Vite5、Pinia和Element-Plus的开源后台管理框架 一、引言 随着技术的不断发展&#xff0c;前端开发领域也在不断演变。为了满足现代应用程序的需求&#xff0c;开发人员需要使用最新、最强大的工具和技术。Geeker-Admin正是…

如何安全地设置MySQL数据库的IP白名单

设置MySQL数据库的IP白名单是一种关键的安全措施&#xff0c;可以确保只有来自特定IP地址的请求被允许访问数据库服务器。这里是如何安全地配置这些设置的分步指南。 步骤1: 登录到MySQL服务器 首先&#xff0c;使用管理员权限登录到你的MySQL服务器。如果你使用的是命令行&a…

activiti初次学习

源代码地址&#xff1a;https://gitee.com/ZSXYX/activiti.git​ 1、安装插件 首先安装下图所示activiti,不确定是哪个插件有用的&#xff0c;有时间可排除下 在resources下创建一个文件夹&#xff1a;processes,右键&#xff0c;新建 生成&#xff1a; 选中act.bpmn20.xm…

【C++】模拟list

list的模拟真的很震撼&#xff0c;第一次学习时给我幼小的心灵留下了极大地冲击 接下来我们一起看看list模拟究竟是怎样一回事 目录 节点的封装&#xff1a;list类的实现&#xff1a;私有成员变量&#xff1a;构造函数&#xff1a;push_back && pop_back: 迭代器类的实…