【Spring -- 07 Aware 接口及 InitializingBean 接口】

Aware 接口

Aware 接口是 Spring 框架中一组回调接口,用于使 Spring 容器内的 bean 获得对容器基础设施的访问权。通过实现这些接口,bean 可以访问 Spring 容器的某些部分,例如 ApplicationContextBeanFactoryResourceLoader 等。以下是一些常用的 Aware 接口及其作用:

1. BeanFactoryAware

通过实现 BeanFactoryAware 接口,bean 可以获取当前容器中的 BeanFactory 实例。BeanFactory 提供了访问容器中所有 bean 的方法。

public class MyBean implements BeanFactoryAware {
    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    public void someMethod() {
        MyOtherBean otherBean = beanFactory.getBean(MyOtherBean.class);
    }
}
2. ApplicationContextAware

通过实现 ApplicationContextAware 接口,bean 可以获取当前的 ApplicationContext 实例,ApplicationContextBeanFactory 的子接口,提供了更强大的功能。

public class MyBean implements ApplicationContextAware {
    private ApplicationContext applicationContext;

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

    public void someMethod() {
        MyOtherBean otherBean = applicationContext.getBean(MyOtherBean.class);
    }
}
3. ResourceLoaderAware

通过实现 ResourceLoaderAware 接口,bean 可以获得 ResourceLoader,用于加载资源文件。

public class MyBean implements ResourceLoaderAware {
    private ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void loadResource() {
        Resource resource = resourceLoader.getResource("classpath:myFile.txt");
    }
}
4. EnvironmentAware

通过实现 EnvironmentAware 接口,bean 可以获取当前的 Environment 实例,Environment 提供了访问环境变量和属性文件的功能。

public class MyBean implements EnvironmentAware {
    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    public void someMethod() {
        String property = environment.getProperty("my.property");
    }
}

InitializingBean 接口

InitializingBean 接口是 Spring 提供的另一个回调接口,用于在 bean 属性设置完毕后执行自定义的初始化逻辑。该接口只有一个方法 afterPropertiesSet(),Spring 容器在设置完所有 bean 属性之后会调用这个方法。

public class MyBean implements InitializingBean {
    private String someProperty;

    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 在此执行初始化逻辑
        if (someProperty == null) {
            throw new IllegalArgumentException("Property 'someProperty' is required");
        }
    }
}
使用 @PostConstruct 注解

除了实现 InitializingBean 接口,还可以使用 JSR-250 的 @PostConstruct 注解来指定初始化方法。Spring 容器会在 bean 属性设置完毕后调用标注了 @PostConstruct 的方法。

public class MyBean {
    private String someProperty;

    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }

    @PostConstruct
    public void init() {
        // 在此执行初始化逻辑
        if (someProperty == null) {
            throw new IllegalArgumentException("Property 'someProperty' is required");
        }
    }
}

多种初始化和销毁

Spring 提供了多种初始化手段,除了 @PostConstruct,@Bean(initMethod) 之外,还可以实现 InitializingBean 接口来进行初始化,如果同一个 bean 用了以上手段声明了 3 个初始化方法,那么它们的执行顺序是

  1. @PostConstruct 标注的初始化方法

  2. InitializingBean 接口的初始化方法

  3. @Bean(initMethod) 指定的初始化方法

与初始化类似,Spring 也提供了多种销毁手段,执行顺序为

  1. @PreDestroy 标注的销毁方法

  2. DisposableBean 接口的销毁方法

  3. @Bean(destroyMethod) 指定的销毁方法

参考资料

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值