Spring alternative for Factory -
may duplicate, please feel free tag... newbie spring.
i implementing userservice getting user details different vendors, class structure
interface userservice ->> userservicea, userserviceb
which user service use depends upon field called provider. code like
public interface externaluserservice { externaluserdto getuserdetail(string username); }
implementations -
public class googleuserservice implements externaluserservice{ @override public externaluserdto getuserdetail(string username) { return user; } } public class facebookuserservice implements externaluserservice{ @override public externaluserdto getuserdetail(string username) { return user; } }
i want use in code in fashion, dont know if possible, giving try see if possible
public class externalusermanager(string provider) { string provider; @autowired externaluserservice service; //this supposed come factory, dont know how in spring context. public void dosomething(string username) { system.out.println(service.getuserdetail(username)); } } had been in conventional java programming, have created factory called userservicefactory, have made things straight. can please me on how possible spring, , if possible, how can achieve it? use spring boot, no xml config.
you can use @bean annotated method scope 'prototype' factory. spring call method anytime bean injected somewhere.
import org.springframework.beans.factory.config.beandefinition; ... @bean @scope(beandefinition.scope_prototype) public externaluserservice externaluserservice(userservicefactory factory,userproviderresolver resolver) { .. create user service depending on resolver.getprovider() }
the userservicefactory used create specific service depending on provider name, described.
create class userproviderresolver whith method getprovider() returns provider name current request or user.
you can @autowire httprequest in userproviderresolver access current request.
Comments
Post a Comment