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

2023-01-21 0 2,951

目录

Spring IOC Bean管理XML

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

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

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

一、常规配置方法

1. 引入依赖

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

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

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

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

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的资料请关注其它相关文章!

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 Spring IOC容器基于XML外部属性文件的Bean管理 http://www.20zxx.cn/463589/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务