Disper
Disper

Reputation: 745

Autowiring class with dynamic param value in Spring

I want to do the following in my Spring MVC application:

  1. Get some data from the database (It will return List<String> list ) in my @Service MainService.
  2. Use this list as a constructor parameter for other @Service ConfigFactory that I have currently @Autowired in MainService.
  3. Fire a method from ConfigFactory to get final result that will be added to ModelAndView in my @Controller class.

I know it would be possible to this that way:

ConfigFactory class:

@Service
public class ConfigFactory(){
    public void init(List<String> list){
        //Use list to initialize ConfigFactory
    }

    public Result getResult(){
        //Do some business logic
        return result;
    }
}

MainService class:

@Service
public class MainService {
    @Autowired ConfigFactory configFactory;

    public Result method(){
        //Get list from database;
        configFactory.init(list);
        Result result = configFactory.getResult();

        //Create a Result that will be later added to controller ModelAndView. 
    }
}

But it doesn't feel nice. So I'm stuck here. Does anyone have an idea how to properly realize this?

Upvotes: 0

Views: 224

Answers (1)

mael
mael

Reputation: 2254

Why don't you pass the list as a parameter to the getResult() method? I suppose that you get values from the list in the init() method, and you initialize some properties in your ConfigFactory? Don't do that, you will probably get problems when several users try to do the same thing.

Upvotes: 1

Related Questions