interface21 - web - ContextLoaderListener(Spring Web Application Context加载流程)

本文详细解析了Spring WebApplicationContext的加载过程,从ContextLoaderListener的contextInitialized方法开始,介绍了如何读取配置文件、创建bean实例并维护在map中,以及在Servlet容器销毁时的资源清理流程。

前言 

最近打算花点时间好好看看spring的源码,然而现在Spring的源码经过迭代的版本太多了,比较庞大,看起来比较累,所以准备从最初的版本(interface21)开始入手,仅用于学习,理解其设计思想,后续慢慢研究其每次版本变更的内容。。。

先从interface21的一个典型web工程例子看起,宠物诊所 - petclinic,因为该工程基本涵盖了Spring的APO、IOC、JDBC、Web MVC、事务、国际化、主题切换、参数校验等主要功能。。。

继上一篇,了解完Log4jConfigListener(加载Log4j日志)的流程后,看看ContextLoaderListener(加载Spring Web Application Context)流程是如何的~~~~~~~

对应的web.xml配置

    <listener>
        <listener-class>com.interface21.web.context.ContextLoaderListener</listener-class>
    </listener>

执行时序图(看不清的话可以点击查看原图)

 

时序图中的各个步骤简要分析

执行的入口在ContextLoaderListener类的contextInitialized方法,由于ContextLoaderListener类实现了ServletContextListener接口,所以在Servlet容器(tomcat)启动时,会自动调用contextInitialized方法。

