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

本文详细介绍了Spring框架中命名空间的使用,包括引入、集合类参数简化、P命名空间的简化以及Context的组件扫描和配置文件加载。同时,还涵盖了数据源的配置,如MySQL连接、DruidDataSource的配置和使用方法。
摘要由CSDN通过智能技术生成


前言

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


一、命名空间

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.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://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.xsd
        http://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.xsd
        http://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);
}

总结

回到顶部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值