Spring核心接口:ObjectProvider接口

30 篇文章 0 订阅
22 篇文章 0 订阅

ObjectProvider 是 Spring Framework 5.0 中引入的一个接口,用于提供对 bean 的延迟访问。它可以用于在需要延迟获取 bean 或在需要对 bean 进行多次访问时,减少 bean 的创建次数和提高应用程序性能。ObjectProvider 接口有两个主要方法:getObject()stream()。其中 getObject() 方法用于获取 bean 的实例,如果 bean 尚未创建,则会创建一个新的实例;stream() 方法用于获取 bean 的流,可以对 bean 进行多次访问。实现了Beanfactory接口

隐式注入是 Spring Framework 中一种注入 bean 的方式,它可以在不使用 @Autowired 注解的情况下,自动将 bean 注入到需要使用的地方。隐式注入可以通过在构造函数、Setter 方法或字段上使用 @Autowired 注解来实现,也可以通过在配置文件中使用 <constructor-arg><property> 元素来实现。在使用隐式注入时,Spring 会自动寻找与要注入的 bean 类型匹配的 bean,并将其注入到需要使用的地方。如果找到多个匹配的 bean,Spring 会抛出一个异常,提示无法确定应该注入哪个 bean。

ObjectProvider 和隐式注入可以结合使用,以实现对 bean 的延迟访问和自动注入。例如,可以在一个 bean 中注入 ObjectProvider,然后在需要使用 AnotherBean 时,调用 ObjectProvider 的 getObject() 方法来获取 AnotherBean 的实例。这样可以实现对 AnotherBean 的延迟访问,同时也可以自动注入 AnotherBean。

以下是一个使用 ObjectProvider 和隐式注入的示例:

@Component
public class MyComponent {

    private final ObjectProvider<AnotherComponent> anotherComponentProvider;

    public MyComponent(ObjectProvider<AnotherComponent> anotherComponentProvider) {
        this.anotherComponentProvider = anotherComponentProvider;
    }

    public void doSomething() {
        AnotherComponent anotherComponent = anotherComponentProvider.getObject();
        // 使用 anotherComponent 做些事情
    }

}

@Component
public class AnotherComponent {

    // ...

}

在这个示例中,MyComponent 通过构造函数注入了 ObjectProvider,然后在 doSomething() 方法中调用 getObject() 方法获取 AnotherComponent 的实例。AnotherComponent 是通过隐式注入的方式注入到 ObjectProvider 中的。这样可以实现对 AnotherComponent 的延迟访问和自动注入。

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
package com.xiangyixiang.www.cloud.business.gateway.configure; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsWebFilter; import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; import org.springframework.web.util.pattern.PathPatternParser; import java.util.stream.Collectors; /**网关统一配置允许跨域 * zj 解决跨域问题 * @author 张静 */ @Configuration public class CorsConfig { @Bean public CorsWebFilter corsFilter() { CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); config.addAllowedMethod("*"); config.addAllowedOrigin("http://39.105.36.172:9250"); config.addAllowedHeader("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); source.registerCorsConfiguration("/**", config); return new CorsWebFilter(source); } @Bean @ConditionalOnMissingBean public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) { return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList())); } }
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

five-five

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值