1. Maven 的配置

1.1 在 pom.xml 中配置 spring-webmvc

可在 Maven Repository 中搜索 Spring Web MVC 来获得对应配置
image.png

  1. <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-webmvc</artifactId>
  5. <version>5.2.10.RELEASE</version>
  6. </dependency>

2. HelloSpring

此处给出两种例子的目录

  1. HelloSpring 为没有 Service 层的例子
  2. IOCBasic 为有 Service 层的例子

image.png

2.1 配置 Dao 层(数据访问层)

2.1.1 接口

Dao 层的接口中只需定义需要使用的方法,一般为获取数据库数据的方法,例如:getUser()

2.1.2 实现类

继承接口,并对方法做出具体实现。

注:如果没有 Service 层,则需要实现set方法,方便 Spring 接管

2.2 配置 Service 层(业务层)

2.2.1 接口

同dao层接口,定义需要的方法

2.2.2 实现类

  1. 定义 Dao 层类的变量
  2. 实现set方法,方便 Spring 实现动态注入。具体为:将传入参数赋给 Dao 层类的变量
  3. 通过 Dao 层类的变量来调用 Dao 层方法

例:UserServiceImpl

  1. public class UserServiceImpl implements UserService{
  2. private UserDao userDao;
  3. // 利用set方法实现动态值的注入
  4. public void setUserDao(UserDao userDao) {
  5. this.userDao = userDao;
  6. }
  7. public void getUser() {
  8. userDao.getUser();
  9. }
  10. }

2.3 配置Spring的xml文件 重点

该文件为 Spring 的核心配置文件,起连接 Dao 层和 Service 层的作用,名字可以随意,此处为:beans.xml

Spring IoC 容器使用一种形式的配置元数据。此配置元数据表示您作为应用程序开发人员如何告诉 Spring 容器实例化,配置和组装应用程序中的对象。传统上,配置元数据以简单直观的 XML 格式提供。 ———— Spring Framework 中文文档

2.3.1 基本格式

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="..." class="..."> (1)(2)
  7. <!-- collaborators and configuration for this bean go here -->
  8. </bean>
  9. <!-- more bean definitions go here -->
  10. <!-- 方式一 --> (3)
  11. <bean id="..." class="...">
  12. <property name="..." value="..."/>
  13. </bean>
  14. <!-- 方式二 --> (4)
  15. <bean id="daoA" class="...">
  16. <bean id="daoB" class="...">
  17. <bean id="service" class="...">
  18. <property name="..." ref="..."/> (5)(6)
  19. </bean>
  20. </beans>
  1. id 属性是标识单个 bean 定义的字符串。
  2. class 属性定义 bean 的类型并使用完全限定的类名。
  3. 通过最简单的方式创建一个 HelloSpring
    1. name 属性为 class 绑定类中的 set 方法,如 set 方法名为:setStr ,那么 name = "str"
    2. value 属性为设置的值
  4. 创建一个 动态绑定 的 Spring
    1. name 属性为 业务层实现类(ServiceImpl) 类中的 set 方法
    2. ref 属性通常指定为需要绑定的 Dao 层实现类 ,例如上面代码中的 daoAdaoB。它引用的是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 注入对象的方式
  1. xml 依赖注入是通过构造函数
  2. property 是通过 set() 方法
  3. 所以 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();
    }
}

2.5 绑定逻辑