一.前言
前面学习提到只讲到了xml文件配置,并未涉及到properties文件,其实xml文件和properties是同一种东西,只是表达形式不一样罢了,但一般情况下,xml文件用来添加改动的配置,properties文件用来配置不经常改变的配置,比如数据库等配置
二.xml文件
2.1 基础配置
xml文件的基础配置如下:
<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"
<!--启用声明事务的命名空间-->
xmlns:tx="http://www.springframework.org/schema/tx"
<!--与上述命名空间定义相配套的schema的定义文件的装载路径-->
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName">
2.2 bean
id就是作为bean的唯一标识符,且id在spring容器中必须唯一
<bean id="hello" class="com.test.Hello"></bean>
配置bean时,没有设置id属性,只设置了name属性,那么name属性就是作为bean的唯一标识符,且name的值在spring容器中必须唯一。
<bean name="hello" class="com.test.Hello"></bean>
如果同时设置了id和name属性,那么id设置的即为唯一标识符,name设置的是别名。
<bean id="hello" name="hello1" class="com.test.Hello"></bean>
其他自行百度吧,😂
当存在多个bean.xml文件时,可以使用import来整合所有的bean.xml为一个xml文件,为applicationContext.xml
import用法:
- 配置文件会先合并,后解析,也就是说,无论是命名空间还是配置的内容,都会合并处理。
- 因为多个 Spring 配置文件最终会合并到一起(形成一个 ApplicationContext),因此这些配置中的 bean 都是可以互相引用的
由于单个bean注入非常浪费时间,因此,可以采用<context:component-scan>方式注入bean
<context:component-scan>有一个use-default-filters属性,该属性默认为true,这就意味着会扫描指定包下的全部的标有@Component的类,并注册成bean.也就是@Component的子注解@Service,@Reposity等
三.properties
3.1 数据库配置
xxx.db.maxWait = 7200000
xxx.db.passwd.new = xxxxxxx
xxx.db.url = jdbc:mysql://localhost:3306/eportal?useUnicode=
xxx.db.user = test
比如
spring.datasource.url=jdbc:mysql://localhost:3306/blue?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=xx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3.2 http服务器端口
server.port=9001
3.3 mybatis配置
mybatis.config-location=classpath:/mybatis/mybatis-config.xml
其他配置参照如此