In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to dynamically register Bean in Spring. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
It is very simple to dynamically register Bean to the Spring container, we only need to inherit BeanDefinitionRegistryPostProcessor
@ Componentpublic 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);}}
You can also overload postProcessBeanFactory to do the same thing, registering User user to the Spring container.
@ 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 and others use this technology to realize the dynamic registration of Bean.
The Mapper we define in MyBatis is essentially a common interface to Java, so how is it managed by the Spring container?
The principle is that subclasses of Mapper interface are dynamically generated by CGLib or JDK dynamic proxy, and objects are instantiated through Spring's dynamic registration mechanism.
BeanFactory.registerSingleton manages the created objects in a Spring container.
@ Mapperpublic interface UserMapper {@ 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 findList (Class clazz, Query query);} @ Componentpublic 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 create (final Class 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)) {/ / return the result} else if (m.isAnnotationPresent (UpdateProvider.class)) {by executing SQL through JDBC } else if (m.isAnnotationPresent (InsertProvider.class)) {} else if (m.isAnnotationPresent (DeleteProvider.class)) {}} System.out.println ("method=" + method.getName ()) Return 0;}});}} about how to dynamically register Bean in Spring to share here, hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.