java - How can Spring instantiate cClasses with private constructors (singleton pattern) and objects from Factories? -
i studying spring core certification , have following doubt related how can spring instantiate classes private constructors (such singleton pattern) or how can instantiate objects factories (that not spring context).
for example have following singleton factory:
public class accountservicesingleton implements accountservice { private static accountservicesingleton inst = new accountservicesingleton(); private accountservicesingleton() { ... } public static accountservice getinstance() { // ... return inst; } }
this singleton factory because build private static object builded private constructor , have public method object.
so think problem how can spring build object? depends on fact that constructor private can't in java configuration class
@confguration public class applicationconfig{ @bean public accountservicesingleton accountservicesingleton(){ return new accountservicesingleton(); } }
because can't access private accountservicesingleton() constructor.
at same time can't use equivalent xml configuration same reason.
have understand problem or missing something?
i think missing because on documentation read can use following 2 solutions previous problem:
use @bean method in @configuration class: so, reading it, think previous java configuration class work...but why?
xml factory-method attribute in xml configuration, searching online found have this, how can use configure previous accountservicesingleton bean in xml configuration?
tnx
it's easy, , can it, too:
constructor cxor = accountservicesingleton.class.getdeclaredconstructor(); cxor.setaccessible(true); return cxor.newinstance();
Comments
Post a Comment