当前位置: 首页 > article >正文

Spring6梳理5——基于XML管理Bean环境搭建

以上笔记来源:
尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)https://www.bilibili.com/video/BV1kR4y1b7Qc

目录

①搭建模块

②引入配置文件

③创建BeanXML文件

④创建Java类文件(User类文件)

⑤创建测试类

⑥运行截图

⑦总结


①搭建模块

搭建方式如:spring-first

②引入配置文件

引入spring-first模块配置文件:beans.xml、log4j2.xml

引入在父模块中

引入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.haozihua</groupId>
    <artifactId>spring6-ioc-xml</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!--spring context依赖-->
        <!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.2</version>
        </dependency>

        <!--junit5测试-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.3</version>
        </dependency>
        <!--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>
    </dependencies>

</project>

引入log4j2文件,具体内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <loggers>
        <!--
            level指定日志级别,从低到高的优先级:
                TRACE < DEBUG < INFO < WARN < ERROR < FATAL
                trace:追踪,是最低的日志级别,相当于追踪程序的执行
                debug:调试,一般在开发中,都将其设置为最低的日志级别
                info:信息,输出重要的信息,使用较多
                warn:警告,输出警告的信息
                error:错误,输出错误信息
                fatal:严重错误
        -->
        <root level="DEBUG">
            <appender-ref ref="spring6log"/>
            <appender-ref ref="RollingFile"/>
            <appender-ref ref="log"/>
        </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>

        <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,适合临时测试用-->
        <File name="log" fileName="H:/spring6_log/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>

        <!-- 这个会打印出所有的信息,
            每次大小超过size,
            则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,
            作为存档-->
        <RollingFile name="RollingFile" fileName="H:/spring6_log/app.log"
                     filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
            <SizeBasedTriggeringPolicy size="50MB"/>
            <!-- DefaultRolloverStrategy属性如不设置,
            则默认为最多同一文件夹下7个文件,这里设置了20 -->
            <DefaultRolloverStrategy max="20"/>
        </RollingFile>
    </appenders>
</configuration>

③创建BeanXML文件

<?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">
    <!--
配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理
通过bean标签配置IOC容器所管理的bean
属性:
    id:设置bean的唯一标识
    class:设置bean所对应类的全路径
-->

    <bean id="user" class="com.atguigu.spring6.User"></bean>
</beans>

④创建Java类文件(User类文件)

package com.atguigu.spring6;

/**
 * @package: com.atguigu.spring6
 * @className: User
 * @Description:
 * @author: haozihua
 * @date: 2024/7/3 21:26
 */
public class User{

    private String name;
    private Integer age;
    public void run(){
        System.out.println("run...");
    }
}

⑤创建测试类

package com.atguigu.spring6;

import com.sun.source.tree.NewClassTree;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @package: com.atguigu.spring6
 * @className: TestUser
 * @Description:
 * @author: haozihua
 * @date: 2024/7/3 21:30
 */
public class TestUser {

    @Test
    public void Testuser1(){
    ApplicationContext context=
            new ClassPathXmlApplicationContext("bean.xml");
        User user = (User)context.getBean("user");
        user.run();
    }
  
}

⑥运行截图

⑦总结

如果要搭建一个基本的Spring系统,需要搭建一个IDEA子项目,引入Pom.xml文件,log4j2.xml文件、在resources文件夹中创建一个bean.xml配置文件,再创建一个测试类


http://www.kler.cn/news/289928.html

相关文章:

  • 【系统分析师】-面向对象方法
  • 【优质源码】3D多人在线游戏,前端ThreeJS,后端NodeJS
  • 使用 nuxi generate 进行预渲染和部署
  • Unity本地化id查找器,luaparser函数参数查找
  • CAS带来的ABA问题以及解决方案
  • 微小目标检测
  • 排查SQL Server中的内存不足及其他疑难问题
  • 力扣3272.统计好整数的数目
  • Ansible的脚本:playbook
  • 获取4字节数据中比特为 1 的总数的方法
  • 线性代数教材书籍推荐
  • 前端面试:分类算法列一下有多少种应用场景?
  • Automatic Trim Implementation
  • 过滤器Filter(JavaEE有三大组件: servlet filter linstener)
  • Ae关键帧动画基础练习-街道汽车超车
  • Oracle查询优化--分区表建立/普通表转分区表
  • python图像类型分类汇总
  • 代码随想录训练营day35|46. 携带研究材料,416. 分割等和子集
  • 荆州农商行资产质量下行压力不减,参股多家银行均有股权被冻结
  • Find My轮椅|苹果Find My技术与轮椅结合,智能防丢,全球定位
  • Spark框架
  • Nginx: 使用KeepAlived配置实现虚IP在多服务器节点漂移及Nginx高可用原理
  • 12、Flink 流上的确定性之流处理详解
  • Tomcat8版本以上配置自定义400错误页面
  • ElasticSearch-基础操作
  • PDF处理技巧:如何有效处理 PDF 旋转的技巧
  • WebGIS与WebGL是什么,两者之间的关系?
  • Transformer直接预测完整数学表达式,推理速度提高多个数量级
  • ElementUI实现el-table组件的合并行功能
  • vs2017 Qt CMakeList.txt添加生成Qt LinguistTools的ts文件