Bean注冊(cè)到Spring容器中

一.Spring如何管理bean

1.@Resource 默認(rèn)根據(jù)name匹配,未找到再根據(jù)type匹配;

2.@Autowired 根據(jù)類型匹配;

3.無實(shí)現(xiàn)類,根據(jù)接口生成代理類,把代理類放到FactoryBean的實(shí)現(xiàn)中,最后把FactoryBean的實(shí)現(xiàn)類注冊(cè)到Spring容器中,本篇具體講第三種方式。

二.核心步驟方法


public interface ITestDao {

    String test();

}


public class ProxyBeanFactory implements FactoryBean {

    /**

    * 返回實(shí)例對(duì)象

    * @return

    * @throws Exception

    */

    @Override

    public Object getObject() throws Exception {

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        Class[] classes = {ITestDao.class};

        InvocationHandler handler = (proxy, method, args) -> "你被代理了 " + method.getName();

        return Proxy.newProxyInstance(classLoader, classes, handler);

    }

    /**

    * 返回實(shí)例類型

    * @return

    */

    @Override

    public Class<?> getObjectType() {

        return ITestDao.class;

    }

    /**

    * 是否單例

    * @return

    */

    @Override

    public boolean isSingleton() {

        return true;

    }

}


@Component

public class RegisterBeanFactory implements BeanDefinitionRegistryPostProcessor

{

    /**

    * 實(shí)現(xiàn) BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry方法,獲取 Bean 注冊(cè)對(duì)象。

    * 定義 Bean,GenericBeanDefinition,這里主要設(shè)置了我們的代理類工廠。

    * 創(chuàng)建 Bean 定義處理類,BeanDefinitionHolder,這里需要的主要參數(shù);定義 Bean 和名稱 setBeanClass(ProxyBeanFactory.class)。

    * 將 bean注冊(cè)到spring容器中去,registry.registerBeanDefinition()

    * @param beanDefinitionRegistry

    * @throws BeansException

    */

    @Override

    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {

        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();

        beanDefinition.setBeanClass(ProxyBeanFactory.class);

        BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(beanDefinition, "testDao");

        System.out.println("注冊(cè) testDao" + definitionHolder);

        BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, beanDefinitionRegistry);

    }

    @Override

    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

}


@Test

    public void test() {

        AnnotationConfigApplicationContext application = new AnnotationConfigApplicationContext(RegisterBeanFactory.class);

        ITestDao testDao = (ITestDao) application.getBean("testDao");

        String result = testDao.test();

        System.out.println(result);

    }

三.測(cè)試結(jié)果

image
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容