基于XML配置方式进行bean组件的声明和setter方式的DI注入

  1. 三层架构实现JdbcTemplate和dao、service、controller的ioc组件配置
  • 父工程依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>ssm-spring-part</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>spring-ioc-xml-practice-02</module>
    </modules>
    <packaging>pom</packaging>
    <description>SSM学习教程</description>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!--如下依赖,是每个子项目都需要的-->
    <dependencies>
        <!--spring context依赖-->
        <!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了,包括ioc,spring-aop,spring-core,spring-context等-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.6</version>
        </dependency>

        <!--junit5测试-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- 数据库驱动和连接池-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>

        <!-- spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.0.6</version>
        </dependency>

    </dependencies>

</project>
  • StudentDao
package com.example.dao;

import com.example.pojo.Student;

import java.util.List;

/**
 * Created by 19920728 on 2023/10/23 22:12
 * 三层架构 dao 层,通过 ioc 容器获取
 */
public interface StudentDao {
    public List<Student> queryAll();
}

  • StudentDaoImpl
package com.example.dao.impl;

import com.example.dao.StudentDao;
import com.example.pojo.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * Created by 19920728 on 2023/10/23 22:13
 */
public class StudentDaoImpl implements StudentDao {

    // 通过 JdbcTemplate 获取
    private JdbcTemplate jdbcTemplate;

    // ioc 中使用 setter di 将 jdbcTemplate 注入
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Student> queryAll() {
        String sql = "select id, name, gender, age, class as classes from students";
        List<Student> studentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
        return studentList;
    }
}
  • StudentService
package com.example.service;

import com.example.pojo.Student;

import java.util.List;

/**
 * Created by 19920728 on 2023/10/23 22:17
 */
public interface StudentService {
    public List<Student> getList();
}
  • StudentServiceImpl
package com.example.service.impl;

import com.example.dao.StudentDao;
import com.example.pojo.Student;
import com.example.service.StudentService;

import java.util.List;

/**
 * Created by 19920728 on 2023/10/23 22:17
 */
public class StudentServiceImpl implements StudentService {

    // 从 ioc 容器中直接获取对象
    private StudentDao studentDao;

    public void setStudentDao(StudentDao studentDao){
        this.studentDao = studentDao;
    }

    @Override
    public List<Student> getList() {
        List<Student> students = studentDao.queryAll();
        return students;
    }
}
  • StudentController
package com.example.controller;

import com.example.pojo.Student;
import com.example.service.StudentService;

import java.util.List;

/**
 * Created by 19920728 on 2023/10/23 22:19
 */
public class StudentController {

    // 从ioc容器中直接获取
    private StudentService studentService;

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    public void findAll() {
        List<Student> list = studentService.getList();
        System.out.println("controller: " + list);
    }
}

  • spring ioc xml配置
<?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">

    <!--以下所有对象全部交给 Spring IOC 容器实例化-->

    <!--加载外部配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置组件 DruidDataSource,属性注入,这样构造spring ioc容器后,就会自动构造出一个 dataSource 实例化对象-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${atguigu.url}"/>
        <property name="driverClassName" value="${atguigu.driver}"/>
        <property name="username" value="${atguigu.username}"/>
        <property name="password" value="${atguigu.password}"/>
    </bean>

    <!-- 配置组件 JdbcTemplate,数据库连接池对象属性注入-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置 dao,注意这里实例化的是一个 class,不能是 interface-->
    <bean id="studentDao" class="com.example.dao.impl.StudentDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

    <!--配置 service-->
    <bean id="studentService" class="com.example.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao"/>
    </bean>

    <!--配置 controller-->
    <bean id="studentController" class="com.example.controller.StudentController">
        <property name="studentService" ref="studentService"/>
    </bean>

</beans>

整体目录结构如下:

  1. 思路

  2. 测试

    /**
     * 三层架构获取组件,测试
     */
    @Test
    public void testFindAll() {
        // 1. 构造ioc容器
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-02.xml");
        // 2. 获取 controller bean
        StudentController controller = applicationContext.getBean(StudentController.class);
        // 3. 调用方法
        controller.findAll();
        // 4. 关闭ioc容器,因为 ioc 容器的关闭方法是其实现类提供的,因此改用 ApplicationContext 的实现类来声明引用
        applicationContext.close();
    }
  1. 结果

  2. 此种方式的缺点

  • 注入的属性必须添加 setter 方法,代码结构混乱
  • 配置文件和Java代码分离,编写不是很方便
  • XML配置文件解析效率低
posted @ 2023-10-20 00:24  LoremMoon  阅读(6)  评论(0编辑  收藏  举报