java - Static util class with Spring, unsure if I should make it a SpringBean, design concerns -
quick high level concept of design..
cli tool create aws ebs snapshots cli tool calls java class com.util.snapshotutil com.util.snapshot calls aws interfacing class com.aws.awsadapter
example usage command line..
cli-tool create-snapshot.. calls java class calling below method snapshotutil.createsnapshot() // statically call awsadapter.createsnapshot();
currently static , outside of spring.
now wondering if awsadapter should not static, , loaded spring, mean snapshotutil need create adapter through applicationcontext, believe, supplying xml adapter bean info.
originally thought since simple util deal ebs snapshots, ignore spring, awsadapter potentially used other means, however, not sure if being static pro or con.
the adapter designed deal ebs snapshot, either creating / deleting / viewing snapshots using amazonec2client instance. if spring managed class wanted use adapter, question if matters if loads adapter through spring or statically call it.
edit in response answer:
i started turning bean , removed static references, gave method getinstance() load through applicationcontext , return caller after initializing other dependencies , configurations. when call outside of spring, okay? seems working, still considered 'injecting'? pretty sure not injecting, since caller not spring managed, feel may hacky? in, using spring bean in non spring class, im never spring managed, feel there no reason turn utility bean. still going because understand benefits.
one reason 'have' turn bean, uses spring bean need handle authenticating, thought , instantiate other bean using 'new' keyword.
am correct when turned class bean not 'injected' callers, @ least when using getinstance() method? if use getinstance method() in spring bean, there difference if 'inject' utility through spring configurations instead?
generally, should favor non-static on static. regarding specific example, should go spring beans, because gives more flexibility when start extending application/module more complex features.
for example, static-only classes require resources other parts of system (and know how di helps here).
or need advise static invocations aspects (be simple ones logging of each request, think of more complex cases transactions). spring beans simple achieve and, important, simple add afterwards without big re-engineering , re-testing.
also, easier integrate beans other spring apis , frameworks integrated spring. example, easy use bean in apache camel route.
these few points came mind, there many more of them. but, always, consider pros , cons , pick right tool job.
edited part of question
"when call outside of spring, okay?"
yes, it's fine obtain bean instance spring application context directly in class not managed spring or when bean name not known until runtime. in example apache camel route, that's camel does. :)
"am correct when turned class bean not 'injected' callers, @ least when using getinstance() method?"
yes, still bean of bean's functionality (with other beans injected in it, aspects around it, etc).
"if use getinstance method() in spring bean, there difference if 'inject' utility through spring configurations instead?"
regarding this, may take @ question , @ article written martin fowler, referenced question.
in opinion, should not it, less readable , quite unnecessary. injecting resources fields type safe , clean mechanism class declare dependent resources.
also, bean lookup may costly if executed frequently. experienced on project worked on in past. don't know why, takes time spring (at least spring version used then) , return bean, , noticeable if executed in loop.
Comments
Post a Comment