Spring Aware标记接口使用案例解析

Aware接口是一个标记接口

XXXAare在Spring中表示对XXX可以感知,通俗点解释就是:如果在某个类里面想要使用Spring的一些东西,就可以通过实现XXXAware接口告诉Spring,Spring看到后就会送过来,而接受的方式是通过实现接口唯一的方法setXXX.比如ApplicationContextAware

ApplicationContextAware使用

编写SpringAware实现ApplicationContext接口

package com.rookie.bigdata.aware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**

 * @author rookie
 * @version 1.0
 * @date 2020/3/21 22:58
  */

public class SpringAware implements ApplicationContextAware {

  private ApplicationContext applicationContext;

  //Spring容器会将ApplicationContext对象传入过来
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    System.out.println("传入的IOC容器applicationContext = [" + applicationContext + "]");
    this.applicationContext = applicationContext;
  }

  public ApplicationContext getApplicationContext() {
    return applicationContext;
  }

}

User实体类

package com.rookie.bigdata.domain;

public class User {
  private String username;
  private String address;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

}

spring-aware.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd">

  <bean id="user" class="com.rookie.bigdata.domain.User">
    <property name="username" value="张三"></property>
    <property name="address" value="北京"></property>
  </bean>

  <bean id="springAware" class="com.rookie.bigdata.aware.SpringAware"></bean>

</beans>

