spring-data-jpa学习(一)环境配置1.1

来源:互联网 发布:中文安卓编程开创e4a 编辑:程序博客网 时间:2024/05/21 22:23

首先展示一下搭建完成之后的工程目录结构


运行程序后数据库会自动生成相应的表结构


工程中关于config的calss一共有4个,分别是Application,ApplicationConfig,WebConfig和PersistenceConfig,接下来将逐一学习。

1.Application

package test.config;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration.Dynamic;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.context.request.RequestContextListener;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;@Configuration@ComponentScan(basePackages=Application.CONFIG_PACKAGE)public class Application extends AbstractAnnotationConfigDispatcherServletInitializer{public static final String CONFIG_PACKAGE="test";//配置root上下文 @Overrideprotected Class<?>[] getRootConfigClasses() {System.err.println("getRootConfigClasses");return new Class[]{Application.class};}//配置dispatcher servlet,如果在root config指定了该转发规则就可以忽略 @Overrideprotected Class<?>[] getServletConfigClasses() {System.err.println("getServletConfigClasses");return new Class[]{WebConfig.class};}//指定开始被servlet处理的url,配置从/开始  @Overrideprotected String[] getServletMappings() {System.err.println("getServletMappings");return new String[]{"/*"};}//将相关的组件注册到服务器@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {System.err.println("onStartup");servletContext.addListener(RequestContextListener.class);super.onStartup(servletContext);}@Overrideprotected void customizeRegistration(Dynamic registration) {System.err.println("customizeRegistration");registration.setInitParameter("dispatchOptionsRequest", "true");}}

此类继承了AbstractAnnotationConfigDispatcherServletInitializer,它的作用是帮助开发者快速创建一个DispatcherServlet。


以这种方式加载dispatcherservlet和传统的以xml来配置dispatcher是怎么对应起来的呢?

要知道这个,必须先知道以web.xml来配置时是如何加载的,即springMVC的启动过程。


首先,看看一个springMVC中的web.xml是怎样写的。

<?xml version="1.0"encoding="UTF-8"?>  <web-app id="WebApp_ID" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/controllers.xml,/WEB-INF/service.xml</param-value>  </context-param>  <servlet>  <servlet-name>dispatch</servlet-name<span style="font-family: Arial, Helvetica, sans-serif;">>  </span><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/applicationContext.xml</param-value>  </init-param>  </servlet>  <servlet-mapping>  <servlet-name>dispatch</servlet-name>  <servlet-pattern>*.*</servlet-pattern>  </servlet-mapping>  </web-app>  

加载过程如下:

1.当web程序启动时,web容器会为程序提供一个ServletContext,这是一个全局容器,一个web程序只会有一个ServletContext。

2.接下来容器会读取web.xml中的<listener>节点,这会触发监听事件,contextLoaderListener会初始化一个WebApplicationContext,这个就是IOC容器!在这个IoC容器初始化完毕后,spring以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE为属性Key,将其存储到ServletContext中,便于获取。接下来<context-param>标签中的xml里配置的bean将会被加入

3.<servlet>和<servlet-mapping>标签是配置dispatcherServlet的,<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 指明了Servlet为DispatcherServlet。<init-param>标签指定了与此DispatcherServlet有关的初始化信息的位置,如果没有<init-param>标签,则默认为/WEB-INF文件夹下的[servlet-name]-setvlet.xml文件。

下面这两幅图很好的说明他们之间的关系




知道了以web.xml方式是如何加载的,那么回到我们的加载方式,他们是怎么对应起来的呢?

先看看Application类中的方法在启动时是以怎样的顺序运行的吧。



@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {System.err.println("onStartup");servletContext.addListener(RequestContextListener.class);super.onStartup(servletContext);}

这个方法很显然是对应读取<listener>标签来加载WebApplicationContext容器,即生成IOC容器


@Overrideprotected Class<?>[] getRootConfigClasses() {System.err.println("getRootConfigClasses");return new Class[]{Application.class};}

这个方法对应于读取<context-param>中指定的beans,即ContextLoaderListener加载beans,其中的参数使用Application.class,我的理解是,其实它只提供了一个ComponentScan的功能,所以这个类只需要@Configuration和@ComponentScan就行了


@Overrideprotected Class<?>[] getServletConfigClasses() {System.err.println("getServletConfigClasses");return new Class[]{WebConfig.class};}

这个方法对应于读取<servlet>标签,加载DispatcerServlet


@Overrideprotected String[] getServletMappings() {System.err.println("getServletMappings");return new String[]{"/*"};}

这个方法对应于读取<servlet-mapping>标签,配置DispatcherServlet分配的url


关于Application.class初始化dispatcher的部分如上,接下来将学习WebConfig.class的内容和PersistenceConfig.class的内容




0 0
原创粉丝点击