Spring IoC及DI依赖注入

news2024/9/22 15:39:06

Spring

1.Spring的含义:

Spring 可从狭义与广义两个角度看待

狭义的 Spring 是指 Spring 框架(Spring Fremework)

广义的 Spring 是指 Spring 生态体系

2.狭义的 Spring 框架

Spring 框架是企业开发复杂性的一站式解决方案

Spring 框架的核心是 IoC 容器和 AOP 面向切面编程

Spring Ioc 负责创建与管理系统对象,并在此基础上扩展功能

3.广义的 Spring 生态系统

4.Spring IoC 容器

4.1 IoC 控制反转:

(1)IoC 控制反转,全称是 Inverse of Control,是一种设计理念

(2)由代理人来创建与管理对象,消费者通过代理人来获取对象

(3)IoC 的目的是降低对象间的直接耦合

加入 IoC 容器将对象统一管理,让对象关联变为弱耦合

4.2 Ioc 容器是Spring生态的地基,用于统一创建和管理对象的依赖

4.3 Spring IoC 容器职责

(1)对象的控制权交由第三方统一管理(IoC控制反转)

(2)利用反射技术实现运行时对象创建与关联(DI依赖注入)

(3)基于配置提高应用程序的可维护性与扩展性

4.4 DI依赖注入

(1)IoC 是设计理念,是现代程序设计遵循的标准,是宏观目标

(2)DI (Dependency Injection) 是具体技术实现,是微观实现

(3)DI 在java中利用反射技术实现对象注入(Injection)

4.4.1 什么是对象依赖注入

依赖注入是指运行时将容器内对象利用反射赋给其他对象的操作

(1)基于Setter方法注入对象

<bean id="sweetApple" class="com.mkyuan.ioc.entity.Apple">
        <!--IoC 容器自动利用反射机制运行时调用setXXX方法为属性赋值-->
        <property name="title" value="红富士"></property>
        <property name="color" value="红色"></property>
        <property name="origin" value="欧洲"></property>
    </bean>

    <bean id="lily" class="com.mkyuan.ioc.entity.Child">
        <property name="name" value="莉莉"></property>
        <property name="apple" ref="sweetApple"></property>
    </bean>

案例:

BookDao 类

public interface BookDao {

    public void insert();
}

BookDaoImpl 类

public class BookDaoImpl implements BookDao{
    @Override
    public void insert() {
        System.out.println("向 MySQL Book 表插入一条数据");
    }
}

BookDaoOracleImpl 类

public class BookDaoOracleImpl implements BookDao{
    @Override
    public void insert() {
        System.out.println("向 Oracle Book 表插入一条数据");
    }
}

BookService 类

public class BookService {

    private BookDao bookDao;

    public BookDao getBookDao() {
        return bookDao;
    }

    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    public void purchase(){
        System.out.println("正在执行图书采购业务方法");
        bookDao.insert();
    }
}

applicationContext-dao.xml

<bean id="bookDao" class="com.mkyuan.ioc.bookshop.dao.BookDaoOracleImpl"></bean>

可通过选择Dao接口下不同的实现类注入对象

applicationContext-service.xml

  <bean id="bookService" class="com.mkyuan.ioc.bookshop.service.BookService">
        <property name="bookDao" ref="bookDao"></property>
    </bean>

BookShopApplication 类

public class BookShopApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-*.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
        bookService.purchase();
    }
}

(2)基于构造方法注入对象

<bean id="sweetApple" class="com.mkyuan.ioc.entity.Apple">
        <property name="title" value="红富士"></property>
        <property name="origin" value="欧洲"></property>
        <property name="color" value="红色"></property>
    </bean>
    <bean id="lily" class="com.mkyuan.ioc.entity.Child">
        <property name="name" value="莉莉"></property>
        <property name="apple" ref="sweetApple"></property>
    </bean>

4.4.2 注入集合对象

Company 类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Company {
    private Set<String> rooms;
    private Map<String, Computer> computers;
    private Properties info;
}

