【Spring6】| Spring's entry program, integrated Log4j2 log framework

Table of contents 

One: Spring's entry program

1. Spring download

2. Spring jar file

3. The first Spring program

4. Detailed analysis of the first Spring program

5. Spring6 enables Log4j2 logging framework


One: Spring's entry program

1. Spring download

Official website address: https://spring.io/

Official website address (Chinese): Spring Chinese website official website

After opening the Spring official website, you can see the Spring Framework and other frameworks derived from the Spring Framework:

What we are about to learn is the Spring Framework; how to download it?

The first step: enter github

Step 2: Find the location in the picture below and click on the hyperlink

Step 3: Find the location in the picture below and click on the hyperlink of the warehouse address

Step 4: Follow the steps in the figure below to find springframwork

Step 5: Continue to find the spring in the picture below in the springframework directory, and you will see many different versions after clicking it

Step 6: Select the corresponding version 

Step 7: Click on the url in the above picture

Click spring-5.3.9-dist.zip to download the spring framework; unzip the downloaded zip package and see several folders:

docs: API help documentation for the spring framework

libs: The jar file of the spring framework ( using the spring framework is to use these jar packages )

schema: Constraint files related to the XML configuration file of the spring framework

2. Spring jar file

Open the libs directory, you will see many jar packages, take the core jar package as an example:

spring-core-5.3.9.jar: bytecode (this is the jar package that supports the running of the program)

spring-core-5.3.9-javadoc.jar: comments in the code

spring-core-5.3.9-sources.jar: source code

 All other jar packages, of course, these jar packages also correspond to this jar file, so they are not listed here:

spring-aop-5.3.9.jar

This jar file contains the classes needed to use Spring's AOP features in the application

spring-aspects-5.3.9.jar

Provide support for AspectJ so that aspect-oriented functions can be easily integrated into the IDE

spring-beans-5.3.9.jar

This jar file is used by all applications, and it contains all classes related to accessing configuration files, creating and managing beans, and performing Inversion of Control / Dependency Injection (IoC/DI) operations. If the application only needs basic IoC/DI support, just import the spring-core.jar and spring-beans.jar files.

spring-context-5.3.9.jar

This jar file provides a number of extensions to the Spring core. You can find all the classes required when using the Spring ApplicationContext feature, all the classes required by JDNI, instrumentation components, and related classes related to Validation.

spring-context-indexer-5.3.9.jar

While classpath scanning is very fast, there are a large number of classes inside Spring, and adding this dependency can improve the startup performance of large applications by creating a static list of candidate objects at compile time.

spring-context-support-5.3.9.jar

Some extension modules used to provide Spring context, such as implementing mail service, view resolution, caching, scheduled task scheduling, etc.

spring-core-5.3.9.jar

The basic core tool class of the Spring framework. The classes in this package are used by other Spring components, which are the basic core of other components. Of course, you can also use these tool classes in your own application system.

spring-expression-5.3.9.jar

Spring expression language.

spring-instrument-5.3.9.jar

Spring3.0's proxy interface to the server.

spring-jcl-5.3.9.jar

Spring's logging module. JCL, the full name is "Jakarta Commons Logging", it can also be called "Apache Commons Logging".

spring-jdbc-5.3.9.jar

Spring's support for JDBC.

spring-jms-5.3.9.jar

This jar package provides support classes for JMS 1.0.2/1.1. JMS is Java Message Service. Belongs to one of the JavaEE specifications.

spring-messaging-5.3.9.jar

Provides support for integrating messaging APIs and messaging protocols

spring-orm-5.3.9.jar

Spring integrates ORM framework support, such as integrating hibernate, mybatis, etc.

spring-oxm-5.3.9.jar

It provides a unified layer of abstraction and encapsulation for mainstream O/X Mapping components, and OXM is Object Xml Mapping. Mutual conversion between objects and XML.

spring-r2dbc-5.3.9.jar

Acronym for Reactive Relational Database Connectivity. This jar file is Spring's support for r2dbc.

spring-test-5.3.9.jar

A simple wrapper for testing frameworks such as Junit.

spring-tx-5.3.9.jar

Consistent declarative and programmatic transaction management support for JDBC, Hibernate, JDO, JPA, Beans, etc.

spring-web-5.3.9.jar

Spring integrates the support of MVC framework, such as integrating Struts and so on.

spring-webflux-5.3.9.jar

WebFlux is a new module added by Spring5, which is used for web development. Its functions are similar to SpringMVC. Webflux uses a current framework that compares process-responsive programming.

spring-webmvc-5.3.9.jar

