1

I want to have a Factory class, which has a method getInstance.

public PersonService getInstance(String someParameter1, Object someParameter 2, ...)

I dont want to instantiate a factory class. i just want to call a method on my factory like

PersonService service = MyFactory.getInstance(..) // some arguments passed

How we can achieve this in spring?

Coralie B
  • 593
  • 3
  • 11
Vishesh
  • 111
  • 1
  • 1
  • 5

1 Answers1

0

In Spring, you're supposed to let the container construct your instances.

You should implement a FactoryBean<PersonService> and add it to your context configuration.

Instead of calling getInstance()/getObject() yourself, just have Spring inject the service via @Autowired where you need it.

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
  • But i need to return a service based on some condition in my factory class. I dont want my client to know which service impl i am returning, hence factory makes sense. If i were to use Factory Bean, then how can i impose those conditions which are provided to my factory my client. – Vishesh Mar 03 '16 at 08:29
  • Unless you can be more specific, all I can say is "by writing the code". – OrangeDog Mar 03 '16 at 09:59