在Spring框架中,Spring默认存在两个配置文件:一个是applicationContext.xml配置文件,另一个是spring-servlet.xml配置文件。
spring-servlet.xml配置文件主要有下面几个配置项。
配置项<context:annotation-config/>
在spring的配置文件中,如果我们一个一个地配置bean会非常麻烦,因此我们可以使用注解的方式,但使用注解也会碰到麻烦事,就是每使用一个注解,都要事先在Spring容器中声明注解使用的Bean类。Spring为了简化注解配置,提供了一次性解决方案,就是在Spring的配置文件中配置<context:annotation-config/>。
配置项< context:component-scan >
Spring需要扫描所有加入注解的Java类,对Java类进行实例化和注入工作。该配置项用于配置Spring扫描Java类的路径。
<context:component-scan>节点的base-package属性用来指定Java类的扫描路径,扫描路径以package方式来指定,路径可以使用通配符,通配符说明如下:
(1)? 匹配一个字符
(2)* 匹配零个或多个字符串(只针对名称,不匹配目录分隔符等)
(3)** 匹配路径中的零个或多个目录
例如:
<!-- 完整的包 -->
<context:component-scan base-package="com.test" />
<!-- 使用通配符,只能匹配 com.a.test 无法匹配 com.a.b.test -->
<context:component-scan base-package="com.*.test" />
<!-- 使用通配符,可以匹配 com.a.b.test 和com.a.test -->
<context:component-scan base-package="com.**.test" />
前面我们已经了解了spring-servlet.xml配置文件的主要配置项,现在建立spring-servlet.xml配置文件。
(1)在eshop项目的src目录下,建立config包,用于存储项目相关配置文件,在config包下建立spring_config包,用于存储Spring配置文件;
(2)在spring_config包下,新建spring-servlet.xml配置文件。
spring-servlet.xml配置文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config /> <context:component-scan base-package="com.eshop"></context:component-scan> </beans>
上面代码<beans>节点的xmlns属性引入了Spring框架的XML元素的命名空间,其中context、xsi、aop、tx、p、until、jdbc、cache、mvc这些名称是命名空间的前缀,命名空间的前缀将Spring整体的命名空间拆分为多个子命名空间。例如jdbc前缀定义了Spring框架JDBC部分相关XML元素的命名空间。
<beans>节点的xsi:schemaLocation属性引入了schema文档,schema文档是XML文档的校验文档,Spring在启动时是要检验XML文档的,如果XML文档元素不在命名空间内或不符合schema的规范要求,Spring就会启动失败。
<context:component-scan>节点的base-package属性定义了Spring扫描包类的路径,这里定义的是com.eshop。因此,我们需要在项目的src目录下新建com包,在com包下新建eshop包,Spring将扫描eshop包下的所有子包和类。
在没有使用Spring MVC框架的情况下,applicationContext.xml配置文件是必须的。如果使用了Spring MVC,bean的配置完全可以放在spring-servlet.xml配置文件中。不过考虑到随着项目的开发深入,可能会把一些配置项放置到applicationContext.xml配置文件中,在本课中先建立一个空的spring-servlet.xml配置文件。
在spring_config包下,新建applicationContext.xml配置文件。
applicationContext.xml配置文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> </beans>