The class library of the SpringMVC framework

spring-websocket-5.3.9.jar

Used when Spring integrates the WebSocket framework

注意:如果只是想用Spring的IoC功能,仅需要引入:spring-context即可,将这个jar包添加到classpath当中。如果采用maven只需要引入context的依赖即可!

现在我们学习的是Spring6,但是并没有Spring6只发布了里程碑版,并没有jar包(当然使用maven做项目管理也不需要下载jar包);需要引入依赖,并且引入依赖的同时也要引入仓库地址(因为还没有正式发布,需要内部的仓库地址)

注:其实spring-context会依赖core和bean的jar包等,但是我们使用maven就只需引入context的jar包即可,其它依赖的会自动导入!

<!--Spring6的正式版发布之前,这个仓库地址是需要的-->
<repositories>
  <repository>
    <id>repository.spring.milestone</id>
    <name>Spring Milestone Repository</name>
    <url>https://repo.spring.io/milestone</url>
  </repository>
</repositories>

<dependencies>
  <!--spring context依赖:使用的是6.0.0-M2里程碑版-->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.0.0-M2</version>
  </dependency>
</dependencies>

3. 第一个Spring程序

前期准备:

①打开IDEA创建Empty Project:spring6

②设置JDK版本17,编译器版本17

③设置IDEA的Maven:关联自己的maven

④在空的工程spring6中创建第一个模块:spring6-001-first

第一步:添加spring context的依赖和单元测试junit依赖,pom.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bjpowernode</groupId>
    <artifactId>spring6-001-first</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!--配置多个仓库-->
    <repositories>
        <!--spring6里程碑的仓库-->
        <repository>
            <id>repository.spring.milestone</id>
            <name>Spring Milestone Repository</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

    <dependencies>
        <!--spring context依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.0-M2</version>
        </dependency>
        <!--单元测试依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
</project>

注意:打包方式jar!

当加入spring context的依赖之后,会关联引入其他依赖:

①spring aop:面向切面编程

②spring beans:IoC核心

③spring core:spring的核心工具包

④spring jcl:spring的日志包

⑤spring expression:spring表达式

第二步:定义bean:User,默认会调用无参构造方法

package com.bjpowernode.spring6.bean;
// 这是一个Bean,封装了用户的信息。Spring可以帮助我们创建User对象吗?
public class User {
   
}

第三步:编写spring的配置文件:spring.xml(名字随意),该文件放在类的根路径下(便于移植)。下图是使用IDEA工具自带的spring配置文件的模板进行创建。

此时就需要在配置文件中进行bean的配置,bean的id和class属性:

id属性:代表对象的唯一标识。

class属性:用来指定要创建的java对象的类名,这个类名必须是全限定类名(带包名)。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
        
        <!--配置bean,这样spring才能帮助我们管理这个对象-->
        <bean id="userBean" class="com.bjpowernode.spring6.bean.User" />
</beans>

第四步:编写测试程序

先创建ClassPathXmlApplicationContext对象,参数是spring配置文件的路径;返回的是ApplicationContext 翻译为:应用上下文,其实就是Spring容器。
解释:ApplicationContext 就是一个接口,接口下有很多实现类,其中有一个实现类叫做:ClassPathXmlApplicationContext,是专门从类路径当中加载spring配置文件的一个Spring上下文对象。
②创建好ClassPathXmlApplicationContext对象,就可以调用getBean方法,参数是spring.xml配置文件当中的id,这样就可以根据bean的id从Spring容器中获取这个对象!      

package com.bjpowernode.spring6.test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpringTest {
    @Test
    public void testFirstSpringTest(){
        // 第一步:获取Spring容器对象
        // 这行代码只要执行:就相当于启动了Spring容器,解析spring.xml文件,并且实例化所有的bean对象,放到spring容器当中
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        // 第二步:根据bean的id从Spring容器中获取这个对象
        Object userBean = applicationContext.getBean("userBean");
        System.out.println(userBean);
    }
}

第五步:运行测试程序,验证Spring容器确实可以帮我们创建对象 

4. 第一个Spring程序详细剖析

(1)bean标签的id属性可以重复吗?

Vip.java

package com.powernode.spring6.bean;

public class Vip {
}

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <bean id="userBean" class="com.powernode.spring6.bean.User"/>
    <bean id="userBean" class="com.powernode.spring6.bean.Vip"/>
</beans>

运行测试程序:

通过测试得出:在spring的配置文件中id是不能重名!

(2)底层是怎么创建对象的?

 User.java:在User类中添加上无参数构造方法

package com.powernode.spring6.bean;

public class User {
    public User() {
        System.out.println("User的无参数构造方法执行");
    }
}

运行测试程序:

通过测试得知:创建对象时确实调用了无参数构造方法,并且是通过反射机制调用无参数构造方法。

如果提供一个有参数构造方法,不提供无参数构造方法会怎样呢?

package com.powernode.spring6.bean;

public class User {
    public User(String name){
        System.out.println("User的有参数构造方法执行");
    }
}

运行测试程序:

通过测试得知:spring是通过调用类的无参数构造方法来创建对象的,所以要想让spring给你创建对象,必须保证无参数构造方法是存在的。

Spring是如何创建对象的呢?原理是什么?

先通过dom4j解析spring.xml文件,从中获取class的全限定类名;然后通过反射机制调用无参数构造方法创建对象!

Class clazz = Class.forName("com.powernode.spring6.bean.User");
Object obj = clazz.newInstance();

(3)把创建好的对象存储到一个什么样的数据结构当中了呢?

存到一个Map集合,Map的key就是id,value就是创建的对象!

(4)spring配置文件的名字必须叫做spring.xml吗?

①这个spring配置文件名字是我们负责提供的,显然spring配置文件的名字是随意的。

②并且这个配置文件不一定就放在跟目录下,也可以先创建xml目录,然后把配置文件放到这个目录下也可以!

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml/beans.xml");

(5)像这样的beans.xml文件可以有多个吗?

再创建一个spring配置文件,起名:beans.xml,放到xml目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

      <bean id="vipBean" class="com.bjpowernode.spring6.bean.Vip" />
</beans>

编写测试

package com.bjpowernode.spring6.test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpringTest {
    @Test
    public void testFirstSpringTest(){
        // 第一步:获取Spring容器对象
        // 这行代码只要执行:就相当于启动了Spring容器,解析spring.xml文件,并且实例化所有的bean对象,放到spring容器当中
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml","xml/beans.xml");
        // 第二步:根据bean的id从Spring容器中获取这个对象
        Object userBean = applicationContext.getBean("userBean");
        System.out.println(userBean);
        Object vipBean = applicationContext.getBean("vipBean");
        System.out.println(vipBean);

    }
}

运行测试程序:

通过测试得知,spring的配置文件可以有多个,在ClassPathXmlApplicationContext构造方法的参数上传递文件路径即可!这是为什么呢?通过源码可以看到参数是一个可变长度参数:

(6)在配置文件中配置的类必须是自定义的吗,可以使用JDK中的类吗,例如:java.util.Date?

spring.xml中增加配置

<bean id="dateBean" class="java.util.Date"/>

编写测试

package com.bjpowernode.spring6.test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpringTest {
    @Test
    public void testFirstSpringTest(){
        // 第一步:获取Spring容器对象
        // 这行代码只要执行:就相当于启动了Spring容器,解析spring.xml文件,并且实例化所有的bean对象,放到spring容器当中
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml","xml/beans.xml");
        // 第二步:根据bean的id从Spring容器中获取这个对象
        Object userBean = applicationContext.getBean("userBean");
        System.out.println(userBean);
        Object vipBean = applicationContext.getBean("vipBean");
        System.out.println(vipBean);

        // 使用JDK中的类
        Object bean = applicationContext.getBean("dateBean");
        System.out.println(bean);

    }
}

运行测试程序:当然是可以的

(7)getBean()方法调用时,如果指定的id不存在会怎样?

运行测试程序:

通过测试得知:当id不存在的时候,会出现异常,而不是返回null!

(8)getBean()方法返回的类型是Object,如果访问子类的特有属性和方法时,还需要向下转型,有其它办法可以解决这个问题吗?  

对获取的时间进行格式化

spring.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
        <!--配置bean,这样spring才能帮助我们管理这个对象-->
        <bean id="dateBean" class="java.util.Date" />
</beans>

编写测试

第一种方法:直接进行强制类型转换。

第二种方法:多传一个参数,这个参数指定要转换的类型,例如:要转换成Date类型就是Date.class。

package com.bjpowernode.spring6.test;

import com.bjpowernode.spring6.bean.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FirstSpringTest {
    @Test
    public void testFirstSpringTest(){
        
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        // -------------第一种方法
        // 返回的是一个Object类型
        Object dateBean = applicationContext.getBean("dateBean");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 这个里面要传Date,需要强转
        Date date = (Date) dateBean;
        String nowDate = sdf.format(date);
        System.out.println(nowDate);
        // -------------第二种方法
        Date dateBean1 = applicationContext.getBean("dateBean", Date.class);
        String nowDate1 = sdf.format(dateBean1);
        System.out.println(nowDate1);
    }
}

