zoukankan      html  css  js  c++  java
  • Spring框架基础(下)

    • log4J
      • 导入log4J.jar
      • 创建log4J.properties
        # Create a file called log4j.properties as shown below and place it in your classpath:
        # 创建一个名为log4j.properties的文件,如下所示,并将其放在类路径中:
        # Global logging configuration
        # 在开发环境下日志级别要设置成DEBUG,生产环境设置成info或error
        log4j.rootLogger=DEBUG, stdout
        # MyBatis logging configuration...
        log4j.logger.org.mybatis.example.BlogMapper=TRACE
        # Console output...
        log4j.appender.stdout=org.apache.log4j.ConsoleAppender
        log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
        log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    • Spring整合web项目原理
      • 加载spring核心配置文件
        • 加载spring配置文件时,new对象可以实现功能,但效率低
          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("service/service.xml");
        • 解决办法
          • 实现思想
            • 把加载配置文件和创建对象过程,在服务器启动时候完成
          • 实现原理(ServletContext对象、监听器)
            • 在ServletContext对象创建时候,使用监听器可以具体到ServletContext对象在什么时候创建
            • 使用监听器(ServletContextListener)监听到ServletContext对象创建时候
              • 加载spring配置文件,把配置文件配置对象创建
              • 把创建出来的对象放到ServletContext域对象里面(setAttribute)
              • 获取对象时候,到ServletContext域得到(getAttribute)
                /**
                * 实现监听器
                */
                public class BaseListener implements ServletContextListener {
                    @Override
                    public void contextInitialized(ServletContextEvent servletContextEvent) {
                        ServletContext servletContext = servletContextEvent.getServletContext();
                        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("service/service.xml");
                
                        servletContext.setAttribute("applicationContext",applicationContext);
                    }
                
                    @Override
                    public void contextDestroyed(ServletContextEvent servletContextEvent) {
                
                    }
                }
                
                
                <!-- 在web.xml配置listener -->
                
                  <!-- 配置listener -->
                    <listener>
                        <listener-class>cn.muriel.auto.web.listener.BaseListener</listener-class>
                    </listener>
                
                
                <!-- 测试代码 -->
                <%@ page import="cn.muriel.auto.service.UserService" %>
                <%@ page import="cn.muriel.auto.dao.UserDao" %>
                <%@ page import="org.springframework.context.ApplicationContext" %>
                <%@ page contentType="text/html;charset=UTF-8" language="java" %>
                <html>
                <head>
                    <title>$Title$</title>
                </head>
                <body>
                <%
                    ApplicationContext applicationContext = (ApplicationContext) application.getAttribute("applicationContext");
                    UserService userService = (UserService) applicationContext.getBean("userService");
                    UserDao userDao = (UserDao) applicationContext.getBean("userDao");
                    userService.setUserDao(userDao);
                    userService.addUser();
                %>
                </body>
                </html>
  • 相关阅读:
    教你一招用 IDE 编程提升效率的骚操作!
    动态拼接sql语句工具类
    mysql数据建模规范
    分割字符串为数字列表
    linux 配置mysql odbc
    nodejs npm常用命令
    (四)jquery easyui panel window使用
    (三)jquery easyui常用form控件的使用
    (二)jquery easyUI提示框的使用
    (一)jQuery easyUI 环境的搭建
  • 原文地址:https://www.cnblogs.com/fatRabbit-/p/10556275.html
Copyright © 2011-2022 走看看