您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Spring中怎么動態注冊Bean,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
動態注冊Bean到Spring容器是很簡單的,我們只要繼承BeanDefinitionRegistryPostProcessor
@Component public class TestDynamicRegistBean implements BeanDefinitionRegistryPostProcessor { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { System.out.println("yzy:TestDynamicRegistBean.postProcessBeanDefinitionRegistry"); GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(User.class); beanDefinition.getPropertyValues().add("name", "yangzhongyu"); registry.registerBeanDefinition("user", beanDefinition); } }
還可以重載postProcessBeanFactory來完成同樣的事情,注冊User user 到Spring容器。
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException{ System.out.println("yzy:TestDynamicRegistBean.postProcessBeanFactory"); GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(User.class); beanDefinition.getPropertyValues().add("age", "yangzhongyu"); ((DefaultListableBeanFactory) beanFactory) .registerBeanDefinition("user", beanDefinition); }
MyBatis,Dubbo等采用了這個技術來實現Bean的動態注冊.
在MyBatis中我們定義的Mapper本質上只是一個Java的普通接口,那么是如何交給到Spring容器管理的呢?
其中的原理就是通過CGLib或者JDK動態代理動態生成了Mapper接口的子類,并且通過Spring的動態注冊機制實例化對象.
beanFactory.registerSingleton把創建好的對象放入Spring容器中管理。
@Mapper public interface UserMapper<T> { @InsertProvider(type = SqlFactory.class, method = "insert") @Options(useGeneratedKeys = true) Boolean insert(T data); @UpdateProvider(type = SqlFactory.class, method = "update") Boolean update(T data); @SelectProvider(type = SqlFactory.class, method = "find") T find(Class clazz, Query query); @SelectProvider(type = SqlFactory.class, method = "find") List<T> findList(Class clazz, Query query); }
@Component public class MapperRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.registerSingleton("userMapper", create(UserMapper.class)); } private <T> T create(final Class<T> interfaceClazz) { return (T) Proxy.newProxyInstance(interfaceClazz.getClassLoader(), new Class<?>[]{interfaceClazz}, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // String respone = (String) method.invoke(proxy, args); boolean isMapper = interfaceClazz.isAnnotationPresent(Mapper.class); if(isMapper){ Method[] methods = interfaceClazz.getDeclaredMethods(); for(Method m : methods){ if(m.isAnnotationPresent(SelectProvider.class)){ //通過JDBC執行SQL把結果返回 }else if(m.isAnnotationPresent(UpdateProvider.class)){ }else if(m.isAnnotationPresent(InsertProvider.class)){ }else if(m.isAnnotationPresent(DeleteProvider.class)){ } } } System.out.println("method="+method.getName()); return 0; } }); } }
關于Spring中怎么動態注冊Bean就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。