3
public static final String STUDENTS = "studentsInfo";
     @Bean(name = "students")
      public List<Student> getStudents() {
        String configuredStudents = env.getProperty("studentsInfo"); //JSON having list of students info
        List<Student> students;
        if (configuredStudents != null) {
          try {
            students =
                Arrays.asList(
                    JsonUtilFactory.getInstance(JsonUtilFactory.JsonUtilType.LOCAL)
                        .toObject(configuredStudents, Student[].class));
          } catch (Exception e) {
           
          }
        }
        return students;
      }
  1. I'm able to refresh env variable [i.e without system restart @RefreshScope and spring-cloud-starter-kubernetes-config]. How to make those changes to bean students ?
  2. Also if I have to partially update JSON [say add a new student], what would be the right approach?
supraja
  • 93
  • 2
  • 11

1 Answers1

1

While I am not sure why you want to create bean of all students, below are my suggestions.

DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) context.getBeanFactory();
registry.destroySingleton({yourbean}) //destroys the bean object
registry.registerSingleton({yourbeanname}, {newbeanobject}) //add to singleton beans cache

How to reinitialize a Spring Bean?

A better approach will be to store them in a noSql database and return the list or if you want to read from file override DataSource to read from file.

Sagar
  • 61
  • 9