文章目录
Spring配置文件-import知识要点
引入其他配置文件(分模块开发)
实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以,可以将部分配置拆解到其他配置文件中,而在Spring主配置文件通过import标签进行加载
import resource="applicationContext-xxx.xml"
案例演示
创建spring2.xml
在另一个spring.xml中引入
这样就可以很好的区分两个配置文件的功能了,并且可以同时利用
Spring的重点配置
Spring相关API
ApplicationContext的继承体系
applicationContext:接口类型,代表应用上下文,可以通过其实例获得Spring容器中的Bean对象
紫色是接口,绿色是抽象类
ApplicationContext的实现类
getBean()方法使用
举例使用
编写UserController.java
package com.taotao.demo;
import com.taotao.domain.User;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* create by 刘鸿涛
* 2022/4/5 17:59
*/
public class UserController {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
// UserService userService = (UserService) app.getBean("userService");
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
测试运行
知识要点
Spring重点API
ApplicationContext app = new ClasspathXmlApplicationContext("xml文件");
app.getBean("id");
app.getBean(class);