Computer 类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Computer {
    private String brand;
    private String type;
    private String sn;
    private Float price;
}

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="c1" class="com.mkyuan.ioc.entity.Computer">
        <constructor-arg name="brand" value="联想"></constructor-arg>
        <constructor-arg name="type" value="台式机"></constructor-arg>
        <constructor-arg name="sn" value="8389283012"></constructor-arg>
        <constructor-arg name="price" value="3085"></constructor-arg>
    </bean>
    <bean id="company" class="com.mkyuan.ioc.entity.Company">
        <property name="rooms">
            <set>
                <value>2001-总裁办</value>
                <value>2003-总经理办公室</value>
                <value>2010-研发部会议室</value>
                <value>2010-研发部会议室</value>
            </set>
        </property>
        <property name="computers">
            <map>
                <entry key="dev-88172" value-ref="c1"></entry>
                <entry key="dev-88173">
                    <bean class="com.mkyuan.ioc.entity.Computer">
                        <constructor-arg name="brand" value="联想"></constructor-arg>
                        <constructor-arg name="type" value="笔记本"></constructor-arg>
                        <constructor-arg name="sn" value="8389283012"></constructor-arg>
                        <constructor-arg name="price" value="5060"></constructor-arg>
                    </bean>
                </entry>
            </map>
        </property>

        <property name="info">
            <props>
                <prop key="phone">010-12345678</prop>
                <prop key="address">深圳市xxx路xx大厦</prop>
                <prop key="website">http://www.xxxx.com</prop>
            </props>
        </property>
    </bean>
</beans>

SpringApplication 类

public class SpringApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Company company = context.getBean("company", Company.class);
        String website = company.getInfo().getProperty("website");
        System.out.println(website);
        System.out.println(company);
    }
}

4.4.3 查看容器内对象

SpringApplication

public class SpringApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Company company = context.getBean("company", Company.class);
        String website = company.getInfo().getProperty("website");
        System.out.println(website);
        System.out.println(company);
        System.out.println("========================");
        //获取容器内所有BeanId数组
        String[] beanNames = context.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            System.out.println(beanName);
            System.out.println("类型:"+context.getBean(beanName).getClass().getName());
            System.out.println("内容:"+context.getBean(beanName));
        }
    }
}

5.Spring Ioc 初体验

5.1.案例:

(1)妈妈在早餐后给三个孩子分发餐后水果

(2)盘子里装有三个水果:红富士/青苹果/金帅

(3)孩子们口味不同:莉莉喜欢甜的/安迪喜欢酸的/露娜喜欢软的

如何让孩子得到喜欢的苹果

5.1.1 普通方式

Apple 类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Apple {
    private String title;
    private String color;
    private String origin;
}

Child 类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Child {
    private String name;
    private Apple apple;

    public void eat() {
        System.out.println(name + "吃到了" + apple.getOrigin() + "种植的" + apple.getTitle());
    }
}

Application 类

public class Application {
    public static void main(String[] args) {
        Apple apple1 = new Apple("红富士", "红色", "欧洲");
        Apple apple2 = new Apple("青苹果", "绿色", "中亚");
        Apple apple3 = new Apple("金帅", "黄色", "中国");
        Child lily = new Child("莉莉", apple1);
        Child andy = new Child("安迪", apple2);
        Child luna = new Child("露娜", apple3);
        lily.eat();
        andy.eat();
        luna.eat();
    }
}

5.1.2 IoC 方式

引入依赖

   <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.9.RELEASE</version>
  </dependency>

