iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring中Bean的作用域和自动装配方式
  • 870
分享到

Spring中Bean的作用域和自动装配方式

2024-04-02 19:04:59 870人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录Bean的作用域默认配置scope = “singleton”scope = “prototype”Bean的自动装配通过name自动装配通过type自动装配Bean的作用域 s

Bean的作用域

spring中bean的作用域共有singleton、prototype、request、session、application、websocket六种

其中后四种都是用在WEB应用程序中的,主要介绍前两种singleton(单例)和prototype(原型)

Bean的作用域范围为singleton时,所有实例共享一个对象。

Spring的默认配置为scope = “singleton”,以下两种配置的效果是一样的:

默认配置


<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--Spring默认配置为scope = "singleton"-->
    <bean id = "user" class="indi.stitch.pojo.User" />
</beans>

scope = “singleton”


<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--Spring默认配置为scope = "singleton"-->
    <bean id = "user" class="indi.stitch.pojo.User" scope = "singleton" />
</beans>

测试类及输出结果:


import indi.stitch.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    
    @Test
    public void test2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("namespace.xml");
        User user = context.getBean("user", User.class);
        User user2 = context.getBean("user", User.class);
        System.out.println(user == user2);
    }
}

在这里插入图片描述

scope = “prototype”


<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--Spring默认配置为scope = "singleton"-->
    <bean id = "user" class="indi.stitch.pojo.User" scope = "prototype" />
</beans>

测试类及输出结果:


import indi.stitch.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    @Test
    public void test2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("namespace.xml");
        User user = context.getBean("user", User.class);
        User user2 = context.getBean("user", User.class);
        System.out.println(user == user2);
    }
}

在这里插入图片描述

Bean的自动装配

Spring中Bean的自动装配基于autowired标签实现

首先创建实体类People、Cat、Dog,People和Cat、Dog是组合关系,People中定义了依赖于Cat、Dog的属性

People实体类


package indi.stitch.pojo;
public class People {
    private Cat cat;
    private Dog dog;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                '}';
    }
}

Cat实体类


package indi.stitch.pojo;
public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}

Dog实体类


package indi.stitch.pojo;
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}

通过name自动装配


<?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 = "cat" class="indi.stitch.pojo.Cat" />
    <bean id = "dog" class="indi.stitch.pojo.Dog" />
    <!--在Spring上下文中通过检索name完成自动装配,检索依据为bean中属性的set方法除set部分外的后缀-->
    <bean id = "people" class="indi.stitch.pojo.People" autowire="byName"/>
</beans>

测试类及输出结果:


import indi.stitch.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}

输出结果

在这里插入图片描述

通过type自动装配


<?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 = "cat" class="indi.stitch.pojo.Cat" />
    <bean id = "dog" class="indi.stitch.pojo.Dog" />
    <!--在Spring上下文中通过对属性对应类型进行检索完成自动装配,Spring配置中不能存在被依赖的相同类型的多个bean,被依赖的bean在Spring中配置时可以省略id属性-->
    <bean id = "people" class="indi.stitch.pojo.People" autowire="byType"/>
</beans>

测试类和结果和上面相同


import indi.stitch.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}

输出结果

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Spring中Bean的作用域和自动装配方式

