1. Maven 的配置
1.1 在 pom.xml 中配置 spring-webmvc
可在 Maven Repository 中搜索 Spring Web MVC 来获得对应配置
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.10.RELEASE</version></dependency>
2. HelloSpring
此处给出两种例子的目录
- HelloSpring 为没有 Service 层的例子
- IOCBasic 为有 Service 层的例子

2.1 配置 Dao 层(数据访问层)
2.1.1 接口
Dao 层的接口中只需定义需要使用的方法,一般为获取数据库数据的方法,例如:getUser()
2.1.2 实现类
继承接口,并对方法做出具体实现。
注:如果没有 Service 层,则需要实现set方法,方便 Spring 接管
2.2 配置 Service 层(业务层)
2.2.1 接口
同dao层接口,定义需要的方法
2.2.2 实现类
- 定义 Dao 层类的变量
- 实现set方法,方便 Spring 实现动态注入。具体为:将传入参数赋给 Dao 层类的变量
- 通过 Dao 层类的变量来调用 Dao 层方法
例:UserServiceImpl
public class UserServiceImpl implements UserService{private UserDao userDao;// 利用set方法实现动态值的注入public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void getUser() {userDao.getUser();}}
2.3 配置Spring的xml文件 重点
该文件为 Spring 的核心配置文件,起连接 Dao 层和 Service 层的作用,名字可以随意,此处为:beans.xml
Spring IoC 容器使用一种形式的配置元数据。此配置元数据表示您作为应用程序开发人员如何告诉 Spring 容器实例化,配置和组装应用程序中的对象。传统上,配置元数据以简单直观的 XML 格式提供。 ———— Spring Framework 中文文档
2.3.1 基本格式
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="..." class="..."> (1)(2)<!-- collaborators and configuration for this bean go here --></bean><!-- more bean definitions go here --><!-- 方式一 --> (3)<bean id="..." class="..."><property name="..." value="..."/></bean><!-- 方式二 --> (4)<bean id="daoA" class="..."><bean id="daoB" class="..."><bean id="service" class="..."><property name="..." ref="..."/> (5)(6)</bean></beans>
id属性是标识单个 bean 定义的字符串。class属性定义 bean 的类型并使用完全限定的类名。- 通过最简单的方式创建一个 HelloSpring
name属性为class绑定类中的 set 方法,如 set 方法名为:setStr,那么name = "str"value属性为设置的值
- 创建一个 动态绑定 的 Spring
name属性为 业务层实现类(ServiceImpl) 类中的 set 方法ref属性通常指定为需要绑定的 Dao 层实现类 ,例如上面代码中的daoA或daoB。它引用的是Spring容器中创建好的对象
该段摘录自 Spring Framework 中文文档,并进行了微小的改动,以符合本文内容 https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/core.html
实例:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 方式一 -->
<bean id="hello" class="com.yafnds.dao.HelloSpring">
<property name="str" value="Spring"/>
</bean>
<!-- 方式二 -->
<bean id="mysqlImpl" class="com.yafnds.dao.UserDaoMySQLImpl"/>
<bean id="oracleImpl" class="com.yafnds.dao.UserDaoOracleImpl"/>
<bean id="UserServiceImpl" class="com.yafnds.service.UserServiceImpl">
<property name="userDao" ref="oracleImpl"/>
</bean>
</beans>
2.3.2 Spring 创建对象相关
2.3.2.1 区别
原来创建对象:
类型 变量名 = new 类型(); HelloSpring hello = new HelloSpring();
Spring创建对象:
id = 变量名 class = new 的对象 property 相当于给对象中的属性设置值
2.3.2.2 注入对象的方式
- xml 依赖注入是通过构造函数
- property 是通过 set() 方法
- 所以 name 属性要和 set() 方法 set 后面的名字相同
2.4 运行 HelloSpring
public class MyTest {
public static void main(String[] args) {
// 获取 Spring 的上下文对象 必需
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 我们的对象现在都在 Spring 的管理中,如果要使用,直接去里面取出来
// 通过 id 取出 UserServiceImpl 对象
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
// 通过 UserServiceImpl 对象调用业务层方法
userServiceImpl.getUser();
}
}
