温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

实现Aware接口的原理及使用方法是什么

发布时间:2023-04-28 11:38:20 来源:亿速云 阅读:75 作者:iii 栏目:开发技术

本篇内容主要讲解“实现Aware接口的原理及使用方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“实现Aware接口的原理及使用方法是什么”吧!

前言

spring 对bean的创建过程做了很完整的封装。但是提供了非常多的扩展接口,供我们使用。这一节主要是实现spring提供的获取 beanFactory,classLoader 等属性的能力。 在我们开发过程中我们经常会使用到 ApplicationContextAware接口,来获取到 spring的上下文。来完成对bean的获取,当拿到了BeanFactory以后,我们能做的东西就多起来了,我们可以通过的spring工厂获取到我们需要的类,等等。

实现Aware接口的原理及使用方法是什么

设计&实现

spring 提供Aware接口机制,给外部的类提供获取spring内部信息的能力。目前spring常用的Aware接口有

实现Aware接口的原理及使用方法是什么

Aware 感知接口

Aware接口,只做标记。类似于Serializable序列化接口,仅标记这个类可以序列化。Aware 仅表示实现类具有在获取springbean创建过程中的一些内部属性的能力。

/**
 * 只做标记
 * spring容器感知接口
 */
public interface Aware {
}

提供具体能力的接口

ApplicationContextAware 提供获取 applicationContext 的能力

public interface ApplicationContextAware extends Aware {
    void setApplicationContext(ApplicationContext applicationContext);
}

BeanClassLoaderAware提供获取 classLoader 的能力

public interface BeanClassLoaderAware extends Aware{
    void setBeanClassLoader(ClassLoader classLoader);
}

BeanFactoryAware 提供获取 BeanFactory 的能力

public interface BeanFactoryAware extends Aware{
    void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}

BeanNameAware 提供获取 beanName 的能力

public interface BeanNameAware extends Aware{
    void setBeanName(String beanName);
}

他们都在创建bean完成后,在添加bean的扩展属性时,给这个bean加上特定的能力

@Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) {
        Object bean = null;
        try {
            bean = createBeanInstance(beanDefinition, beanName, args);
            // 注入属性
            applyPropertyValues(beanName, bean, beanDefinition);
            // 提供给外部的扩展包装,执行 Bean 的初始化方法和 BeanPostProcessor 的前置和后置处理方法
            bean = initializeBean(beanName, bean, beanDefinition);
        } catch (Exception e) {
            throw new RuntimeException("bean create error!", e);
        }
        // 注册实现了 DisposableBean 接口的 Bean 对象
        registerDisposableBeanIfNecessary(beanName, bean, beanDefinition);
        registerSingleton(beanName, bean);
        return bean;
    }
private Object initializeBean(String beanName, Object bean, BeanDefinition beanDefinition) throws BeansException {
        if (bean instanceof Aware) {
            if (bean instanceof BeanFactoryAware) {
                ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
            }
            if (bean instanceof ApplicationContextAware) {
                ((ApplicationContextAware) bean).setApplicationContext(this);
            }
            if (bean instanceof BeanClassLoaderAware) {
                ((BeanClassLoaderAware) bean).setBeanClassLoader(getClassLoader());
            }
            if (bean instanceof BeanNameAware) {
                ((BeanNameAware) bean).setBeanName(beanName);
            }
        }
		.....
	}

测试

实现 需要添加特定能力的 Aware接口,实现他们的方法

public class UserService implements InitializingBean, DisposableBean, ApplicationContextAware, BeanClassLoaderAware, BeanNameAware {
    private ApplicationContext applicationContext;
    private ClassLoader classLoader;
    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
}
@Test
    public void testContext1() throws BeansException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring.xml");
        applicationContext.registerShutdownHook();
        UserService userService = (UserService) applicationContext.getBean("userService");
        System.out.println(userService.say());
        System.out.println(userService.getApplicationContext());
        System.out.println(userService.getClassLoader());
        System.out.println(userService.getBeanName());
    }

实现Aware接口的原理及使用方法是什么

到此,相信大家对“实现Aware接口的原理及使用方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI