-
applicationContext.xml:这是Spring框架的核心配置文件,用于定义应用程序的Bean以及它们之间的依赖关系、AOP、事务管理等。它负责整个应用程序的配置和初始化,包含了Spring容器的配置信息。
-
spring-mvc.xml:这是Spring MVC框架的配置文件,用于配置Web MVC相关的组件,如处理器映射器、视图解析器、拦截器等。它会在web.xml文件中通过<servlet>和<servlet-mapping>元素进行引入,并与Spring容器进行整合。
-
jdbc.properties:这是一个数据库连接配置文件,用于配置数据库的连接信息,比如数据库URL、用户名、密码等。在Spring应用程序中,可以通过读取该属性文件来获取数据库连接的配置信息。
-
web.xml:这是Java Web应用程序的部署描述文件,用于配置和声明应用程序的属性和行为。它在Spring环境搭建中的作用是引入Spring MVC框架,并指定相关配置文件的位置。
一般情况下,web.xml文件会加载applicationContext.xml和spring-mvc.xml这两个Spring配置文件。在web.xml中配置<servlet>元素和<servlet-mapping>元素时,可以指定applicationContext.xml和spring-mvc.xml的位置,以便在应用程序启动时完成Spring容器和Spring MVC的初始化和配置。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--全局的初始化参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--SpringMVC的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
<!--
<servlet>标签用于定义一个Servlet组件,其中servlet-name指定了Servlet的名称,这里命名为DispatcherServlet。
<servlet-class>指定了处理请求的Servlet类,这里是org.springframework.web.servlet.DispatcherServlet,它是Spring MVC框架提供的核心Servlet。
<init-param>标签用于指定初始化参数,即contextConfigLocation参数。这个参数指定了Spring MVC配置文件的位置,这里设置为classpath:spring-mvc.xml,表示配置文件位于classpath下。如果配置文件位于其他路径,可以根据实际情况进行修改。
<load-on-startup>标签指定了Servlet的启动顺序,数值越小越先被加载。这里设置为2,表示在容器启动时就会加载并初始化这个Servlet。
通过以上配置,当应用程序启动时,容器会创建DispatcherServlet的实例,并加载spring-mvc.xml配置文件来初始化Spring MVC框架。DispatcherServlet作为整个请求处理的核心,会根据配置文件中定义的处理器映射、视图解析器等组件来处理请求,并将结果返回给客户端。
-->
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--
<servlet-mapping>标签用于指定一个Servlet的URL映射关系。
<servlet-name>指定了要映射的Servlet的名称,这里与之前配置的<servlet-name>保持一致,都是DispatcherServlet。
<url-pattern>指定了URL的匹配规则,这里设置为/,表示所有的请求都会被该Servlet处理。
通过以上配置,当有请求到达时,根据URL的匹配规则,容器会将该请求交给名称为DispatcherServlet的Servlet进行处理。由于设置了<url-pattern>/</url-pattern>,任何请求都会匹配到这个Servlet。然后,DispatcherServlet会根据Spring MVC配置文件中定义的处理器映射关系,将请求分发给相应的Controller类进行处理,并最终返回结果给客户端。这样,就实现了请求到处理的全过程
-->
</web-app>
- 同时,applicationContext.xml中可以引入jdbc.properties文件,通过配置数据源和相关的数据库连接信息。这样,在Spring应用程序中,可以使用Spring的JDBC模块来操作数据库
====================================================================
-
web.xml文件:web.xml文件是Java Web应用程序的部署描述文件,它配置了整个应用程序的属性和行为。在web.xml文件中,通过<servlet>元素和<servlet-mapping>元素配置了Spring MVC框架的DispatcherServlet,并指定了spring-mvc.xml配置文件的位置。这样,当有请求到达时,DispatcherServlet就会被调用,并根据配置的映射关系来处理请求。
DispatcherServlet
的主要职责是接收请求、分发请求、调用处理器、解析视图、渲染视图,并将处理结果返回给客户端。它是 Spring MVC 框架实现请求驱动的核心组件,负责协调各个组件的工作,实现了松耦合的请求处理流程 -
spring-mvc.xml文件:spring-mvc.xml文件是Spring MVC框架的配置文件,用于配置Web MVC相关的组件。在web.xml中引入spring-mvc.xml后,会将其中定义的处理器映射器、视图解析器、拦截器等组件加载到Spring MVC框架中。这些组件在请求过程中被调用,来处理请求、解析视图、拦截请求等操作。
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 通过声明这两个命名空间,我们可以在 Spring MVC 配置文件中使用对应的命名空间前缀来引入和使用相应的配置元素,例如 <mvc:annotation-driven>、<mvc:default-servlet-handler> 等。这样可以简化配置文件的编写,并提供了一种结构化的方式来描述和配置 Spring MVC 的各个组件和特性 --> <!--1、mvc注解驱动 --> <mvc:annotation-driven></mvc:annotation-driven> <!--2、配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 3,静态资源权限开放--> <mvc:default-servlet-handler/> <!-- <mvc:default-servlet-handler/> 是 Spring MVC 中的一个配置标签,用于配置默认的Servlet处理器。 默认情况下,DispatcherServlet 处理所有的请求并将它们分发到相应的处理器进行处理。但有些请求可能不需要被 DispatcherServlet 处理,而是由容器的默认Servlet(通常是 Web 服务器的默认Servlet)来处理,例如静态资源(如图片、CSS、JavaScript文件)等。在这种情况下,我们可以使用 <mvc:default-servlet-handler/> 来配置默认的Servlet处理器。 当配置了 <mvc:default-servlet-handler/> 后,Spring MVC 在处理请求时会检查是否存在与请求路径匹配的静态资源文件,如果存在,则会将请求转发到容器的默认Servlet进行处理。这样可以提高性能,因为默认Servlet通常能更有效地处理静态资源。 --> </beans>
-
applicationContext.xml文件:applicationContext.xml文件是Spring框架的核心配置文件,用于定义应用程序的Bean以及它们之间的依赖关系、AOP、事务管理等。在web.xml中配置了<context-param>元素,指定了applicationContext.xml的位置,在应用程序启动时,Spring容器会加载并初始化这个配置文件,创建相应的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/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 "> <!--1,加载jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!--2,配置数据源对象 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--3,配置jdbcTemplate对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!-- --> </beans>
-
jdbc.properties文件:jdbc.properties文件是一个数据库连接配置文件,用于配置数据库的连接信息。在applicationContext.xml中可以通过配置context:property-placeholder来加载jdbc.properties文件,并将其中定义的属性值注入到Spring Bean中,比如数据源的URL、用户名、密码等。这样,在应用程序中就可以使用Spring的JDBC模块来访问数据库,实现数据库连接和操作。
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=root