描述信息
最近卷起来,系统学习Spring;俗话说:万事开头难;创建一个Spring项目在网上找了好久没有找到好的方式;摸索了半天产出如下文档。
在 Idea 中新建项目
![](https://img-blog.csdnimg.cn/4cee51f9f2cf4cc180e65a3e90778e05.png)
填写信息如下
![](https://img-blog.csdnimg.cn/f5db70271dd44ff5979eb78cd8a09ccc.png)
生成项目目录结构
![](https://img-blog.csdnimg.cn/d0f4edb983cd4beabcf02015d4051f69.png)
pom添加依赖
![](https://img-blog.csdnimg.cn/9290580947aa4162b24d75ddb317866d.png)
org.springframework
spring-core
5.3.31
org.springframework
spring-context
5.3.13
org.springframework
spring-beans
5.3.31
org.springframework
spring-aop
5.3.29
定义 applicationContext.xml 文件,并添加扫描路径
![](https://img-blog.csdnimg.cn/d0bb4d73cad5461284df0aae332190a9.png)
添加一个测试类型UserService
![](https://img-blog.csdnimg.cn/eae13d289909433289804f3a7e5977ed.png)
在测试类 Main 中进行测试
![](https://img-blog.csdnimg.cn/16f19dd2ad0a422199f023f2a5288639.png)
public class Main {
public static void main(String[] args) {
// 扫描路径
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取bean
UserService userService = (UserService) context.getBean("userService");
userService.print();
}
}
可以看到 userService bean 已经初始化好了,并且执行了print 方法。
|