一、Spring5.x与日志框架的整合

1.1 为什么要整合?

通过整合,能够在控制台中输出Spring框架在运行过程中的一些重要信息比方bean对象的创立或销毁益处:便于理解Spring框架的运行过程,利于程序调试

1.2 如何整合?

默认状况下spring框架自带的整合日志框架如下    |Spring 1、2、3晚期都是与commons-logging.jar    |Spring5.x 默认整合的日志框架 logback logj2然而咱们在这里咱们突破默认,要应用log4j日志框架    1.引入log4j jar包    2.引入log4j.properties配置文件
  • pom 依赖
<!--log4j 日志框架-->   <dependency>      <groupId>org.slf4j</groupId>   <!--这里为对应的日志门面-->      <artifactId>slf4j-log4j12</artifactId>      <version>1.7.25</version>   </dependency>   <dependency>      <groupId>log4j</groupId>      <artifactId>log4j</artifactId>      <version>1.2.17</version>   </dependency>
  • log4j.properties
#resources文件夹的根目录下### 配置根 log4j.rootLogger = debug.console ### 日志输入到控制台显示配置log4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.Target=System.outlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n
  • 察看打印进去的日志(运行了junit测试后)
23:29:05.482 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7bd4937b23:29:05.849 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 1 bean definitions from class path resource [applicationContext.xml]23:29:05.905 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'u'u = cn.paul.spring.demo.hellospring.entity.User@22356acd

阐明:从下面的配置文件,咱们看到了它加载了这个实现类,而后读取对应的配置文件信息,最初创立了实例

二、注入(Injection)

接下来咱们就进入到了Spring最牛逼的中央了,注入。