创建 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="sweetApple" class="com.mkyuan.ioc.entity.Apple">
        <property name="title" value="红富士"></property>
        <property name="origin" value="欧洲"></property>
        <property name="color" value="红色"></property>
    </bean>

    <bean id="sourApple" class="com.mkyuan.ioc.entity.Apple">
        <property name="title" value="青苹果"></property>
        <property name="origin" value="中亚"></property>
        <property name="color" value="绿色"></property>
    </bean>

    <bean id="softApple" class="com.mkyuan.ioc.entity.Apple">
        <property name="title" value="金帅"></property>
        <property name="origin" value="中国"></property>
        <property name="color" value="黄色"></property>
    </bean>
    <bean id="lily" class="com.mkyuan.ioc.entity.Child">
          <property name="name" value="莉莉"></property>
          <property name="apple" ref="sweetApple"></property>
      </bean>

      <bean id="andy" class="com.mkyuan.ioc.entity.Child">
          <property name="name" value="安迪"></property>
          <property name="apple" ref="sourApple"></property>
      </bean>

      <bean id="luna" class="com.mkyuan.ioc.entity.Child">
          <property name="name" value="露娜"></property>
          <property name="apple" ref="softApple"></property>
      </bean>

</beans>

SpringApplication 类

public class SpringApplication {
    //创建 spring IoC 容器,并且根据配置文件在容器中实例化对象
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Apple sweetApple = context.getBean("sweetApple", Apple.class);
        System.out.println(sweetApple);
        Child lily = context.getBean("lily", Child.class);
        lily.eat();
        Child andy = context.getBean("andy", Child.class);
        andy.eat();
        Child luna = context.getBean("luna", Child.class);
        luna.eat();
}

6.Bean 的配置方式

6.1 基于 XML 配置 bean

(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/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--bean标签默认通过默认构造方法创建对象-->
    <bean id="apple1" class="com.mkyuan.ioc.entity.Apple">
    </bean>
    <!--bean标签通过带参构造方法创建对象-->
    <bean id="apple2" class="com.mkyuan.ioc.entity.Apple">
        <constructor-arg name="title" value="红富士"></constructor-arg>
        <constructor-arg name="color" value="红色"></constructor-arg>
        <constructor-arg name="origin" value="欧洲"></constructor-arg>
        <constructor-arg name="price" value="19.8"></constructor-arg>
    </bean>
    <bean id="apple3" class="com.mkyuan.ioc.entity.Apple">
        <constructor-arg index="0" value="红富士"></constructor-arg>
        <constructor-arg index="1" value="红色"></constructor-arg>
        <constructor-arg index="2" value="欧洲"></constructor-arg>
        <constructor-arg index="3" value="19.8"></constructor-arg>
    </bean>
</beans>

(2)基于静态工厂实例化对象

AppleStaticFactory 类

/**
 * 静态工厂通过静态方法创建对象,隐藏创建对象的细节
 */
public class AppleStaticFactory {
    public static Apple createSweetApple() {
        Apple apple = new Apple();
        apple.setTitle("红富士");
        apple.setOrigin("欧洲");
        apple.setColor("红色");
        return apple;
    }
}
 <!--利用静态工厂获取对象-->
    <bean id="apple4" class="com.mkyuan.ioc.factory.AppleStaticFactory" factory-method="createSweetApple">
    </bean>

(3)基于工厂实例方法实例化对象

AppleFactoryInstance 类

/**
 * 工厂实例方法创建对象是指 Ioc 容器对工厂进行实例化并调用对应的实例方法创建对象的过程
 */
public class AppleFactoryInstance {

    public Apple createSweetApple() {
        Apple apple = new Apple();
        apple.setTitle("红富士");
        apple.setOrigin("欧洲");
        apple.setColor("红色");
        return apple;
    }
}
 <!--利用工厂实例方法获取对象-->
    <bean id="factoryInstance" class="com.mkyuan.ioc.factory.AppleFactoryInstance"></bean>
    <bean id="apple5" factory-bean="factoryInstance" factory-method="createSweetApple">
    </bean>

6.1.1 id和name属性相同点

(1)bean id与 name 都是设置对象在 IoC 容器中唯一标示

(2)两者在同一个配置文件中都不允许出现重复

(3)两者允许在多个配置文件中出现重复对象,新对象覆盖旧对象

6.1.2 id和name属性区别

(1)id 更为严格,一次只能定义一个对象标示(推荐)

(2)name 更为宽松,一次允许定义多个对象标示

 <bean name="apple2,apple7" class="com.mkyuan.ioc.entity.Apple">
        <constructor-arg name="title" value="红富士2号"></constructor-arg>
        <constructor-arg name="color" value="红色"></constructor-arg>
        <constructor-arg name="origin" value="欧洲"></constructor-arg>
        <constructor-arg name="price" value="29.8"></constructor-arg>
    </bean>

(3)id 与 name 的命名要求有意义,按照驼峰命名书写

(4) 没有id和name默认使用类名全称作为bean标示

 <bean  class="com.mkyuan.ioc.entity.Apple">
        <constructor-arg name="title" value="红富士3号"></constructor-arg>
        <constructor-arg name="color" value="红色"></constructor-arg>
        <constructor-arg name="origin" value="欧洲"></constructor-arg>
        <constructor-arg name="price" value="29.8"></constructor-arg>
    </bean>
Apple apple2 = context.getBean("com.mkyuan.ioc.entity.Apple", Apple.class);
System.out.println(apple2);

6.2 基于注解配置 bean

6.2.1 基于注解的优势

(1)摆脱繁琐的XML形式的bean与依赖注入的配置

(2)基于"声明式"的原则,更适合轻量级的现代企业的应用

(3)让代码的可读性变的更好,研发人员拥有更好的开发体验

6.2.2 三类注解

(1)组件类型注解-声明当前类的功能与职责

(2)自动装配注解-根据属性特征自动注入对象

(3)元数据注解-更细化的辅助 IoC 容器管理对象的注解

6.2.3 四种组件类型注解

6.2.4 两种自动装配注解

6.3 基于 java 代码配置 bean

Config 类

@Configuration
@ComponentScan(basePackages = "com.mkyuan")
public class Config {

    @Bean
    public UserDao userDao() {
        UserDao userDao = new UserDao();
        System.out.println("已创建:" + userDao);
        return userDao;
    }

    @Bean
    @Primary
    public UserDao userDao1() {
        UserDao userDao = new UserDao();
        System.out.println("已创建:" + userDao);
        return userDao;
    }

    @Bean
    public UserService userService(UserDao userDao, EmployeeDao employeeDao) {
        UserService userService = new UserService();
        System.out.println("已创建:" + userService);
        userService.setUserDao(userDao);
        System.out.println("调用setUserDao:" + userDao);
        userService.setEmployeeDao(employeeDao);
        System.out.println("调用setEmployeeDao:" + employeeDao);
        return userService;
    }

    @Bean
    @Scope("prototype")
    public UserController userController(UserService userService) {
        UserController userController = new UserController();
        System.out.println("已创建:" + userController);
        userController.setUserService(userService);
        System.out.println("调用setUserService:" + userService);
        return userController;
    }
}

7.bean scope 属性

7.1 bean scope属性

bean scope属性用于决定对象何时被创建与作用范围

bean scope 配置将影响容器内对象的数量

bean scope 默认值singleton(单例),指全局共享一个对象实例,默认情况下bean会在 Ioc 容器创建后自动实例化,全局唯一

7.2 singleton与 prototype 对比

8.bean 的生命周期

Order 类

public class Order {
    private Float price;

    private Integer quantity;

    private Float total;

    public Order() {
        System.out.println("创建Order对象," + this);
    }

    public void init() {
        System.out.println("执行init方法");
        total = price * quantity;
    }

    public void destroy() {
        System.out.println("释放与订单对象相关的资源");
    }

    public void pay() {
        System.out.println("订单金额为:" + total);
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        System.out.println("设置price:" + price);
        this.price = price;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        System.out.println("设置quantity:" + quantity);
        this.quantity = quantity;
    }

    public Float getTotal() {
        return total;
    }

    public void setTotal(Float total) {
        this.total = total;
    }
}

applicationContext.xml

<bean id="order1" class="com.mkyuan.ioc.entity.Order" init-method="init" destroy-method="destroy">
        <property name="price" value="19.8"></property>
        <property name="quantity" value="20"></property>
</bean>

SpringApplication 类

public class SpringApplication {
    public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      System.out.println("==========IoC容器已初始化=============");
      Order order1 = context.getBean("order1", Order.class);
      order1.pay();
      ((ClassPathXmlApplicationContext) context).registerShutdownHook();
    }
}

