以上笔记来源:
尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)https://www.bilibili.com/video/BV1kR4y1b7Qc
目录
①搭建模块
②引入配置文件
③创建bean-dip.xml文件
④创建课程类文件
⑤创建学生类文件
⑥创建老师类文件
⑦创建测试类文件
⑧运行截图
⑨总结
①搭建模块
搭建方式如: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>
③创建bean-dip.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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
1.创建三个对象
2.注入普通类型属性
3.使用util:类型 定义
4.在学生bean引入util:类型定义bean,完成list,map类型的注入-->
<!--p命名空间注入-->
<bean id="studentp" class="com.atguigu.spring6.iocxml.diprename.Student"
p:sid="100" p:sname="mary" p:lessonList-ref="lessonList" p:teacherMap-ref="teacherMap">
</bean>
<bean id="student" class="com.atguigu.spring6.iocxml.diprename.Student">
<property name="sid" value="10000"></property>
<property name="sname" value="lucy"></property>
<property name="lessonList" ref="lessonList"></property>
<property name="teacherMap" ref="teacherMap"></property>
</bean>
<util:list id="lessonList">
<ref bean="lessonone"></ref>
<ref bean="lessontwo"></ref>
</util:list>
<util:map id="teacherMap">
<entry>
<key>
<value>10010</value>
</key>
<ref bean="teacherone"></ref>
</entry>
<entry>
<key>
<value>10011</value>
</key>
<ref bean="teachertwo"></ref>
</entry>
</util:map>
<bean id="lessonone" class="com.atguigu.spring6.iocxml.diprename.Lesson">
<property name="lessonName" value="java开发"></property>
</bean>
<bean id="lessontwo" class="com.atguigu.spring6.iocxml.diprename.Lesson">
<property name="lessonName" value="python开发"></property>
</bean>
<bean id="teacherone" class="com.atguigu.spring6.iocxml.diprename.Teacher">
<property name="teacherId" value="101"></property>
<property name="teacherName" value="贾宝玉"></property>
</bean>
<bean id="teachertwo" class="com.atguigu.spring6.iocxml.diprename.Teacher">
<property name="teacherId" value="102"></property>
<property name="teacherName" value="李时珍"></property>
</bean>
</beans>
④创建课程类文件
package com.atguigu.spring6.iocxml.diprename;
/**
* @package: com.atguigu.spring6.iocxml.dimap
* @className: Lesson
* @Description:
* @author: haozihua
* @date: 2024/9/26 14:39
*/
public class Lesson {
private String lessonName;
public String getLessonName() {
return lessonName;
}
public void setLessonName(String lessonName) {
this.lessonName = lessonName;
}
@Override
public String toString() {
return "Lesson{" +
"lessonName='" + lessonName + '\'' +
'}';
}
}
⑤创建学生类文件
package com.atguigu.spring6.iocxml.diprename;
import java.util.List;
import java.util.Map;
/**
* @package: com.atguigu.spring6.iocxml.dimap
* @className: Student
* @Description:
* @author: haozihua
* @date: 2024/8/20 15:20
*/
public class Student {
private List<Lesson> lessonList;
//一个学生对应很多个老师
private Map<String, Teacher> teacherMap;
private String sid;
private String sname;
public List<Lesson> getLessonList() {
return lessonList;
}
public void setLessonList(List<Lesson> lessonList) {
this.lessonList = lessonList;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Map<String, Teacher> getTeacherMap() {
return teacherMap;
}
public void setTeacherMap(Map<String, Teacher> teacherMap) {
this.teacherMap = teacherMap;
}
public void run() {
System.out.println("学生编号: " + sid + " " + "学生名称:" + sname);
System.out.println(teacherMap);
for (String teacher : teacherMap.keySet()) {
System.out.println(teacher);
}
for (Teacher teacher : teacherMap.values()) {
System.out.println(teacher);
}
}
}
⑥创建老师类文件
package com.atguigu.spring6.iocxml.diprename;
/**
* @package: com.atguigu.spring6.iocxml.dimap
* @className: Teacher
* @Description:
* @author: haozihua
* @date: 2024/8/20 15:21
*/
public class Teacher {
private Integer teacherId;
private String teacherName;
public Integer getTeacherId() {
return teacherId;
}
public void setTeacherId(Integer teacherId) {
this.teacherId = teacherId;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
@Override
public String toString() {
return "Teacher{" +
"teacherId=" + teacherId +
", teacherName='" + teacherName + '\'' +
'}';
}
public Teacher(Integer teacherId, String teacherName) {
this.teacherId = teacherId;
this.teacherName = teacherName;
}
public Teacher() {
}
}
⑦创建测试类文件
package com.atguigu.spring6.iocxml.diprename;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @package: com.atguigu.spring6.iocxml.dimap
* @className: TestMap
* @Description:
* @author: haozihua
* @date: 2024/8/20 15:50
*/
public class TestP {
@Test
public void Testp(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean-dip.xml");
Student student = context.getBean("studentp", Student.class);
student.run();
System.out.println(student.getLessonList());
}
}
⑧运行截图
⑨总结
与上一篇文章的运行结果类似,而P命名空间是通过p:实体类变量名=“数值或者util集合”进行属性注入的,并通过测试类定义好的方法进行输出。总体来说较Property属性注入简便。