简体   繁体   中英

How can we create a factory pattern to return a spring bean

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM