原型模式——Spring源码分析

原型模式在Spring框架中源码分析

1 Spring中原型bean的创建,就是原型模式的应用

Monster

dart 复制代码
package com.baidu.spring.bean;
public class Monster {
    private Integer id = 10;
    private String nickname = "牛魔王";
    private String skill = "芭蕉扇";

    public Monster() {
        System.out.println("monster 创建 ..");
    }

    public Monster(Integer id, String nickname, String skill) {
        this.id = id;
        this.nickname = nickname;
        this.skill = skill;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getSkill() {
        return skill;
    }

    public void setSkill(String skill) {
        this.skill = skill;
    }

    @Override
    public String toString() {
        return "Monster{" +
                "id=" + id +
                ", nickname='" + nickname + '\'' +
                ", skill='" + skill + '\'' +
                '}';
    }
}

ProtoType

dart 复制代码
package com.baidu.spring.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProtoType {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        Object bean = applicationContext.getBean("id01");
        System.out.println("bean = " + bean);

        Object bean2 = applicationContext.getBean("id01");
        System.out.println("bean2 = " + bean2);

        System.out.println(bean == bean2);


    }

}
复制代码
bean.xml
dart 复制代码
<?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">


    <bean id="id01" class="com.baidu.spring.bean.Monster" scope="prototype" />

</beans>

debug执行

相关推荐
wangchunyu1149 小时前
JAVA CompletableFuture异步编程实战
java·开发语言·python
paopaokaka_luck10 小时前
流浪动物救助与收养系统(DeepSeek AI对话、 ECharts多维分析、救助流程衔接、领养与回访衔接、 捐赠确认与公示、志愿者协作)
java·前端·javascript·人工智能·mysql·echarts·推荐算法
Thomas.Sir10 小时前
第9课:SpringBoot4.x Web开发基础:WebMvc自动配置、静态资源、请求参数接收
java·spring boot
程序 代码狂人10 小时前
切片表与分区表的区别(数据开发面试版)
java·大数据·开发语言·数据仓库·算法
2501_9445215911 小时前
Deepmerge 跨端三方库仓颉适配全流程
java·python·eclipse·github
南归北隐12 小时前
Spring AI Alibaba Graph框架实现MCP数据库查询实战
java·后端·spring ai·mcp·spring ai mcp
古法老登瞎折腾13 小时前
大数据智能运维实战(九):让 AI 查日志——找到异常,也保留上下文
java·大数据·运维·人工智能·spring ai·mcp
ai大模型-探索者13 小时前
Function Call与MCP(区别、联系与实战)
java·服务器·网络
略略略咯咯13 小时前
啊啊啊啊啊
java·服务器·数据库
雷一言说13 小时前
Spring AI 2.0 Tool Search:工具多了,只把相关的发给模型
spring