0

I've been working around Java and Spring(Batch, Boot), and have a question.

I created beans by Spring for generating a service class. I bind it with @Autowired annotation like this:

@Component
public class MyTaskRunner {

  @Autowired
  private MyService myService;

  public void run() {
    List<SomeObject> someObjects = myService.getSomeObjects();
    // Do some tasks
  }
}

Here, I also want to use such a bean in SomeObject which are deserialized from a database(meaning, not from Spring). I've looked for solutions here and am going to try them, but most of them seem elaborate, just for injecting a bean.

My question is: Is it so unusual usage of bean that it needs some work?

Thank you in advance! Any comments would be appreciated.

tsuda7
  • 413
  • 1
  • 6
  • 25
  • 1
    Be more specific about why you're serializing an object that needs access to services. It's often better to use Command Object and store only the configuration for the job, then combine it with its environment when you execute it. – chrylis -cautiouslyoptimistic- Apr 07 '16 at 07:08
  • The object is a configuration of a batch, which is serialized and saved into a database, and the class itself has a method which executes its batch task. The batch task needs access to other tables in the database, and I want to use a service bean or dao there. Should I add this information to the question? – tsuda7 Apr 07 '16 at 07:12
  • I found your answer to another question: http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null I'm going to read it and try understand Spring more deeply. – tsuda7 Apr 07 '16 at 07:32
  • 1
    I would recommend something like the Visitor pattern, where the execute method is passed a context object that contains its dependencies. See, for example, how EL (such as SpEL) is invoked. – chrylis -cautiouslyoptimistic- Apr 07 '16 at 08:22
  • @chrylis, The Visitor pattern is really what I need! Thank you very much for informing such a way, I'll create a component that has visit methods, and inside the component I'll autowire the service bean. – tsuda7 Apr 07 '16 at 09:11

0 Answers0