步骤描述:

  1. 进入ContextLoaderListener类的contextInitialized方法,该类只有一句代码,执行ContextLoader.initContext(event.getServletContext())方法;
    public void contextInitialized(ServletContextEvent event) {
            ContextLoader.initContext(event.getServletContext());
        }
  2. 进入ContextLoader类的initContext方法,首先,从servletContext中获取contextClass参数,如果配置了该参数,则创建该实例对象,否则创建默认的XmlWebApplicationContext实例对象,接下来调用XmlWebApplicationContext的setServletContext方法;
    public static WebApplicationContext initContext(ServletContext servletContext) throws ApplicationContextException {
            servletContext.log("Loading root WebApplicationContext");
            String contextClass = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
    
            // Now we must load the WebApplicationContext.
            // It configures itself: all we need to do is construct the class with a no-arg
            // constructor, and invoke setServletContext.
            try {
                Class clazz = (contextClass != null ? Class.forName(contextClass) : DEFAULT_CONTEXT_CLASS);
                logger.info("Loading root WebApplicationContext: using context class '" + clazz.getName() + "'");
    
                if (!WebApplicationContext.class.isAssignableFrom(clazz)) {
                    throw new ApplicationContextException("Context class is no WebApplicationContext: " + contextClass);
                }
    
                WebApplicationContext webApplicationContext = (WebApplicationContext) clazz.newInstance();
                webApplicationContext.setServletContext(servletContext);
                return webApplicationContext;
    
            } catch (ApplicationContextException ex) {
                handleException("Failed to initialize application context", ex);
    
            } catch (BeansException ex) {
                handleException("Failed to initialize beans in application context", ex);
    
            } catch (ClassNotFoundException ex) {
                handleException("Failed to load config class '" + contextClass + "'", ex);
    
            } catch (InstantiationException ex) {
                handleException("Failed to instantiate config class '" + contextClass + "': does it have a public no arg constructor?", ex);
    
            } catch (IllegalAccessException ex) {
                handleException("Illegal access while finding or instantiating config class '" + contextClass + "': does it have a public no arg constructor?", ex);
    
            } catch (Throwable ex) {
                handleException("Unexpected error loading context configuration", ex);
            }
    
            return null;
        }
  3. 进入XmlWebApplicationContext类的setServletContext方法,首先,调用initConfigLocation方法从servletContext中获取contextConfigLocation参数(Spring Application配置文件),如果没配置该参数,则默认获取/WEB-INF/applicationContext.xml该文件;
        public void setServletContext(ServletContext servletContext) throws ApplicationContextException {
            this.servletContext = servletContext;
            this.configLocation = initConfigLocation();
            logger.info("Using config location '" + this.configLocation + "'");
            refresh();
    
            if (this.namespace == null) {
                // We're the root context
                WebApplicationContextUtils.publishConfigObjects(this);
                // Expose as a ServletContext object
                WebApplicationContextUtils.publishWebApplicationContext(this);
            }
        }
  4. 迎来了非常关键的一步操作,调用AbstractApplicationContext类的refresh()方法,该方法具体如下,每个阶段的英文注释已经比较清晰了,下面步骤也会做个详细描述:
        public final void refresh() throws ApplicationContextException {
            if (this.contextOptions != null && !this.contextOptions.isReloadable())
                throw new ApplicationContextException("Forbidden to reload config");
    
            this.startupTime = System.currentTimeMillis();
    
            refreshBeanFactory();
    
            if (getBeanDefinitionCount() == 0)
                logger.warn("No beans defined in ApplicationContext [" + getDisplayName() + "]");
            else
                logger.info(getBeanDefinitionCount() + " beans defined in ApplicationContext [" + getDisplayName() + "]");
    
            // invoke configurers that can override values in the bean definitions
            invokeContextConfigurers();
    
            // load options bean for this context
            loadOptions();
    
            // initialize message source for this context
            initMessageSource();
    
            // initialize other special beans in specific context subclasses
            onRefresh();
    
            // check for listener beans and register them
            refreshListeners();
    
            // instantiate singletons this late to allow them to access the message source
            preInstantiateSingletons();
    
            // last step: publish respective event
            publishEvent(new ContextRefreshedEvent(this));
        }
  5. 首先,调用AbstractXmlApplicationContext类的refreshBeanFactory方法,该方法如下,具体完成的操作内容下面步骤会详细描述:
      protected void refreshBeanFactory() throws ApplicationContextException {
            String identifier = "application context with display name [" + getDisplayName() + "]";
            InputStream is = null;
            try {
                // Supports remote as well as local URLs
                is = getInputStreamForBeanFactory();
                this.xmlBeanFactory = new XmlBeanFactory(getParent());
                this.xmlBeanFactory.setEntityResolver(new ResourceBaseEntityResolver(this));
                this.xmlBeanFactory.loadBeanDefinitions(is);
                if (logger.isInfoEnabled()) {
                    logger.info("BeanFactory for application context: " + this.xmlBeanFactory);
                }
            } catch (IOException ex) {
                throw new ApplicationContextException("IOException parsing XML document for " + identifier, ex);
            } catch (NoSuchBeanDefinitionException ex) {
                throw new ApplicationContextException("Cannot load configuration: missing bean definition [" + ex.getBeanName() + "]", ex);
            } catch (BeansException ex) {
                throw new ApplicationContextException("Cannot load configuration: problem instantiating or initializing beans", ex);
            } finally {
                try {
                    if (is != null)
                        is.close();
                } catch (IOException ex) {
                    throw new ApplicationContextException("IOException closing stream for XML document for " + identifier, ex);
                }
            }
        }
  6. 调用XmlWebApplicationContext类的getInputStreamForBeanFactory方法,读取阶段3获取到的配置文件为输入流InputStream
        protected InputStream getInputStreamForBeanFactory() throws IOException {
            InputStream in = getResourceAsStream(this.configLocation);
            if (in == null) {
                throw new FileNotFoundException("Config location not found: " + this.configLocation);
            }
            return in;
        }
  7. 返回配置文件输入流InputStream
  8. 回到AbstractXmlApplicationContext的refreshBeanFactory方法,new出一个XmlBeanFactory对象
  9. 设置xmlBeanFactory.setEntityResolver,这里的EntityResolver主要用于寻找DTD声明
  10. 调用xmlBeanFactory的loadBeanDefinitions方法加载bean定义声明
  11. 进入xmlBeanFactory类的loadBeanDefinitions方法,解析读取的配置文件流InputStream为org.w3c.dom.Document对象,然后调用loadBeanDefinitions方法依次解析各个bean元素节点信息
        public void loadBeanDefinitions(InputStream is) throws BeansException {
            if (is == null)
                throw new BeanDefinitionStoreException("InputStream cannot be null: expected an XML file", null);
    
            try {
                logger.info("Loading XmlBeanFactory from InputStream [" + is + "]");
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                logger.debug("Using JAXP implementation [" + factory + "]");
                factory.setValidating(true);
                DocumentBuilder db = factory.newDocumentBuilder();
                db.setErrorHandler(new BeansErrorHandler());
                db.setEntityResolver(this.entityResolver != null ? this.entityResolver : new BeansDtdResolver());
                Document doc = db.parse(is);
                loadBeanDefinitions(doc);
            } catch (ParserConfigurationException ex) {
                throw new BeanDefinitionStoreException("ParserConfiguration exception parsing XML", ex);
            } catch (SAXException ex) {
                throw new BeanDefinitionStoreException("XML document is invalid", ex);
            } catch (IOException ex) {
                throw new BeanDefinitionStoreException("IOException parsing XML document", ex);
            } finally {
                try {
                    if (is != null)
                        is.close();
                } catch (IOException ex) {
                    throw new FatalBeanException("IOException closing stream for XML document", ex);
                }
            }
        }
  12. 寻找Document中声明为bean的Element节点,依次解析
        public void loadBeanDefinitions(Document doc) throws BeansException {
            Element root = doc.getDocumentElement();
            logger.debug("Loading bean definitions");
            NodeList nl = root.getElementsByTagName(BEAN_ELEMENT);
            logger.debug("Found " + nl.getLength() + " <" + BEAN_ELEMENT + "> elements defining beans");
            for (int i = 0; i < nl.getLength(); i++) {
                Node n = nl.item(i);
                loadBeanDefinition((Element) n);
            }
        }
  13. 解析Element节点内容,获取class声明信息、PropertyValues等信息,封装成AbstractBeanDefinition对象,添加到beanDefinitionMap中
        private void loadBeanDefinition(Element el) throws BeansException {
            // The DTD guarantees an id attribute is present
            String id = el.getAttribute(ID_ATTRIBUTE);
            logger.debug("Parsing bean definition with id '" + id + "'");
    
            // Create BeanDefinition now: we'll build up PropertyValues later
            AbstractBeanDefinition beanDefinition;
    
            PropertyValues pvs = getPropertyValueSubElements(el);
            beanDefinition = parseBeanDefinition(el, id, pvs);
            registerBeanDefinition(id, beanDefinition);
    
            String name = el.getAttribute(NAME_ATTRIBUTE);
            if (name != null && !"".equals(name)) {
                // Automatically create this alias. Used for
                // names that aren't legal in id attributes
                registerAlias(id, name);
            }
        }
  14. 判断是否需要注册alias,放到aliasMap中,实际上就是维护了bean的name和id关系
  15. 返回到AbstractXmlApplicationContext类refreshBeanFactory方法中
  16. 返回到AbstractApplicationContext类refresh方法中
  17. 执行AbstractApplicationContext的invokeContextConfigurers方法,实际上内部是执行所有实现了BeanFactoryPostProcessor接口的bean的postProcessBeanFactory方法
        private void invokeContextConfigurers() {
            String[] beanNames = getBeanDefinitionNames(BeanFactoryPostProcessor.class);
            for (int i = 0; i < beanNames.length; i++) {
                String beanName = beanNames[i];
                BeanFactoryPostProcessor configurer = (BeanFactoryPostProcessor) getBean(beanName);
                configurer.postProcessBeanFactory(getBeanFactory());
            }
        }
  18. 执行AbstractApplicationContext的loadOptions方法,获取contextOptions bean,首先,查看配置文件是否已经配置contextOptions bean,没有则自己创建一个new ContextOptions()对象,主要用于当应用运行时,是否可以重新加载该配置,如果配置成false的话,会在调用refresh方法时,抛出一个ApplicationContextException("Forbidden to reload config")异常;
        private void loadOptions() {
            try {
                this.contextOptions = (ContextOptions) getBean(OPTIONS_BEAN_NAME);
            } catch (NoSuchBeanDefinitionException ex) {
                logger.info("No options bean (\"" + OPTIONS_BEAN_NAME + "\") found: using default");
                this.contextOptions = new ContextOptions();
            }
        }
  19. 执行AbstractApplicationContext的initMessageSource方法,获取messageSource bean,首先,查看配置文件是否已经配置messageSource bean,没有则自己创建一个StaticMessageSource对象,注意如果Parent context不为null的话,需要设置Parent MessageSource
        private void initMessageSource() {
            try {
                this.messageSource = (MessageSource) getBean(MESSAGE_SOURCE_BEAN_NAME);
                // set parent message source if applicable,
                // and if the message source is defined in this context, not in a parent
                if (this.parent != null && (this.messageSource instanceof NestingMessageSource) &&
                        Arrays.asList(getBeanDefinitionNames()).contains(MESSAGE_SOURCE_BEAN_NAME)) {
                    ((NestingMessageSource) this.messageSource).setParent(this.parent);
                }
            } catch (NoSuchBeanDefinitionException ex) {
                logger.info("No MessageSource found for [" + getDisplayName() + "]: using empty StaticMessageSource");
                // use empty message source to be able to accept getMessage calls
                this.messageSource = new StaticMessageSource();
            }
        }
  20. 执行AbstractXmlUiApplicationContext的onRefresh方法,获取themeSource bean, 主题相关(如应用可配置暗色主题或亮色主题功能),同样,这里也首先查看配置文件是否已经配置themeSource bean,没有则自己创建一个ResourceBundleThemeSource对象,注意这里还需要根据判断条件设置Parent ThemeSource
        protected void onRefresh() {
            this.themeSource = UiApplicationContextUtils.initThemeSource(this);
        }
        public static ThemeSource initThemeSource(ApplicationContext applicationContext) {
            ThemeSource themeSource;
            try {
                themeSource = (ThemeSource) applicationContext.getBean(THEME_SOURCE_BEAN_NAME);
                // set parent theme source if applicable,
                // and if the theme source is defined in this context, not in a parent
                if (applicationContext.getParent() instanceof ThemeSource && themeSource instanceof NestingThemeSource &&
                        Arrays.asList(applicationContext.getBeanDefinitionNames()).contains(THEME_SOURCE_BEAN_NAME)) {
                    ((NestingThemeSource) themeSource).setParent((ThemeSource) applicationContext.getParent());
                }
            } catch (NoSuchBeanDefinitionException ex) {
                logger.info("No ThemeSource found for [" + applicationContext.getDisplayName() + "]: using ResourceBundleThemeSource");
                themeSource = new ResourceBundleThemeSource();
            }
            return themeSource;
        }
  21. 执行AbstractApplicationContext的refreshListeners方法,寻找所有ApplicationListener bean,将其放到ApplicationEventMulticaster对象的Set集合中
        private void refreshListeners() {
            logger.info("Refreshing listeners");
            List listeners = BeanFactoryUtils.beansOfType(ApplicationListener.class, this);
            logger.debug("Found " + listeners.size() + " listeners in bean factory");
            for (int i = 0; i < listeners.size(); i++) {
                ApplicationListener listener = (ApplicationListener) listeners.get(i);
                addListener(listener);
                logger.info("Bean listener added: [" + listener + "]");
            }
        }
  22. 执行AbstractApplicationContext的preInstantiateSingletons方法,创建单例的bean实例,创建bean对象是在调用getBean方法时创建的,具体创建逻辑在getSharedInstance方法里;另外,对实现了ApplicationContextAware接口的bean,会调用对应的接口setApplicationContext方法,这里涉及的细节比较多,后续有时间可以具体详细分析;
        private void preInstantiateSingletons() {
            logger.info("Configuring singleton beans in context");
            String[] beanNames = getBeanDefinitionNames();
            logger.debug("Found " + beanNames.length + " listeners in bean factory: names=[" +
                    StringUtils.arrayToDelimitedString(beanNames, ",") + "]");
            for (int i = 0; i < beanNames.length; i++) {
                String beanName = beanNames[i];
                if (isSingleton(beanName)) {
                    getBean(beanName);
                }
            }
        }
        public Object getBean(String name) throws BeansException {
            Object bean = getBeanFactory().getBean(name);
            configureManagedObject(name, bean);
            return bean;
        }
        private final synchronized Object getSharedInstance(String pname, Map newlyCreatedBeans) throws BeansException {
            // Get rid of the dereference prefix if there is one
            String name = transformedBeanName(pname);
    
            Object beanInstance = this.singletonCache.get(name);
            if (beanInstance == null) {
                logger.info("Creating shared instance of singleton bean '" + name + "'");
                beanInstance = createBean(name, newlyCreatedBeans);
                this.singletonCache.put(name, beanInstance);
            } else {
                if (logger.isDebugEnabled())
                    logger.debug("Returning cached instance of singleton bean '" + name + "'");
            }
    
            // Don't let calling code try to dereference the
            // bean factory if the bean isn't a factory
            if (isFactoryDereference(pname) && !(beanInstance instanceof FactoryBean)) {
                throw new BeanIsNotAFactoryException(name, beanInstance);
            }
    
            // Now we have the beanInstance, which may be a normal bean
            // or a FactoryBean. If it's a FactoryBean, we use it to
            // create a bean instance, unless the caller actually wants
            // a reference to the factory.
            if (beanInstance instanceof FactoryBean) {
                if (!isFactoryDereference(pname)) {
                    // Configure and return new bean instance from factory
                    FactoryBean factory = (FactoryBean) beanInstance;
                    logger.debug("Bean with name '" + name + "' is a factory bean");
                    beanInstance = factory.getObject();
    
                    // Set pass-through properties
                    if (factory.getPropertyValues() != null) {
                        logger.debug("Applying pass-through properties to bean with name '" + name + "'");
                        new BeanWrapperImpl(beanInstance).setPropertyValues(factory.getPropertyValues());
                    }
                    // Initialization is really up to factory
                    //invokeInitializerIfNecessary(beanInstance);
                } else {
                    // The user wants the factory itself
                    logger.debug("Calling code asked for BeanFactory instance for name '" + name + "'");
                }
            }    // if we're dealing with a factory bean
    
            return beanInstance;
        }
        private void configureManagedObject(String name, Object bean) {
            if (bean instanceof ApplicationContextAware &&
                    (!isSingleton(name) || !this.managedSingletons.contains(bean))) {
                logger.debug("Setting application context on ApplicationContextAware object [" + bean + "]");
                ApplicationContextAware aca = (ApplicationContextAware) bean;
                aca.setApplicationContext(this);
                this.managedSingletons.add(bean);
            }
        }
  23. 执行AbstractApplicationContext的publishEvent方法,发布ContextRefreshedEvent事件,如果parent不为空,一起发布,内部的逻辑是执行对应eventListeners的onApplicationEvent方法
        public final void publishEvent(ApplicationEvent event) {
            if (logger.isDebugEnabled()) {
                logger.debug("Publishing event in context [" + getDisplayName() + "]: " + event.toString());
            }
            this.eventMulticaster.onApplicationEvent(event);
            if (this.parent != null) {
                parent.publishEvent(event);
            }
        }
  24. 回到XmlWebApplicationContext类
  25. 执行WebApplicationContextUtils.publishConfigObjects方法,寻找所有config bean,将其设置到ServletContext的属性中
        public static void publishConfigObjects(WebApplicationContext wac) throws ApplicationContextException {
            logger.info("Configuring config objects");
            String[] beanNames = wac.getBeanDefinitionNames();
            for (int i = 0; i < beanNames.length; i++) {
                String name = beanNames[i];
                if (name.startsWith(CONFIG_OBJECT_PREFIX)) {
                    // Strip prefix
                    String strippedName = name.substring(CONFIG_OBJECT_PREFIX.length());
                    try {
                        Object configObject = wac.getBean(name);
                        wac.getServletContext().setAttribute(strippedName, configObject);
                        logger.info("Config object with name [" + name + "] and class [" + configObject.getClass().getName() +
                                "] initialized and added to ServletConfig");
                    } catch (BeansException ex) {
                        throw new ApplicationContextException("Couldn't load config object with name '" + name + "': " + ex, ex);
                    }
                }
            }
        }
  26. 执行WebApplicationContextUtils.publishWebApplicationContext,将WebApplicationContext设置到ServletContext属性中
        public static void publishWebApplicationContext(WebApplicationContext wac) {
            // Set WebApplicationContext as an attribute in the ServletContext so
            // other components in this web application can access it
            ServletContext sc = wac.getServletContext();
            if (sc == null)
                throw new IllegalArgumentException("ServletContext can't be null in WebApplicationContext " + wac);
    
            sc.setAttribute(WebApplicationContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE_NAME, wac);
            logger.info(
                    "Loader initialized on server name "
                            + wac.getServletContext().getServerInfo()
                            + "; WebApplicationContext object is available in ServletContext with name '"
                            + WebApplicationContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE_NAME
                            + "'");
        }
  27. 返回webApplicationContext到ContextLoader类
  28. ContextLoaderListener.contextInitialized方法执行结束

 

