温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-05-30 16:26:51 来源:亿速云 阅读:121 作者:iii 栏目:开发技术

本篇内容主要讲解“Spring IOC容器基于XML外部属性文件的Bean管理怎么配置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring IOC容器基于XML外部属性文件的Bean管理怎么配置”吧!

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容器基于XML外部属性文件的Bean管理怎么配置”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI