Spring IOC容器基于XML外部属性文件的Bean管理

网友投稿 302 2022-07-27


目录Spring IOC Bean管理XML一、常规配置方法1. 引入依赖2. xml 文件配置数据库连接池二、引入外部属性文件来配置数据库连接池1. 创建外部文件2. 引入外部文件到xml配置文件中3. 引用外部文件里的属性

Spring IOC Bean管理XML

有时候,为了灵活方便,我们会把某些固定的数据存放到文件里,然后去读取里面的内容来使用。

比如数据库的连接信息,这些内容就可以放到 properties 文件中,然后使用 xml 配置文件去读取里面的内容,完成需要的注入。

这里使用德鲁伊连接池举例,德鲁伊连接池是阿里巴巴开源的数据库连接池项目。

一、常规配置方法

1. 引入依赖

下载一个德鲁伊的 jar 包,放到 lib 下面。

然后通过 File-Project Structure 添加这个 lib 下的jar包,应用。

2. xml 文件配置数据库连接池

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

二、引入外部属性文件来配置数据库连接池

1. 创建外部文件

创建 properties 格式文件,写入数据库信息。

prop.driverClass=com.mysql.jdbc.Driver

prop.url=jdbc:mysql://localhost:3306/userDb

prop.username=root

prop.password=123456

2. 引入外部文件到xml配置文件中

把刚才创建的 properties 文件引入到 spring 的配置文件中来,通过使用名称空间 context。

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

3. 引用外部文件里的属性

通过${}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

以上就是Spring IOODaWhGfpNfC容器Bean管理XML外部属性文件的详细内容,更多关于Spring IOC Bean管理XML的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于mybatis一对一查询一对多查询遇到的问题(mybatis一对一)
下一篇:java实现简单学生成绩档案管理系统(java学生成绩管理系统登录界面)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~