Spring’s dynamically registered beans

When do you need to actively register beans with the Spring container?

For example, I have made a basic platform to support table scanning, users only need to add basic configuration + Groovy tasks, and then throw the platform to run. The basic platform is always running, so the most straightforward need to register the DataSource bean is the DataSource bean. So how do you play it?

I. Actively register Bean support

Implementing the bean definition with a BeanDefinition is, in the end, a few lines of code

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);
}
Copy the code

Test the following

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;

@Component
public class BeanHolder {
    private final ConfigurableApplicationContext applicationContext;

    public BeanHolder(ConfigurableApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        initSer();
    }

    public void initSer(a) {
        InrSer ser = registerBean("test", InrSer.class);
        ser.name = "A grey";
        ser.uid = 22;
        System.out.println(ser);

        InrSer ser2 = registerBean("test2", InrSer.class, "A Gray Blog".20);
        System.out.println(ser2);
    }

    @ToString
    public static class InrSer {
        private String name;
        private Integer uid;

        @Autowired
        private VisitService visitService;

        public InrSer(a) {}public InrSer(String name, Integer uid) {
            this.name = name;
            this.uid = uid; }}private <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);
        returnapplicationContext.getBean(name, clazz); }}Copy the code

The output is as follows

II. The other

1. A gray Blog: https://liuyueyi.github.io/hexblog.

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

2. Statement

As far as the letter is not as good as, has been the content, purely one’s own words, because of the limited personal ability, it is hard to avoid omissions and mistakes, such as finding bugs or better suggestions, welcome criticism and correction, not grudging gratitude

  • Micro Blog address: Small Gray Blog
  • QQ: a gray /3302797840

3. Scan attention

Small grey Blog& public number

Knowledge of the planet