传统MVC模式下,在Dao层添加操作时,Controller层也要进行修改,耦合性高,且违反了“开闭原则”
提出Ioc思想,由Ioc容器为类创建对象,而不是自己new
首先学习面向xml文件的Spring文件的Spring思想,当然后期会学到面向注解的Spring思想
在pom.xml文件引入依赖
在Resources文件夹下创建一个名为ApplicationContext.xml的Spring配置文件后,声明Bean对象
(注意:如果不是在resources文件夹下创建的话,初始化程序的时候xml文件不会被默认加载)
id指的是对象名称,class表示该对象的作用域、路径
固定模板
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
创建测试类来测试一下