iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring IOC容器基于XML外部属性文件的Bean管理
  • 480
分享到

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

2024-04-02 19:04:59 480人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录spring ioc Bean管理XML一、常规配置方法1. 引入依赖2. xml 文件配置数据库连接池二、引入外部属性文件来配置数据库连接池1. 创建外部文件2. 引入外部文件

Spring ioC Bean管理XML

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

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

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

一、常规配置方法

1. 引入依赖

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

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

2. 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">
    <!--直接配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.Mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
        <property name="username" value="root"></property>
        <property name="passWord" value="123456"></property>
    </bean>
</beans>

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

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。

<?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 http://www.springframework.org/schema/context/spring-context.xsd">
    <!--引入外部属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
</beans>

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

通过${}

<?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 http://www.springframework.org/schema/context/spring-context.xsd">
    <!--引入外部属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClass}"></property>
        <property name="url" value="${prop.url}"></property>
        <property name="username" value="${prop.username}"></property>
        <property name="password" value="${prop.password}"></property>
    </bean>
</beans>

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

--结束END--

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

本文链接: https://www.lsjlt.com/news/150165.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作