Spring底层组件xxxAware家族

搞懂xxxAware家族对理解Spring源码和提高代码能力也有帮助。

Spring中常见xxxAware接口列举如下:

  • ApplicationContextAware
  • BeanNameAware
  • EmbeddedValueResolverAware
  • EnvironmentAware
  • MessageSourceAware
  • ResourceLoaderAware
1、ApplicationContextAware

ApplicationContextAware的作用就是通过它,spring可以把上下文环境对象ApplicationContext注入进来,然后通过ApplicationContext对象来得到Spring容器中的bean,做一些应用级别的事情。

简而言之:ApplicationContextAware是用来获取ApplicationContext对象的。


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 类说明:演示使用 ApplicationContextAware 获取配置文件的属性
 */
@PropertySource(value = "classpath:/test.properties")
@Component
public class Plane implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public Plane() {
        System.out.println("Plane.......constructor");
    }

    @PostConstruct
    public void init() {
        System.out.println("Plane.......init........@PostConstruct");
        Environment environment = applicationContext.getEnvironment();
        String property = environment.getProperty("bird.color");
        System.out.println("Plane...... bird.color=" + property);
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Plane......destroy........@PreDestroy");
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        // 容器初始化,将applicationContext传进来,那么其他方法就可使用到IOC容器了
        // 这个方法是ApplicationContextAwareProcessor的
        this.applicationContext = context;
    }
}

写测试类 验证一下:

package org.xiaobuisme.example;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.xiaobuisme.example.cap8.Plane;

public class Cap8Test {
    public static void main(String[] args) {
        // 验证 实现 ApplicationContextAware的接口后,如何使用 applicationContext获取配置文件变量
        AnnotationConfigApplicationContext anno = new AnnotationConfigApplicationContext(Plane.class);
        // 打印出 Plane...... bird.color=blue
        // 说明 此种方式 可以使用applicationContext进行操作
        anno.close();
    }
}

输出结果:

Plane.......constructor
Plane.......init........@PostConstruct
Plane...... bird.color=blue
九月 08, 2023 11:12:55 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5fa7e7ff: startup date [Fri Sep 08 11:12:55 CST 2023]; root of context hierarchy
Plane......destroy........@PreDestroy
2、BeanNameAware

用来获取当前bean的名称的

3、EmbeddedValueResolverAware 

用来解析表达式的

4、EnvironmentAware

用来获取环境变量的

5、MessageSourceAware

用来做国际化的

6、ResourceLoaderAware

用来加载资源文件的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个全家桶式的技术栈,包含了很多组件。其中,Eureka、Ribbon、Feign、Hystrix和Zuul是其最核心的几个组件。 Eureka是Spring Cloud中的服务注册和发现组件,它通过维护一个服务注册表来管理微服务的注册和发现。每个微服务都会向Eureka服务器注册自己的信息,其他微服务可以通过向Eureka服务器查询来发现并调用其他微服务。 Ribbon是一个客户端负载均衡器,它可以根据一些特定的负载均衡算法来分发请求到多个服务实例中。Ribbon可以与Eureka集成,从而能够动态地获取微服务的实例列表,并根据负载均衡策略来选择目标服务实例。 Feign是一个声明式的REST客户端,它可以帮助我们快速、简单地编写HTTP请求的客户端代码。Feign可以与Ribbon集成,从而能够自动地实现负载均衡和服务发现。 Hystrix是一个容错和延迟容忍性库,它可以帮助我们控制和防止分布式系统中的雪崩效应。Hystrix通过使用断路器模式来实现容错保护,当某个服务出现故障时,Hystrix可以快速地断开与该服务的连接,避免影响其他服务的正常运行。 Zuul是一个边缘服务网关,它可以帮助我们实现动态路由、负载均衡、安全认证和监控等功能。Zuul可以将所有的请求转发给后端的微服务,并提供一些过滤器来处理请求和响应。 综上所述,Spring Cloud的核心组件通过服务注册和发现、负载均衡、声明式REST客户端、容错保护和边缘服务网关等方式来支持微服务架构的开发和运行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Spring Cloud核心组件底层原理](https://blog.csdn.net/aichuanwendang/article/details/84983661)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [SpringCloud底层原理](https://download.csdn.net/download/weixin_38693192/14941427)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值