01-环境:
IDEA:2020
JDK:1.8
JVM:11
Gradle:7.3
Spring源码版本:5.3x
02-代码
package xyz.taichu.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author
* @site
* @company
* @create 2021-11-14 9:48
*/
public class Run {
public static void main(String[] args) {
System.out.println("hello world");
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("person.xml");
Person person= (Person) applicationContext.getBean("person");
}
}
1,文件结构:
03-打印报错信息
2,文字+图片
D:\Gitee\SpringCode\spring-source-code\mySpring\src\main\java\xyz\taichu\ioc\Run.java:5: 错误: 程序包org.springframework.context不存在
import org.springframework.context.ApplicationContext;
^
D:\Gitee\SpringCode\spring-source-code\mySpring\src\main\java\xyz\taichu\ioc\Run.java:6: 错误: 程序包org.springframework.context.support不存在
import org.springframework.context.support.ClassPathXmlApplicationContext;
^
D:\Gitee\SpringCode\spring-source-code\mySpring\src\main\java\xyz\taichu\ioc\Run.java:17: 错误: 找不到符号
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("person.xml");
^
符号: 类 ApplicationContext
位置: 类 Run
D:\Gitee\SpringCode\spring-source-code\mySpring\src\main\java\xyz\taichu\ioc\Run.java:17: 错误: 找不到符号
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("person.xml");
^
符号: 类 ClassPathXmlApplicationContext
位置: 类 Run
04-解决:
1,解释:
其实,原因还是对于Gradle不熟悉(正经人都用Maven,谁用Gradle!( ̄_, ̄ ))
没有在本模块中引用对于其他模块的依赖:
2,修改本模块的Gradle配置文件:
2,添加代码:
//说明:本人环境是Gradle7.3,
//很多教程中使用compile(project(":spring-beans")),已经被7弃用了
implementation project(path: ':spring-beans')
implementation project(path: ':spring-context')
重新运行dependcies,构建项目:
再运行代码-解决: