文章目录
- 前言
- 一、命名空间
- 1. 引入
- 2. util
- 3. p
- 4. context
- 二、数据源
- 1.pom
- 2. jdbc.properties
- 3. dataSource.xml
- 3.1 util
- 3.2 context
- 4. springContext.xml
- 5. 使用
- 总结
前言
这一章承接上一章内容,主要有关于对命名空间的使用和数据源配置。
一、命名空间
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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsdhttp://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.xsdhttp://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.xsdhttp://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);
}
总结
回到顶部
官方网站
官方文档
视频学习