【spring】Aware接口学习

Aware接口介绍

在Spring框架中,Aware接口是一种特殊类型的接口,它们用于在Spring容器创建bean时,自动注入某些依赖。这些接口允许bean知道Spring容器的某些特定上下文信息。例如,BeanNameAware接口允许bean知道它在Spring容器中的名

接口路径:org.springframework.beans.factory.Aware

常见的Aware接口

以下是一些常见的Aware接口及其作用:

ApplicationContextAware
  • 作用: 实现该接口的Bean可以获取到ApplicationContext对象,从而可以访问Spring容器中的其他Bean。
  • 方法void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
BeanFactoryAware
  • 作用: 实现该接口的Bean可以获取到BeanFactory对象,从而可以访问Spring容器中的其他Bean。
  • 方法void setBeanFactory(BeanFactory beanFactory) throws BeansException;
BeanNameAware
  • 作用: 实现该接口的Bean可以获取到自己在Spring容器中的Bean名称。
  • 方法void setBeanName(String name);
ResourceLoaderAware
  • 作用: 实现该接口的Bean可以获取到ResourceLoader对象,从而可以加载资源文件。
  • 方法void setResourceLoader(ResourceLoader resourceLoader);
MessageSourceAware
  • 作用: 实现该接口的Bean可以获取到MessageSource对象,从而可以解析国际化消息。
  • 方法void setMessageSource(MessageSource messageSource);
ApplicationEventPublisherAware
  • 作用: 实现该接口的Bean可以获取到ApplicationEventPublisher对象,从而可以发布Spring事件。
  • 方法void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);
EnvironmentAware
  • 作用: 实现该接口的Bean可以获取到Environment对象,从而可以访问Spring的环境配置。
  • 方法void setEnvironment(Environment environment);
ServletContextAware
  • 作用: 实现该接口的Bean可以获取到ServletContext对象,从而可以访问Servlet容器的上下文。
  • 方法void setServletContext(ServletContext servletContext);

代码示例

demoBean类实现了ApplicationContextAware接口,并通过setApplicationContext方法获取到了ApplicationContext对象。这样,demoBean就可以在运行时访问Spring容器中的TestBean (其他Bean)。

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

@Component
public class DemoBean implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void doSomething() {
        // 使用applicationContext获取TestBean 
        TestBean testrBean = applicationContext.getBean(TestBean .class);
        anotherBean.doSomethingElse();
    }
}

import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

@Component
public class DemoServletContextAwareBean implements ServletContextAware {

    private ServletContext servletContext;

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public void doSomethingWithServletContext() {
     
        String contextPath = servletContext.getContextPath();
        System.out.println("Web应用程序上下文路径 : " + contextPath);

       
        String initParam = servletContext.getInitParameter("myInitParam");
        System.out.println("初始化参数: " + initParam);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值