执行结果:

(9)ClassPathXmlApplicationContext是从类路径中加载配置文件,如果没有在类路径当中,又应该如何加载配置文件呢?

例如:在d盘下的spring.xml,怎样去加载?

调用的方法都不同了,调用的是FileSystemXmlApplicationContext方法,并把文件的绝对路径传过去!

User.java

package com.bjpowernode.spring6.bean;

public class User {
    public void doSome(){
        System.out.println("doSome方法执行了");
    }
}

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
        <!--配置bean,这样spring才能帮助我们管理这个对象-->
        <bean id="userBean" class="com.bjpowernode.spring6.bean.User" />
        <bean id="dateBean" class="java.util.Date" />
</beans>

编写测试

package com.bjpowernode.spring6.test;

import com.bjpowernode.spring6.bean.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FirstSpringTest {
    @Test
    public void testFirstSpringTest(){
        FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\\spring.xml");
        User user = applicationContext.getBean("userBean", User.class);
        user.doSome();
    }
}

运行结果:也能正常的加载创建对象 

(10)ApplicationContext的超级父接口BeanFactory

ApplicationContext接口的一个超级父接口是:BeanFactory,翻译为Bean工厂,就是能够生产Bean对象的一个工厂对象。
BeanFactory是IoC容器的顶级接口!
③实际上Spring的IoC容器底层实际上使用了:工厂模式
Spring底层的IoC是怎么实现的?XML解析 + 工厂模式 + 反射机制

(11)何时创建对象?

User.java

package com.bjpowernode.spring6.bean;

public class User {
    public User() {
        System.out.println("User的无参数构造方法执行。");
    }
}

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
        <!--配置bean,这样spring才能帮助我们管理这个对象-->
        <bean id="userBean" class="com.bjpowernode.spring6.bean.User" />
</beans>

编写测试

package com.bjpowernode.spring6.test;

import com.bjpowernode.spring6.bean.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FirstSpringTest {
    @Test
    public void testFirstSpringTest(){
        // 此时就把对象已经创建出来了
        new ClassPathXmlApplicationContext("spring.xml");
    }
}

执行结果:

①实际上在执行new ClassPathXmlApplicationContext("spring.xml");时对象就创建出来了。

②调用getBean方法,只是从集合当中把数据取出来!

5. Spring6启用Log4j2日志框架

从Spring5之后,Spring框架支持集成的日志框架是Log4j2;如何启用日志框架?

第一步:在pom.xml中引入Log4j2的依赖

<!--log4j2的依赖-->
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.19.0</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j2-impl</artifactId>
  <version>2.19.0</version>
</dependency>

第二步: 在类的根路径下提供log4j2.xml配置文件

注:文件名固定为log4j2.xml,文件必须放到类根路径下!

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <loggers>
        <!--
            level指定日志级别,从低到高的优先级:
                ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
        -->
        <root level="DEBUG">
            <appender-ref ref="spring6log"/>
        </root>
    </loggers>

    <appenders>
        <!--输出日志信息到控制台-->
        <console name="spring6log" target="SYSTEM_OUT">
            <!--控制日志输出的格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-3level %logger{1024} - %msg%n"/>
        </console>
    </appenders>

</configuration>

第三步:使用日志框架 

① Step 1: Call the getLogger method of LoggerFactory to return the logger object , the parameter is a specific class, for example: to get the logger object of the FirstSpringTest class, the parameter is FirstSpringTest.class, that is to say, as long as it is in the FirstSpringTest class If the code is executed to record the log, the relevant log information will be output.

②The second step: record the log, and output the log according to different calling levels.

package com.bjpowernode.spring6.test;

import com.bjpowernode.spring6.bean.User;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FirstSpringTest {
    @Test
    public void testFirstSpringTest(){
        new ClassPathXmlApplicationContext("spring.xml");

        // 第一步:创建日志记录器对象
        Logger logger = LoggerFactory.getLogger(FirstSpringTest.class);
        // 第二步:记录日志,根据不同的级别来输出日志
        logger.info("我是一条消息");
        logger.debug("我是一条调试信息");
        logger.error("我是一条错误信息");
    }
}

Results of the:

The level we set before is DEBUG , which is lower than the info and error levels. The higher the level, the less information is output, and the lower the level, the more information is output ; so all three logs will be output!

Guess you like

Origin blog.csdn.net/m0_61933976/article/details/128642738