就这样,Spring Web Application Context加载完成了,是不是感觉也挺简单的,主要就是读取xml配置文件中bean的配置信息,创建bean实例放到一个map中维护,当然,中间还穿插了各种逻辑;

另外补充下,当Servlet容器销毁时,会调用ContextLoaderListener的contextDestroyed方法,最终是调用ContextLoader.closeContext(event.getServletContext(),执行一些资源销毁等操作,销毁工厂创建的bean对象,发布ContextClosedEvent事件等;

  public void close() {
        logger.info("Closing application context [" + getDisplayName() + "]");

        // destroy all cached singletons in this context,
        // invoking DisposableBean.destroy and/or "destroy-method"
        getBeanFactory().destroySingletons();

        // publish respective event
        publishEvent(new ContextClosedEvent(this));
    }

interface21代码参考

 https://github.com/peterchenhdu/interface21

转载于:https://www.cnblogs.com/chenpi/p/9527304.html

一、实验步骤 1、创建maven web项目或者模块,导入相关软件的依赖包。 2、进行tomcat的相关配置。 3、在数据库mybatis中创建book表,并插入一条数据,sql语句如下: USE mybatis; CREATE TABLE `tb_book` ( `id` int(11) , `name` varchar(32) , `press` varchar(32) , `author` varchar(32) ); INSERT INTO `tb_book` VALUES (1, 'Java EE企业级应用开发教程', '人民邮电出版社', '黑马程序员'); 4、在src/main/java里创建com包,在里面创建itheima子包,在itheima包中分别创建controller,service,dao和domain包。 5、在domain包下创建名称为Book的实体类。代码如下: public class Book { private Integer id; //图书id private String name; //图书名称 private String press; //出版社 private String author; //作者 //getter和setter方法自行补充 @Override public String toString() { return super.toString(); } } 6、在dao包下创建接口BookMapper,里面创建一个根据id查询图书信息的方法,代码如下: public interface BookMapper { public Book findBookById(Integer id); } 7、在src/main/resources文件夹中,创建多级文件夹com/itheima/dao,在dao文件夹下,创建映射文件BookMapper.xml,记录与findBookById对应的sql语句。 <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itheima.dao.BookMapper"> <!--根据id查询图书信息 --> <select id="findBookById" parameterType="int" resultType="com.itheima.domain.Book"> select * from tb_book where id = #{id} </select> </mapper> 8、在service包里创建BookService接口,在BookService接口中定义findBookById()方法,通过id获取对应的Book信息。代码如下: public interface BookService { public Book findBookById(Integer id); } 9、在service包中创建子包impl,在ipml包中创建BookServiceImpl类,BookServiceImpl类实现BookService接口的findBookById()方法,并在类中注入一个BookMapper对象。findBookById()方法通过注入的BookMapper对象调用findBookById()方法,根据id查询对应的图书信息。代码如下: public class BookServiceImpl implements BookService { @Autowired private BookMapper bookMapper; public Book findBookById(Integer id) { return bookMapper.findBookById(id); } } 10、在controller包中创建BookController类,在BookController类中注入一个BookService对象,并且定义一个名称为findBookById()的方法。findBookById()方法获取传递过来的图书id,并将图书id作为参数传递给BookService对象调用的findBookById()方法。代码如下: @Controller public class BookController { @Autowired private BookService bookService; @RequestMapping("/book") public ModelAndView findBookById(Integer id) { Book book = bookService.findBookById(id); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("book"); modelAndView.addObject("book", book); return modelAndView; } } 11、在resources文件夹中创建jdbc.properties,作为数据库连接配置文件,代码如下: jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true\ &characterEncoding=utf-8&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=root 12、在resources文件夹中创建Spring的配置文件application-dao.xml文件,用于扫描包,引入数据库连接配置文件,配置Spring和MyBatis的整合信息(包括SqlSessionFactoryBean类和MapperScannerConfigurer的配置)。代码如下: <!--开启注解扫描, 扫描包--> <context:component-scan base-package="com.itheima.service"/> <!--引入属性文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--创建SqlSessionFactory对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--数据源--> <property name="dataSource" ref="dataSource"/> </bean> <!--扫描Dao包,创建动态代理对象,会自动存储到spring IOC容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定要扫描的dao的包--> <property name="basePackage" value="com.itheima.dao"/> </bean> 13、在src/test/java目录下,创建名称为BookTest的测试类,用于对Spring和MyBatis的整合进行测试。用上@RunWith和@ContextConfiguration注解,代码如下,对此方法进行测试,成功打印出书籍信息即可证明Spring和MyBatis整合成功。 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations ="classpath:application-dao.xml") public class BookTest { @Autowired private BookService bookService; @Test public void findBookById() { Book book = bookService.findBookById(1); System.out.println("图书id:" + book.getId()); System.out.println("图书名称:" + book.getName()); System.out.println("作者:" + book.getAuthor()); System.out.println("出版社:" + book.getPress()); } } 14、下面对SpringSpringMVC进行整合。在resources目录下创建Spring MVC的配置文件spring-mvc.xml,负责扫描controller层以及配置视图解析器。代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置要扫描的包 --> <context:component-scan base-package="com.itheima.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="suffix" value=".jsp"/> </bean> <!-- 配置默认Servlet,处理静态资源 --> <mvc:default-servlet-handler/> <!-- 配置注解驱动 --> <mvc:annotation-driven/> </beans> 15、在项目的web.xml中配置Spring的监听器ContextLoaderListener加载Spring容器及Spring的配置文件,具体配置如下所示: <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 16、接着在web.xml中配置SpringMVC的前端控制器,并在初始化前端控制器时加载Spring MVC的的配置文件spring-mvc.xml。代码如下: <!--Spring MVC 前端控制器--> <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>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 17、在webapp目录下创建book.jsp,代码如下: <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head><title>图书信息查询</title></head> <body> <table border="1"> <tr> <th>图书id</th> <th>图书名称</th> <th>出版社</th> <th>作者</th> </tr> <tr> <td>${book.id}</td> <td>${book.name}</td> <td>${book.press}</td> <td>${book.author}</td> </tr> </table> </body> </html> 18、运行项目,在浏览器地址栏中输入http://localhost/book?id=1来进行图书查询。如果能顺利访问,表示SSM整合成功(如果你端口不是80的话,访问url得加上端口)。 用idea按照以上流程,详细的列出每一步的步骤,教会小白。
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值