title: Why does springboot refresh the bean only once when I remove bean and add another bean?
body: <p>I have a requirement to remove the corresponding Bean and then add another Bean, but I find that springboot only refreshes the injected Bean once</p>
  
  <p>the springboot version is 2.1.5.RELEASE,JDK1.8</p>
  
  <pre class="lang-java prettyprint-override"><code>    public class Users {
          String name;
          int age;
          String gender;
  
          public Users() {
          }
          public Users(String name, int age, String gender) {
              this.name = name;
              this.age = age;
              this.gender = gender;
          }
      }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>    @Component
      public class UsersController implements ApplicationRunner {
           @Autowired(required = false)
      //     @Resource
           List&lt;Users&gt; users= Collections.emptyList();
  
  
          @Override
          public void run(ApplicationArguments args) {
              System.out.println(users);
          }
      }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>    @RestController
      public class HelloController implements 
      BeanDefinitionRegistryPostProcessor, ApplicationContextAware {
  
          ApplicationContext applicationContext;
  
          DefaultListableBeanFactory defaultListableBeanFactory;
  
          Users users;
  
          int i=0;
  
          @RequestMapping("/hello1")
          public String index1() {
              return "hello";
          }
          @RequestMapping("/hello3")
          public Users index3() {
              UsersController controller = 
      applicationContext.getBean(UsersController.class);
              controller.run(null);
              return null;
          }
  
          @Override
          public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry 
      registry) throws BeansException {
  
          }
  
          @Override
          public void postProcessBeanFactory(ConfigurableListableBeanFactory 
      beanFactory) throws BeansException {
              defaultListableBeanFactory= (DefaultListableBeanFactory) 
      beanFactory;
              Users users=new Users("hello"+(i++),23,"fe");
              this.users=users;
              defaultListableBeanFactory.registerBeanDefinition(users.getName(),
                  BeanDefinitionBuilder.genericBeanDefinition(Users.class, () -&gt; 
      users).getRawBeanDefinition());
  
          }
          @EventListener
          public void onApplicationEvent(@NonNull EnvironmentChangeEvent 
      environmentChangeEvent) {
              System.out.println("before 
      remove："+applicationContext.getBean(Users.class));
              defaultListableBeanFactory.removeBeanDefinition(users.getName());
              Users users=new Users("hello"+(i++),23,"fe");
              this.users=users;
              defaultListableBeanFactory.registerBeanDefinition(users.getName(),
                      BeanDefinitionBuilder.genericBeanDefinition(Users.class, 
      () -&gt; users).getRawBeanDefinition());
  
          System.out.println("after："+applicationContext.getBean(Users.class));
          }
  
          @Override
          public void setApplicationContext(ApplicationContext 
      applicationContext) throws BeansException {
              this.applicationContext=applicationContext;
          }
      }
  </code></pre>
  
  <p>pom.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>    &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
           &lt;/dependency&gt;
           &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-config&lt;/artifactId&gt;
              &lt;version&gt;RELEASE&lt;/version&gt;
           &lt;/dependency&gt;
           &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
               &lt;version&gt;1.18.8&lt;/version&gt;
            &lt;/dependency&gt;
      &lt;/dependencies&gt;
  </code></pre>
  
  <p>only bootstrap.yml:</p>
  
  <pre><code>     management:
        endpoints:
          web:
            exposure:
             include: '*'
        endpoint:
          health:
            show-details: always
  </code></pre>
  
  <p>when start the application it will output<br/> <code>[Users(name=hello0, age=23, gender=fe)]</code>,<br/>
  and then visit the url by POST,<br/>
          <code>http://localhost:8080/actuator/refresh</code><br/>
  it will output<br/>
          <code>before remove：Users(name=hello0, age=23, gender=fe)</code><br/>
          <code>after：Users(name=hello1, age=23, gender=fe)</code><br/>
  then visit the url<br/>
          <code>http://localhost:8080/hello3</code><br/>
  output<br/>
          <code>[Users(name=hello1, age=23, gender=fe)]</code><br/>
  This is to be expected,if visit the url again<br/>
          <code>http://localhost:8080/actuator/refresh</code><br/>
  output<br/>
          <code>before remove：Users(name=hello1, age=23, gender=fe)</code><br/>
          <code>after：Users(name=hello2, age=23, gender=fe)</code><br/>
  This is also to be expected,but visit the url<br/>
          <code>http://localhost:8080/hello3</code><br/>
  we hope it output <br/>
          <code>Users(name=hello2, age=23, gender=fe)</code><br/>
  but it output<br/>
          <code>Users(name=hello1, age=23, gender=fe)</code><br/></p>
  
  <p>The output message is not what we expected.<br/>
  If change @Autowired to @Resource in class UsersController, it should be normal.But it maybe null in real environment,the application will run fail if use @Resource<br/>
  How can I solve this problem?<br/>
  Thanks for your time.</p>