Spring温故而知新- bean的装配

2024-03-13 02:38

本文主要是介绍Spring温故而知新- bean的装配,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址:https://blog.csdn.net/kkfd1002/article/details/80220481

按条件装配bean

就是当满足特定的条件时Spring容器才创建Bean,Spring中通过@Conditional注解来实现条件化配置bean

复制代码
package com.sl.ioc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;@Configuration
public class AnimalConfig {@Bean("dog")@Conditional(DogCondition.class)public Dog DogInstance() {return new Dog();}@Bean("cat")@Conditional(CatCondition.class)public Cat CatInstance() {return new Cat();}}
复制代码

@Conditional和 :Condition接口的实现

复制代码
public @interface Conditional {/*** All {@link Condition}s that must {@linkplain Condition#matches match}* in order for the component to be registered.*/Class<? extends Condition>[] value();}
public interface Condition {/*** Determine if the condition matches.* @param context the condition context* @param metadata metadata of the {@link org.springframework.core.type.AnnotationMetadata class}* or {@link org.springframework.core.type.MethodMetadata method} being checked* @return {@code true} if the condition matches and the component can be registered,* or {@code false} to veto the annotated component's registration*/boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);}
复制代码

Conditional注解通过value传入一个类,实现Condition接口,通过实现Condition接口中matches方法决定是否需要装配Bean,如果满足条件需要创建bean则返回true,否则返回false

自己定义两个继承Condition接口的类:通过ConditionContext查找当前环境中是否存在dog或者cat属性,如果存在,则创建对应的bean对象,具体实现如下:

复制代码
package com.sl.ioc;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;public class DogCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {       Environment environment = context.getEnvironment();        boolean flag= environment.containsProperty("dog");        return flag;}
}
复制代码

 

复制代码
package com.sl.ioc;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class CatCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {Environment environment = context.getEnvironment();boolean flag= environment.containsProperty("cat");return flag;}
}
复制代码
复制代码
package com.sl.ioc;
import org.springframework.stereotype.Component;
@Component
public interface Animal {void Say();}package com.sl.ioc;import org.springframework.stereotype.Component;@Component
public class Cat implements Animal {@Overridepublic void Say() {System.out.println("I am a cat");}
}package com.sl.ioc;
import org.springframework.stereotype.Component;@Component
public class Dat implements Animal {@Overridepublic void Say() {System.out.println("I am a dog");}
}
复制代码

测试代码:

复制代码
public class TestClass {@Testpublic void TestGetDoInstance() {System.setProperty("dog","");    ApplicationContext context = new AnnotationConfigApplicationContext(AnimalConfig.class);String[] beanNames = context.getBeanDefinitionNames();for(String bean : beanNames) {System.out.println(bean);}}
}
复制代码

运行测试可以看到输出的beanname中会包含dog的bean:

自动装配的歧义处理

Spring自动装配时如果存在多个bean能够匹配的话,那么这种情况会阻碍Spring通过属性、构造函数或方法进行装配。针对这种情况,Spring提供了多种 可选方案来解决这个问题,可以选择一个bean作为首选的bean,或者使用限定符来确定唯一bean

1:使用首选Bean

Spring提供@Primary注解来设置首选Bean,当初选自动装配歧义时,会选择装配带有@Primary的bean

沿用上面的示例代码,尝试装载animal

复制代码
@Component
public class AnimalInstance {@Autowiredpublic Animal animal;}
复制代码

当Spring尝试注入animal实例时,由于Dog和Cat都继承自Animal,所以此处产生了歧义,下面通过使用@Primary指定首选bean

复制代码
@Component
@Primary   //指定首选bean
public class Cat implements Animal {@Overridepublic void Say() {System.out.println("I am a cat");}
}
复制代码

同样也可以使用XML配置来实现:<bean>元素提供了primary属性来设置首选bean

<bean id="cat"  class="com.sl.ioc.Cat" primary ="true" >

测试代码:

复制代码
public class TestClass {@Testpublic void TestGetDoInstance() {//应用上下文
        ApplicationContext context = new AnnotationConfigApplicationContext(AnimalConfig.class);AnimalInstance animalInstance = context.getBean(AnimalInstance.class);animalInstance.animal.Say();}
}
复制代码

运行结果:

首选项只是标识一个优先选择装载的bean,如果配置了多个@Primary,那么将带来新的歧义,Spring依然无法完成自动装配,可以通过下面限定符来解决这个问题

2:使用限定符

Spring提供@Qualifier注解来指定想要注入的具体bean。例如上面的示例,如果指定注入dog:

复制代码
package com.sl.ioc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;@Component
public class AnimalInstance {@Autowired@Qualifier("dog")public Animal animal;}
复制代码

解释一下:@Qualifier("dog")表示指定的bean具有”dog”限定符,spring中bean如果没有指定限定符,会使用默认限定符,即使用beanID作为限定符。所以上面是恰好使用了dog bean的ID作为了限定符。也可以写成如下方式:

复制代码
@Component
@Qualifier("specialdog")    //为bean指定限定符
public class Dog implements Animal
{@Overridepublic void Say() {System.out.println("I am a dog");}    
}
复制代码
复制代码
@Component
public class AnimalInstance {@Autowired@Qualifier("specialdog")    //使用上面定义的限定符public Animal animal; 
}
复制代码

Bean的作用域

Spring容器在创建bean实例的同时,还允许指定bean实例的作用域,常见作用域有一下几种:

1:单例作用域(Singleton)

2:原型作用域(Prototype)

3:会话作用域(Session)

4:请求作用域(Request)

5:全局会话作用域(globalSession)

Singleton作用域

在整个应用中,Spring IOC容器为使用singleton模式的bean只创建一个实例,Spring将会缓存Bean实例,任何对该类型beand请求都会返回该实例。单例也是Spring默认的作用域。具体使用如下,通过XML配置

<bean id="beanid"  class="com.sl.ioc.xxx" scope="singleton" ></bean>

<bean>元素提供了scope属性来设置singleton作用域

对应的注解:

@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)

Prototype原型作用域

每次注入或者从Spring容器中获取时都创建一个新的bean实例:

<bean id="beanid"  class="com.sl.ioc.xxx" scope="prototype" ></bean>

<bean>元素提供了scope属性来设置singleton作用域

对应的注解:

@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Session会话作用域

在web应用中,针对每个会话,Spring容器根据bean定义创建的bean实例,只在当前会话Session中有效,XML配置如下:

<bean id="beanid"  class="com.sl.ioc.xxx" scope="session" ></bean>

针对某个HTTP Session,Spring容器会根据bean定义创建一个新的bean实例,该bean仅在当前HTTP Session内有效。所以可以根据需要放心的更改bean实例的内部状态,而不影响其他Http Session中bean实例。当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被销毁掉。

Request 请求作用域

在web应用中,针对每次请求,Spring容器根据bean定义创建新的bean实例,只在当前请求内有效

<bean id="beanid"  class="com.sl.ioc.xxx" scope="request" ></bean>

 

该bean实例只在当前请求内有效,在请求处理完成之后bean也会被销毁掉

globalSession全局会话作用域

类似于session作用域,只是其用于portlet环境的web应用。如果在非portlet环境将视为session作用域。

<bean id="beanid"  class="com.sl.ioc.xxx" scope="globalSession" ></bean>

 




这篇关于Spring温故而知新- bean的装配的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/803400

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项