本文链接: https://www.lsjlt.com/news/136959.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring中Bean的作用域和自动装配方式
    目录Bean的作用域默认配置scope = “singleton”scope = “prototype”Bean的自动装配通过name自动装配通过type自动装配Bean的作用域 S...
    99+
    2024-04-02
  • Spring Bean集合注入和自动装配的方法
    本篇内容介绍了“Spring Bean集合注入和自动装配的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、Spring Be...
    99+
    2023-07-02
  • spring中bean的继承与自动装配的用法
    本篇内容介绍了“spring中bean的继承与自动装配的用法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Spring之Bean的基本概念大...
    99+
    2023-05-30
    spring
  • 详解Spring Bean的集合注入和自动装配
    目录一、Spring Bean 集合注入集合常用标签案例二、Spring Bean自动装配什么是自动装配自动装配的方式案例注意点一、Spring Bean 集合注入 在【Spring...
    99+
    2024-04-02
  • Spring使用注解实现Bean的自动装配
    目录一、利用注解方式注入属性二、@Autowired三、@Qualifier四、@Resource总结一、利用注解方式注入属性 <xml version="1.0" enco...
    99+
    2024-04-02
  • spring装配bean的方式有哪些
    Spring装配Bean的方式有以下几种:1. 基于XML配置文件:通过在XML配置文件中定义Bean的方式进行装配,可以使用元素定...
    99+
    2023-09-27
    spring bean
  • Spring装配Bean之如何实现组件扫描和自动装配
    这篇文章给大家分享的是有关Spring装配Bean之如何实现组件扫描和自动装配的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Spring从两个角度来实现自动化装配:组件扫描:Spring会自动发现应用上下文中所创...
    99+
    2023-05-31
    spring bean
  • Spring中Bean的作用域是什么
    这篇文章给大家介绍Spring中Bean的作用域是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一、Bean的作用域首先我们来讲一下有关于bean的作用域,一般情况下,我们书写在IOC容器中的配置信息,会在我们的I...
    99+
    2023-06-20
  • Spring中的Bean作用域是什么
    本文小编为大家详细介绍“Spring中的Bean作用域是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“Spring中的Bean作用域是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。概述scope用来声明...
    99+
    2023-06-30
  • 一文搞懂Spring中的Bean作用域
    目录概述Singletonprototyperequestsessionapplication概述 scope用来声明容器中的对象所应该处的限定场景或者说该对象的存活时间,即容器在对...
    99+
    2024-04-02
  • spring IOC容器的Bean管理XML自动装配过程
    目录什么是自动装配?自动装配过程1. 创建 2 个类2. 配置文件3. 测试方法什么是自动装配? 在之前的内容中,每给属性注入值都要一个个的用 property 标签来完成,比如: ...
    99+
    2024-04-02
  • Spring使用@Autowired注解实现自动装配方式
    目录Spring支持注解配置引入注解依赖启用注解使用@Autowired注解实现自动装配1、IOC容器配置2、实体类使用@Autowired注解注入属性3、测试结果@Autowire...
    99+
    2024-04-02
  • 使用Spring注解怎么实现Bean自动装配功能
    这篇文章将为大家详细讲解有关使用Spring注解怎么实现Bean自动装配功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。使用须知:导入约束:context约束配置注解的支持: contex...
    99+
    2023-06-14
  • Spring Bean中的作用域和生命周期实例分析
    这篇文章主要介绍“Spring Bean中的作用域和生命周期实例分析”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring Bean中的作用域和生命周期实例分析”文章能帮助大...
    99+
    2023-07-02
  • Java Spring中Bean的作用域及生命周期
    目录1.Bean的作用域1.1 被修改的Bean案例1.2 为什么使用单例模式作为默认作用域1.3 作用域1.4 Bean的6种作用域1.5 设置作用域2.Spring执行流程和Be...
    99+
    2022-11-13
    Java Spring中Bean的作用域 Spring中Bean的生命周期
  • spring IOC容器的Bean管理XML自动装配怎么实现
    这篇文章主要讲解了“spring IOC容器的Bean管理XML自动装配怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“spring IOC容器的Bean管理XML...
    99+
    2023-06-30
  • Spring自动装配之方法、构造器位置的自动注入操作
    Spring自动装配之方法、构造器位置的自动注入 1. 注解定义 @Autowired的定义信息如下: @Target({ElementType.CONSTRUCTOR, Ele...
    99+
    2024-04-02
  • 使用Spring由构造方法自动装配
    Spring由构造方法自动装配 在Spring中,可以使用“通过构造自动装配”,实际上是按构造函数的参数 类型自动装配。 这意味着,如果一个bean的数据类型与其他bean的构造器参...
    99+
    2024-04-02
  • 详解Spring中Bean的作用域与生命周期
    目录一、Bean的作用域二、Bean的生命周期使用代码演示Bean的生命周期一、Bean的作用域 通过Spring容器创建一个Bean的实例时,不仅可以完成Bean的实例化,还可以使...
    99+
    2024-04-02
  • Spring中Bean的作用域与生命周期详解
    目录一、Bean的作用域1、单实例Bean声明2、多实例Bean声明二、Bean的生命周期1、bean的初始和销毁2、bean的后置处理器总结一、Bean的作用域 首先我们来讲一下有...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作