博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Aware 接口
阅读量:3737 次
发布时间:2019-05-22

本文共 2450 字,大约阅读时间需要 8 分钟。

有些时候,在 Bean 的初始化中,需要使用 Spring 框架自身的对象来执行一些操作,比如获取 ServletContext 的一些参数,获取 ApplicaitionContext 中的 BeanDefinition 的名字,获取 Bean 在容器中的名字等等。为了让 Bean 可以获取到框架自身的一些对象,Spring 提供了一组以 Aware 为结尾的接口。

这些接口均继承于 org.springframework.beans.factory.Aware 标记接口,并提供了由 Bean 实现的 set 方法,Spring 通过基于 setter 的依赖注入方式,使相应的对象可以被 Bean 使用。以下是一些重要的 Aware 接口:

  • ApplicationContextAware:获得 ApplicationContext 对象,可以用来获取所有 Bean definition 的名字;
  • BeanFactoryAware:获得 BeanFactory 对象,可以用来检测 Bean 的作用域;
  • BeanNameAware:获得 Bean 在配置文件中定义的名字;
  • ResourceLoaderAware:获得 ResourceLoader 对象,可以获得 classpath 中的某个文件;
  • ServletContextAware:在 MVC 应用中,可以获取 ServletContext 对象,可以读取 Context 中的参数;
  • ServletConfigAware: 在 MVC 应用中,可以获取 ServletConfig 对象,可以读取 Config 中的参数。
public class TestService implements   ApplicationContextAware,        ApplicationEventPublisherAware, BeanClassLoaderAware, BeanFactoryAware,        BeanNameAware, EnvironmentAware, ImportAware, ResourceLoaderAware{        @Override    public void setBeanClassLoader(ClassLoader classLoader) {        System.out.println("执行setBeanClassLoader,ClassLoader Name = " + classLoader.getClass().getName());    }    @Override    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {        System.out.println("执行setBeanFactory,setBeanFactory:: giraffe bean singleton=" +  beanFactory.isSingleton("giraffeService"));    }    @Override    public void setBeanName(String s) {        System.out.println("执行setBeanName: Bean Name defined in context="+s);    }    @Override    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {        System.out.println("执行setApplicationContext:: Bean Definition Names="                + Arrays.toString(applicationContext.getBeanDefinitionNames()));    }    @Override    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {        System.out.println("执行setApplicationEventPublisher");    }    @Override    public void setEnvironment(Environment environment) {        System.out.println("执行setEnvironment");    }    @Override    public void setResourceLoader(ResourceLoader resourceLoader) {        Resource resource = resourceLoader.getResource("classpath:spring-beans.xml");        System.out.println("执行setResourceLoader:: Resource File Name="+ resource.getFilename());    }    @Override    public void setImportMetadata(AnnotationMetadata annotationMetadata) {        System.out.println("执行setImportMetadata");    }}

转载地址:http://rnsin.baihongyu.com/

你可能感兴趣的文章
Webpack学习
查看>>
IDEA 整合 Tomcat 服务器
查看>>
IDEA 中动态 web 工程的操作
查看>>
Servlet技术初理解与使用
查看>>
ServletConfig 类
查看>>
ServletContext 类
查看>>
HTTP 协议
查看>>
HttpServletRequest 类
查看>>
HttpServletResponse 类
查看>>
JSP概述
查看>>
vue-router路由
查看>>
Element-ui
查看>>
SpringBoot概述
查看>>
Spring Boot 入门案例
查看>>
SpringBoot运行原理探究
查看>>
.properties 文件,.yml 文件 ,yaml文件语法学习
查看>>
jsp 的三种语法
查看>>
jsp 九大内置对象和四大域对象
查看>>
jsp 的常用标签
查看>>
jsp练习
查看>>