0

I will try to explain this with an example, hopefully that is the easiest way to understand what i'm aiming at here. I'm still learning Spring, so maybe i'm just missing something obvious here.

Suppose we have a simple Spring application which gets a stream of integers from some source, applies some processing to it (what exactly is user defined), and then sends those processed integers to some sink.

Lets also assume there is some nice web GUI as a frontend where the user can add different processors at will, like addition, subtraction and so on and then for each processors how much for example he would like to add.

How would i apply these changes the user can make to a running Spring application? Especially if these are bigger changes which require removing/adding beans or have bigger consequences for an already running bean?

How would i save and apply these changes when the Spring application starts again?

My intuitive approach would be to have these processors annotated with @Scope("prototype") and then simply create what beans are used when i receive a change to what processors are there. Changes can theoretically also be handled this way, but that doesn't seem like a good solution at all when the beans take longer to load and not very "springy". Also poses the problem what happens when the user makes fast changes and the beans are not even refreshed when the next changes come in.

Saving is kind of the same boat, i would probably write it to some database and then query that on startup for how the configuration was, but that also feels not very "springy".

Any suggestions appreciated :)

edit: For my actual use case, the processors need to be beans (i think) because i want to able to inject other classes in there.

wlfbck
  • 554
  • 8
  • 22

1 Answers1

2

sorry that this is an answer, rather than a comment, as it will be long.

Why should you create (Beans) as the user wills. Does this mean if the User makes a mistake on the configuration file, then you will delete those already created beans?

You could consider it in a different way, such as the following example which is more "springy" :)

1- Create hot-reload properties: https://www.baeldung.com/spring-reloading-properties How to hot-reload properties in Java EE and Spring Boot?

2- On configuration change, call ProcessHandler.addProcess, or ProcessHandler.deleteProcess. You achieve the same thing using only 1 (Bean), and 1 (Pojo)

@Service
public class ProcessHandler{
   
   @Autowired
   private Configuration yourConfig;

   private List<Processor> processors = new ArrayList<Process>();

   public void addProcess(){
    
      Process process = new Process(yourConfig);
      process.doSomething();
      processors.add(process);
   }

   public void removeProcess(){
   }
  
}

public class Processor{

    public Processor(Configuration yourConfig){

   //Do some work related to configurations; 
  }
}
  
JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • So you wouldn't make a processor a spring bean at all then? >Why should you create (Beans) as the user wills. Does this mean if the User makes a mistake on the configuration file, then you will delete those already created beans? Oh the user would never access those configuration files himself, only via the GUI. i would be responsible for saving this. But ye, that would imply that. – wlfbck Nov 22 '21 at 11:56
  • A Spring Bean is something that Spring manages. In your case, you want to manage addition/deletion of (Something) based on whatever the user modifies. The decision is whether you want it to be `simple` or `complex` to achieve your purpose. I would pick the `simple` choice if it fits the requirements. – JCompetence Nov 22 '21 at 11:57
  • Sorry for the botched comment above, but SO just doesn't let me put in newline in comments. – wlfbck Nov 22 '21 at 11:58
  • Yeah, but i thought there could be a way where spring manages this depending on what the user does. Because this seems like i'm back at square one with regards to having to control my object relations and stuff. – wlfbck Nov 22 '21 at 12:00
  • No worries. Please take this as a suggestion. It is to simplify your design. Of course you can make Spring do bean injection using `ApplicationContext` – JCompetence Nov 22 '21 at 12:00
  • You could also look at this https://medium.com/@venkivenki4b6/spring-dynamically-register-beans-in-4-ways-at-run-time-c1dc45dcbeb9 Good luck! – JCompetence Nov 22 '21 at 12:01