• 第一个Spring程序


    通过《Spring IoC容器》的学习,读者对 Spring 的 IoC 容器已经有了一个初步的了解。下面通过具体的案例演示 IoC 容器的使用。

    1. 创建项目

    在 MyEclipse 中创建 Web 项目 springDemo01,将 Spring 框架所需的 JAR 包复制到项目的 lib 目录中,并将添加到类路径下,添加后的项目如图 1 所示。


    图 1 Spring所需的JAR包

    2. 创建 PersonDao 接口

    在项目的 src 目录下创建一个名为 com.mengma.ioc 的包,然后在该包中创建一个名为 PersonDao 的接口,并在接口中添加一个 add() 方法,如下所示。

    1. package com.mengma.ioc;
    2. public interface PersonDao {
    3. public void add();
    4. }
    3. 创建接口实现类 PersonDaoImpl
    

    在 com.mengma.ioc 包下创建 PersonDao 的实现类 PersonDaoImpl,编辑后如下所示。

    1. package com.mengma.ioc;
    2. public class PersonDaoImpl implements PersonDao {
    3. @Override
    4. public void add() {
    5. System.out.println("save()执行了...");
    6. }
    7. }

    上述代码中,PersonDaoImpl 类实现了 PersonDao 接口中的 add() 方法,并且在方法调用时会执行输出语句。

    4. 创建 Spring 配置文件

    在 src 目录下创建 Spring 的核心配置文件 applicationContext.xml,编辑后如下所示。

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans
    4. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    5. <bean id="personDao" class="com.mengma.ioc.PersonDaoImpl" />
    6. beans>

    上述代码中,第 2~5 行代码是 Spring 的约束配置,第 7 行代码表示在 Spring 容器中创建一个 id 为 personDao 的 bean 实例,其中 id 表示文件中的唯一标识符,class 属性表示指定需要实例化 Bean 的实全限定类名(包名+类名)。

    需要注意的是,Spring 的配置文件名称是可以自定义的,通常情况下,都会将配置文件命名为 applicationContext.xml(或 bean.xml)。

    5. 编写测试类

    在 com.mengma.ioc 包下创建测试类 FirstTest,并在该类中添加一个名为 test1() 的方法,编辑后如下所示。

    1. package com.mengma.ioc;
    2. import org.junit.Test;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;
    5. public class FirstTest {
    6. @Test
    7. public void testl() {
    8. // 定义Spring配置文件的路径
    9. String xmlPath = "applicationContext.xml";
    10. // 初始化Spring容器,加载配置文件
    11. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
    12. xmlPath);
    13. // 通过容器获取personDao实例
    14. PersonDao personDao = (PersonDao) applicationContext
    15. .getBean("personDao");
    16. // 调用 personDao 的 add ()方法
    17. personDao.add();
    18. }
    19. }
    上述代码中,首先定义了 Spring 配置文件的路径,然后创建 Spring 容器,接下来通过 Spring 容器获取了 personDao 实例,最后调用实例的 save() 方法。
    

    6. 运行项目并查看结果

    使用 JUnit 测试运行 test1() 方法,运行成功后,控制台的输出结果如图 2 所示。

    从图 2 的输出结果中可以看出,程序已经成功输出了“save()执行了…”语句。在程序执行时,对象的创建并不是通过 new 一个类完成的,而是由 Spring 容器管理实现的。这就是 Spring IoC 容器思想的工作机制。


    图 2 输出结果

  • 相关阅读:
    C++ primer 查漏补缺九:第六章 函数
    CleanMyMac X2023最新版安装图文详解
    单面PCB布线阻抗的工程设计
    深度学习——模型选择、欠拟合和过拟合
    APT攻击与零日漏洞
    交易用户如何去使用l2行情数据api接口?
    Hive3:数据的加载与导出
    Spring的使用
    算法学习笔记 4-1 二分算法(Binary-Search):致敬经典,超越经典 与 LeetCode真题(Java)
    商城免费搭建之java商城 开源java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
  • 原文地址:https://blog.csdn.net/unbelievevc/article/details/126221055