案例03 基于xml配置的setter方法注入案例

简介: 通过setter方法方式来演示Spring容器在应用中是如何实现依赖注入的,实现StudentService调用StudentDao的saveStudent操作。

通过setter方法方式来演示Spring容器在应用中是如何实现依赖注入的,实现StudentService调用StudentDao的saveStudent操作。

1. 创建项目

Idea创建Java项目,项目名称为:case03-spring-student02。

2. 导入spring相关jar包

case03-spring-student02项目下创建lib目录,在lib目录下导入Jar包:

    • 核心包

    spring-core-5.3.25.jar、

    spring-beans-5.3.25.jar、

    spring-context-5.3.25.jar、

    spring-expression-5.3.25.jar

      • 测试包

      junit-4.6.jar

        • 依赖包

        commons-logging-1.2.jar

        3. 创建Spring配置文件

        src目录下创建applicationContext.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"
               xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
            <!--将StudentDao类配置给Spring,让Spring创建其实例-->
            <bean id="studentDao" class="com.wfit.dao.StudentDao"/>
            <!--StudentService类配置给Spring,让Spring创建其实例-->
            <bean id="studentService" class="com.wfit.service.StudentService">
                <!--将studentDao实例注入到了studentService中-->
                <property name="studentDao" ref="studentDao"/>
            </bean>
        </beans>

        image.gif

        4. 创建StudentService类

        src目录下创建com.wfit.service包,此包目录下创建StudentService类,实现addStudent方法。

        public class StudentService {
            private StudentDao studentDao;
            public void setStudentDao(StudentDao studentDao) {
                this.studentDao = studentDao;
            }
            public void addStudent(){
                studentDao.saveStudent();
            }
        }

        image.gif

        5. 创建StudentDao类

        com.wfit.dao目录下创建StudentDao.java类。

        public class StudentDao {
            public void saveStudent(){
                System.out.println("保存学生信息成功!");
            }
        }

        image.gif

        6. 创建测试类

        src目录下创建com.wfit.test包,此包目录下创建TestStudent测试类。

        public class TestStudent {
            @Test
            public void test(){
                //加载applicationContext.xml配置
                ApplicationContext applicationContext =
                        new ClassPathXmlApplicationContext("applicationContext.xml");
                //通过容器获取StudentService实例
                StudentService studentService = applicationContext.getBean("studentService", StudentService.class);
                //调用addStudent方法
                studentService.addStudent();
            }
        }

        image.gif

        7.执行结果



        目录
        相关文章
        |
        2月前
        |
        XML Ubuntu Linux
        部署08---扩展-Win10配置WSL(Ubuntu)环境,WSL系统是什么意思,是Windows系统上的一个子系统, xml的一大特点是直链系统,直接链接你的CPU,硬盘和内存,如何用 WSL部署
        部署08---扩展-Win10配置WSL(Ubuntu)环境,WSL系统是什么意思,是Windows系统上的一个子系统, xml的一大特点是直链系统,直接链接你的CPU,硬盘和内存,如何用 WSL部署
        |
        6天前
        |
        XML Java 数据格式
        Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
        Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
        57 18
        Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
        |
        19天前
        |
        SQL XML Java
        mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
        当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
        26 1
        |
        1月前
        |
        XML Android开发 UED
        "掌握安卓开发新境界:深度解析AndroidManifest.xml中的Intent-filter配置,让你的App轻松响应scheme_url,开启无限交互可能!"
        【8月更文挑战第2天】在安卓开发中,scheme_url 通过在`AndroidManifest.xml`中配置`Intent-filter`,使应用能响应特定URL启动或执行操作。基本配置下,应用可通过定义特定URL模式的`Intent-filter`响应相应链接。
        80 12
        |
        1月前
        |
        Shell Android开发
        安卓scheme_url调端:在AndroidManifest.xml 中如何配置 Intent-filter?
        为了使Android应用响应vivo和oppo浏览器的Deep Link或自定义scheme调用,需在`AndroidManifest.xml`中配置`intent-filter`。定义启动的Activity及其支持的scheme和host,并确保Activity可由外部应用启动。示例展示了如何配置HTTP/HTTPS及自定义scheme,以及如何通过浏览器和adb命令进行测试,确保配置正确无误。
        |
        2月前
        |
        XML Java 关系型数据库
        Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
        Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
        |
        2月前
        |
        XML Java 数据格式
        支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
        支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
        |
        1月前
        |
        XML Java 数据格式
        Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
        这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
        Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
        |
        3月前
        |
        XML Java 数据格式
        java创建xml文件内容
        java创建xml文件内容
        |
        3月前
        |
        XML Java 数据格式
        java解析xml文件内容
        java解析xml文件内容