测试类

	 @Test
  public void test1(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/aware/spring-aware.xml");

//    User user = (User) applicationContext.getBean("user");
//    System.out.println(user);

    SpringAware springAware = (SpringAware) applicationContext.getBean("springAware");
    ApplicationContext applicationContext1 = springAware.getApplicationContext();
    User user = (User) applicationContext1.getBean("user");
    System.out.println(user);
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Spring实现Aware接口自定义获取bean的两种方式

    在使用spring编程时,常常会遇到想根据bean的名称来获取相应的bean对象,这时候,就可以通过实现BeanFactoryAware来满足需求,代码很简单: @Servicepublic class BeanFactoryHelper implements BeanFactoryAware { private static BeanFactory beanFactory; @Override public void setBeanFactory(BeanFactory beanFactory

  • Spring ProtocolResolver策略接口示例

    ProtocolResolver是一个策略接口,可以用于自定义协议解析, 比如spring就有一个 "classpath:"开头的特定协议(但是spring并不是自定义ProtocolResolver 实现来完成这个功能的) @FunctionalInterface public interface ProtocolResolver { @Nullable Resource resolve(String location, ResourceLoader resourceLoader);

  • Spring中@Import的各种用法以及ImportAware接口详解

    @Import 注解 @Import注解提供了和XML中<import/>元素等价的功能,实现导入的一个或多个配置类.@Import即可以在类上使用,也可以作为元注解使用. @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Import { /** * {@link Configuration}, {@link ImportSelector}, {@link I

  • SpringBoot实现文件上传接口

    摘要 公司都是采用SpringBoot作为项目框架,其实SpringBoot和SSM框架很接近,基本上只是将SSM的一些配置项修改为自动配置或者简单的注解配置就可以了,建议不了解的SpringBoot的朋友们可以了解一下,上手很快,其实文件上传框架根本没有多大关系.我只是顺便帮SpringBoot打个广告罢了. 正题 需求:需要实现一个文件上传的web接口. 1.先实现一个Controller接口,如下: package com.lanxuewei.utils.aspect; import co

  • Spring Validator接口校验与全局异常处理器

    Spring Validator接口校验 上一篇日志使用Bean Validation校验机制,对基本数据类型进行校验,方法是在实体类属性上使用注解标识校验方式,最后在Controller类中具体方法的形参里添加@Vlidated注解.Bean Validation校验有一个缺点是,我们的数据校验是在Java实体类里进行约束的,如果我们有多个处理器方法需要用到同一个实体类,那么定义在实体类属性上的校验规则就不好划分了,有的处理器只需要校验一个属性,而有的处理器需要校验多个属性,我们不可能为每一个

  • SpringBoot下如何实现支付宝接口的使用

    这篇文章主要介绍了SpringBoot下如何实现支付宝接口的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 前期准备: 参考之前写过的 支付宝接口引入servlet版本 Jar包引入: <!-- 支付宝 --> <dependency> <groupId>net.guerlab</groupId> <artifactId>sdk-alipay-starter</artifactId&g

  • Springboot单体架构http请求转换https请求来支持微信小程序调用接口

    http请求转换https请求 1.话不多说,直接上代码! application.properties配置文件 #(密钥文件路径,也可以配置绝对路径) server.ssl.key-store= classpath:证书文件名.pfx #(密钥生成时输入的密钥库口令) server.ssl.key-store-password:123456 #(密钥类型,与密钥生成命令一致) server.ssl.key-store-type:PKCS12 证书一般最好放在resources目录下 接下来配置

  • Spring Boot配置接口WebMvcConfigurer的实现

    WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制.基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口,WebMvcConfigurerAdapter 抽象类是对WebMvcConfigurer接口的简单抽象(增加了一些默认实现),但在在SpringBoot2.0及Spring5.0中WebMvcConfigurerAdapt

  • Spring Aware标记接口使用案例解析

    Aware接口是一个标记接口 XXXAare在Spring中表示对XXX可以感知,通俗点解释就是:如果在某个类里面想要使用Spring的一些东西,就可以通过实现XXXAware接口告诉Spring,Spring看到后就会送过来,而接受的方式是通过实现接口唯一的方法setXXX.比如ApplicationContextAware ApplicationContextAware使用 编写SpringAware实现ApplicationContext接口 package com.rookie.bigd

  • Spring Aware源码设计示例解析

    目录 1. Aware介绍 2. Aware类别 2.1 BeanClassLoaderAware 2.2 BeanFactoryAware 2.3 BeanNameAware 2.4 ApplicationContextAware 3. Aware的使用 4. Aware的作用 5. Aware的调用 1. Aware介绍 前一篇讲到了BeanPostProcessor的相关知识,我们知道BeanPostProcessor是对整个容器中的Bean做前置和后置增强处理.这样的实现方式限制了开发者

  • Spring Boot整合Elasticsearch实现全文搜索引擎案例解析

    简单说,ElasticSearch(简称 ES)是搜索引擎,是结构化数据的分布式搜索引擎.Elastic Search是一个开源的,分布式,实时搜索和分析引擎.Spring Boot为Elasticsearch及Spring Data Elasticsearch提供的基于它的抽象提供了基本的配置.Spring Boot提供了一个用于聚集依赖的spring-boot-starter-data-elasticsearch 'StarterPOM'. 引入spring-boot-starter-dat

  • Spring Aware接口示例代码详解

    若 Spring 检测到 bean 实现了 Aware 接口,则会为其注入相应的依赖.所以通过让bean 实现 Aware 接口,则能在 bean 中获得相应的 Spring 容器资源. Spring 中提供的 Aware 接口有: BeanNameAware:注入当前 bean 对应 beanName BeanClassLoaderAware:注入加载当前 bean 的 ClassLoader BeanFactoryAware:注入 当前BeanFactory容器 的引用 BeanNameAw

  • JavaScript fetch接口案例解析

    在 AJAX 时代,进行 API 等网络请求都是通过 XMLHttpRequest 或者封装后的框架进行网络请求. 现在产生的 fetch 框架简直就是为了提供更加强大.高效的网络请求而生,虽然在目前会有一点浏览器兼容的问题,但是当我们进行 Hybrid App 开发的时候,如我之前介绍的 Ionic 和 React Native,都可以使用 fetch 进行完美的网络请求. 如果看网上的fetch教程,会首先对比XMLHttpRequest和fetch的优劣,然后引出一堆看了很快会忘记的内容(

  • 浅析对Spring aware接口理解

    目录 1. aware接口的作用 2. 常用aware接口及作用 3. 使用样例:ApplicationContextAware 在Bean中获取上下文 4. 自定义aware的方式 4.1 定义继承Aware的接口 4.2 注册实现BeanPostProcessor接口的Bean 4.3 实现TimeAware接口,并测试 5. 源码处理方式 5.1 初始化阶段的源码逻辑 5.2 实现前三个aware接口的处理 5.3 剩余实现aware接口的Bean的处理 1. aware接口的作用 通过a

  • Springboot 整合 Dubbo/ZooKeeper 实现 SOA 案例解析

    一.为啥整合 Dubbo 实现 SOA Dubbo 不单单只是高性能的 RPC 调用框架,更是 SOA 服务治理的一种方案. 核心: 远程通信,向本地调用一样调用远程方法. 集群容错 服务自动发现和注册,可平滑添加或者删除服务提供者. 我们常常使用 Springboot 暴露 HTTP 服务,并走 JSON 模式.但慢慢量大了,一种 SOA 的治理方案.这样可以暴露出 Dubbo 服务接口,提供给 Dubbo 消费者进行 RPC 调用.下面我们详解下如何集成 Dubbo. 二.运行 spring

  • spring cloud Ribbon用法及原理解析

    这篇文章主要介绍了spring cloud Ribbon用法及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 简介 这篇文章主要介绍一下ribbon在程序中的基本使用,在这里是单独拿出来写用例测试的,实际生产一般是配置feign一起使用,更加方便开发.同时这里也通过源码来简单分析一下ribbon的基本实现原理. 基本使用 这里使用基于zookeeper注册中心+ribbon的方式实现一个简单的客户端负载均衡案例. 服务提供方 首先是一个

  • spring获取bean的源码解析

    介绍 前面一章说了AbstractApplicationContext中的refresh方法中的invokeBeanFactoryPostProcessors.主要是调用BeanFactoryPostProcessor.其中也有获取bean的过程,就是beanFactory.getBean的方法.这一章就说下getBean这个方法.由于spring中获取bean的方法比较复杂,涉及到的流程也非常多,这一章就先说下整个大体的流程.其中的细节会在后面也会慢慢说. 源码 直接看源码吧 @Overrid

  • Java之Spring注解配置bean实例代码解析

    前面几篇均是使用xml配置bean,如果有上百个bean,这是不可想象的.故而,请使用注解配置bean !!! [1]注解类别 @Component : 基本注解, 标识了一个受 Spring(点击这里可以下载<Spring应用开发完全手册>) 管理的组件 @Repository : 标识持久层组件 @Service : 标识服务层(业务层)组件 @Controller : 标识表现层组件 Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件. 对于扫描到的组

随机推荐