执行结果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/759316.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据库java中jdbcTemplate的事务问题

1.什么都不设置事务是默认提交的 两次获取的连接是不是一样的 参考文献(重磅): (542条消息) JdbcTemplate的事务控制_jdbctemplate transactionmanager_DayDayUp丶的博客-CSDN博客 PostMapping("/pinYin22")CrossOriginTransactionalpublic String pinYin22(HttpS…

【js实现语言国际化】使用json配置文件实现

需求&#xff1a;使用js让项目实现中文简体、繁体跟英文的切换&#xff0c;实现语言国际化 首先准备三种json配置文件&#xff1a; en.json {"textOne": "Today is Monday","textTwo": "Tomorrow is Tuesday","textThree"…

F#奇妙游(14):F#实现WPF的绑定

WPF中的绑定 绑定在UI开发中是一个非常重要的概念&#xff0c;它可以让我们的UI界面和数据模型之间建立起联系&#xff0c;当数据模型发生变化时&#xff0c;UI界面也会随之变化&#xff0c;反之亦然。这样的好处是显而易见的&#xff0c;我们不需要手动去更新UI界面&#xff…

金智教育IPO过会:计划募资约6亿元,郭超、史鸣杰为实控人

7月13日&#xff0c;深圳证券交易所披露的信息显示&#xff0c;江苏金智教育信息股份有限公司&#xff08;下称“金智教育”&#xff09;获得上市委会议通过。据贝多财经了解&#xff0c;金智教育于2022年6月30日递交上市申请材料&#xff0c;先后递交了6个版本的招股书&#x…

NDK OpenGL与OpenCV实现大眼萌特效

NDK​系列之OpenGL与OpenCV实现大眼萌特效&#xff0c;本节主要是在上一节OpenGL仿抖音极快极慢录制特效视频上增加大眼萌的特效。 OpenGL视频特效系列&#xff1a; NDK OpenGL渲染画面效果 NDK OpenGL离屏渲染与工程代码整合 NDK OpenGL仿抖音极快极慢录制特效视频 NDK O…

通讯录实现

普通版 需求 通讯录可以用来存储1000个人的信息&#xff0c;每个人的信息包括&#xff1a;姓名、性别、年龄、电话、住址 提供方法&#xff1a; 添加联系人信息删除指定联系人信息查找指定联系人信息修改指定联系人信息显示所有联系人信息清空所有联系人以名字排序所有联系…

【Linux后端服务器开发】UDP协议

目录 一、端口号 二、UDP报头格式 三、UDP的特点 四、UDP协议实现网络聊天群 一、端口号 端口号port标识了一个主机上进行通信的不同的应用程序。 0 ~ 1023&#xff1a;系统端口号&#xff0c;HTTP、FTP、SSH等这些广为使用的应用层协议&#xff0c;它们的端口号都是固定…

Windows软件开发常用技巧总结

本文总结了本人在日常工作学习中遇到的问题及其解决方法&#xff0c;没有固定的涉及领域 目的就是为了在下一次遇到类似问题的时候方便查找&#xff0c;从而快速解决问题 本文不定时更新~ 目录 Windows使用 如何实现桌面图标随意排列 文件资源管理器相关 显示隐藏文件 修改…

Linux--获取最近一次的进程退出码:echo $?

举例&#xff1a; #include <stdio.h> int main() { printf("hello world,pid: %d,ppid: %…

JavaFx 用户界面控件3——TableView

1.表格视图 TableView ableView是JavaFX提供的一个强大的控件&#xff0c;可以用于显示表格数据。它通过为TableView设定items属性&#xff08;存储行数据的ObservableList对象&#xff09;和列属性&#xff08;TableColumn对象&#xff09;来完成数据填充与展示。 以下是一个…

如何做一线leader

文章目录 道领导力五个层次关键&#xff1a;信任 处事原则 术避坑指南事急则乱员工沟通向上管理人才招聘人才培养裁人员工关怀 道 领导力 五个层次 职位 当面交代事情&#xff0c;观察眼神、语气。反复确认有没有问题&#xff0c;如果有可以及时讨论策略&#xff0c;准备资源…

IDEA连接达梦数据库

在 IntelliJ IDEA 中连接达梦数据库&#xff0c;可以按照以下步骤进行操作&#xff1a; 1. 打开 IntelliJ IDEA&#xff0c;进入项目。 2. 在顶部菜单栏选择 "View" -> "Tool Windows" -> "Database"&#xff0c;打开数据库工具窗口。 3.…

自旋锁的优势和特点

ucos为何没自旋锁&#xff1f; UC/OS是一个适用于嵌入式系统的实时操作系统&#xff0c;它的设计目标是提供一种轻量级的任务调度和同步机制。相比于一般的操作系统&#xff0c;UC/OS在实现上更加精简&#xff0c;因此并没有像Linux那样的完整的锁机制。 UC/OS提供了一些基本…

17 | 从后端到前端:微服务后,前端如何设计?

微服务架构通常采用前后端分离的设计方式。作为企业级的中台&#xff0c;在完成单体应用拆分和微服务建设后&#xff0c;前端项目团队会同时面对多个中台微服务项目团队&#xff0c;这时候的前端人员就犹如维修电工一样了。 面对如此多的微服务暴露出来的 API 服务&#xff0c…

ChatGPT与Claude对比分析

一 简介 1、ChatGPT: 访问地址&#xff1a;https://chat.openai.com/ 由OpenAI研发,2022年11月发布。基于 transformer 结构的大规模语言模型,包含1750亿参数。训练数据集主要是网页文本,聚焦于流畅的对话交互。对话风格友好,回复通顺灵活,富有创造性。存在一定的安全性问题,可…

山西电力市场日前价格预测【2023-07-17】

日前价格预测 预测明日&#xff08;2023-07-17&#xff09;山西电力市场全天平均日前电价为335.50元/MWh。其中&#xff0c;最高日前电价为377.51元/MWh&#xff0c;预计出现在06: 00。最低日前电价为271.94元/MWh&#xff0c;预计出现在13: 30。 价差方向预测 1&#xff1a;实…

力扣 45. 跳跃游戏 II

题目来源&#xff1a;https://leetcode.cn/problems/jump-game-ii/description/ C题解1&#xff1a;因为每一步都要能走到下一个更远的地方&#xff0c;就比如 [2,3,1,1,4]&#xff0c;第一步虽然可以到索引2的位置&#xff0c;但是到索引1的位置下一步可以走更远。所以需要记录…

工厂方法模式详解

文章目录 前言一、工厂方法模式的定义二、举个例子三、工厂方法模式的缺点总结 前言 工厂方法模式是应用比较广泛的一种设计模式&#xff0c;它相对于简单工厂模式进行了一些优化&#xff0c;如果再增加一个具体产品不用修改代码&#xff0c;也不会违反开闭原则。 一、工厂方法…

Spring 6【什么是Spring 6、Spring框架介绍 、Spring IoC/DI 详解 】(一)-全面详解(学习总结---从入门到深化)

目录 一、Spring 6 二、Spring框架介绍 三、Spring IoC/DI 详解 一、Spring 6 1. 为什么要学习Spring 6 Spring 作为 Java程序员必会武功&#xff0c;无论是Spring的哪个版本&#xff0c;至少需要会一版本。再此基础上不会一套 组合拳SSM或SSH&#xff0c;出门都不好意思…

DAO(Data Access Object)

ProductDAO.java Search.java (Servlet) Compile Tomcat http://localhost:8080/book/chapter15/search.jsp Insert.java Compile http://localhost:8080/book/chapter15/insert.jsp