180804-spring's Dynamic Register bean

Source: Internet
Author: User

Spring's dynamic Register bean

What scenario does it take to proactively register beans with the spring container?

As I have done before a base platform to support the scan table, the user only need to add the basic configuration + Groovy task, you can throw to this platform to run, and this basic platform is always running, so in the new task, the most intuitive to register is the DataSource data source this bean, So how can we play?

I. Actively registering bean support

With the help BeanDefinition of implementing the bean definition, from the final use, the code is relatively small, a few lines

public <T> T registerBean(String name, Class<T> clazz, Object... args) {      BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);      if (args.length > 0) {          for (Object arg : args) {              beanDefinitionBuilder.addConstructorArgValue(arg);          }      }      BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();        BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) applicationContext.getBeanFactory();      beanFactory.registerBeanDefinition(name, beanDefinition);      return applicationContext.getBean(name, clazz);}

Test as follows

Import Com.github.hui.story.quickstory.server.visitservice;import Lombok. Tostring;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.config.beandefinition;import Org.springframework.beans.factory.support.beandefinitionbuilder;import Org.springframework.beans.factory.support.beandefinitionregistry;import Org.springframework.context.configurableapplicationcontext;import org.springframework.stereotype.component;@    Componentpublic class Beanholder {private final configurableapplicationcontext applicationcontext; Public Beanholder (Configurableapplicationcontext applicationcontext) {this.applicationcontext = ApplicationContext        ;    Initser ();        } public void Initser () {Inrser ser = Registerbean ("Test", Inrser.class);        Ser.name = "one ash";        Ser.uid = 22;        SYSTEM.OUT.PRINTLN (Ser);        Inrser ser2 = Registerbean ("Test2", Inrser.class, "a gray-grey blog", 20);    System.out.println (SER2); } @ToStrinG public static class Inrser {private String name;        Private Integer uid;        @Autowired private Visitservice Visitservice;            Public Inrser () {} public Inrser (String name, Integer uid) {this.name = name;        This.uid = UID; }} private <T> T Registerbean (String name, class<t> clazz, Object ... args) {Beandefinitionbuild        Er beandefinitionbuilder = beandefinitionbuilder.genericbeandefinition (clazz); if (Args.length > 0) {for (Object Arg:args) {Beandefinitionbuilder.addconstructorargvalue            (ARG);        }} beandefinition Beandefinition = Beandefinitionbuilder.getrawbeandefinition ();        Beandefinitionregistry beanfactory = (beandefinitionregistry) applicationcontext.getbeanfactory ();        Beanfactory.registerbeandefinition (name, beandefinition);    Return Applicationcontext.getbean (name, clazz); }}

The output is as follows

Ii. other 1. A grey and grey blog:https://liuyueyi.github.io/hexblog

A gray and gray personal blog, recording all the study and work in the blog, welcome everyone to visit

2. Disclaimer

The letter is not as good as, has been on the content, purely opinion, because of limited personal ability, inevitably there are omissions and errors, such as the detection of bugs or better suggestions, welcome criticism, please feel grateful

    • Weibo address: small Gray Gray Blog
    • QQ: A grey/grey/3302797840
3. Scan for attention

Small grey ash blog& public number

Knowledge Planet

180804-spring's Dynamic Register bean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.