02.Dependency Injection - Spring Container.
Dependency Injection - Spring Container.
Object එකක්
තව object එකක්
මත depend වෙනවා.Object hierarchy එකක් වගේ තම තියෙන්නේ.එම
dependencies අප
විසින් handle නොකරන
අතර එය spring, spring boot වලින් handle කරනු
ලබයි. Example එකක්
ලෙස laptop class එක සලකන්න.Laptop එකක්
තව ගොඩක් දේවල් මත රදා පවතිනවා hard drive,
ram වගේ ඒවා මත. අප laptop object එකක්
හදන විට එකට
අදාල ඒ dependencies එනම් hard drive, ram වගේ ඒවා දාලා අපි හදන්න යන්නේ නෑ.අපි කරන්නේ අපි framwork එකන්
ඉල්ලනවා මට
laptop එකක් ඕනේ කියලා. framwork එක ඕනේ කරන සියලුම dependencies ටික දාලා අන්තිමට අපිට
full comlete laptop එක දෙනවා.එනම් අවශ්ය වන dependencies ටික හදල inject කරන්නේ
frmawork එකෙන්.ඒක තමා dependency injection කියන්නේ.මෙය spring boot වලදී කරනු ලබන්නේ Spring Container මගිනි.
මෙය
අවශ්ය වෙන්නේ ඇයි,
Loosely coupled කියන
එක ලගා කරගන්න සහ application එක easily
test කරන්න පුලුවන් වෙන්න
තමා dependency injection කියන එක use වෙන්නේ.
හිතනන
ASUS laptop එකේ දැනට use කරන්නේ Samsung hard drive එකක් කියලා. Loosely coupled නෑ
කියන්නේ එනම්
tightly couple කියන්නේ එයාල
Samsung hard drive මතම
depend කියන එක.එත් එහෙම නෙවෙයිනෙ වෙන්නේ
Samsung නැත්තන් ASUS company එක මොකද කරන්නේ වෙන company එකකට
යනවා hard drive එක හදාගන්න.එක
තමා loosly couple කියනේ.මේක program එකේදී
integrate කරන්න
තමා abstraction කියන එක use කරන්නේ.එනම් abstract class හෝ interface use කරලා hard drive එක implement කරනවා.අපි hard drive එක refer කරන්නේ
interface එක
හරහා නිසා ASUS වලට hard drive හදන company එක මාරු කරන්න පුලුවන් වගේ
code එකෙනුත් implement එක මාරු කරන්න ලේසියෙන්ම පුලුවන්
සහ එම dependencies අපි handle නොකරන
අතර 3rd party කෙනෙක් එනම් framwok එක මගින් එම dependencies inject කිරීමද සිදු කරයි.එනම් loosly couple වෙලා
තියෙන්නේ. තවද
hard drive එක
පිටින් අරන් සෙට් කරන නිසා ASUS වලට එයාලගේ laptop එක test කරන්න ලේසි.ඉතින් code එකෙත් loosly couple එක
නිසා එක class එකක්,
එකට depend වෙන
අනෙත් ඒවාට බලපෑමක් ඇති
නොවන ලෙස test කරීම ද ලේසි.
Spring වලදී
වෙන්නේ මෙහෙමයි
Spring වලදී
අපි කියනවා මොනවද inject කරන
ඕන object කියලා.මෙයට class name එකට
උඩින් @Component කියලා
යොදනවා එහෙමත් නැත්තන් XML එකක beans ටික configure කරනවා(XML
configure කරන එක පරණ ක්රමයකි). Spring
framework එක ඊට අදාල class වලින් object (beans) හදලා
container එකක තියාගන්නවා.මේක
spring වලදී spring container කියලා
කියනවා.අපිට bean එකක් අවශ්ය වන විට
අප එය spring වලින්
ඉල්ලනවා. Spring වලින් එම එම bean එක අපිට ලබා දෙන්නේ ApplicationContext හෝ BeanFactory හරහා.මේවා factory pattern එක use කරන අතර එමගින් අපට spring container එකෙන් අවශ්ය beans ලබා දෙනු ලබයි.එහිදී එම beans ආකාර දෙකකට ලබා දෙනු ලබයි. එනම් singleton
සහ prototype කියන
අකර දෙකකිනි.මේවා
beans scope නම් වේ. Singleton වලදී එකම object එක සැම request එකකටම
ලබාදෙන අතර, prototype වලදී request එකෙන්
request එකට new bean එකක්
හදල දෙනු ලබයි.තවද Spring Controller එකේ
හැදෙන bean refer කල හැකි ආකාර දෙකක් පවතින අතර ඒවා නම්, object type එකෙන්
refer කිරීම
සහ object name එකෙන්
refer කිරීමයි.
@Component
Class Student{}
Object type එකෙන් refer – student විදිහට තමා spring container එකේ search කරන්නේ.
Spring container එක තුල object
එක ඇත -> Student student = new Student();
@Autowired
private Student
student;
................................................................................................................
@Component(“stu”)
Class Student{}
Spring container එක තුල object
එක ඇත -> Student student = new Student();
@Autowired
@Qualifier(“stu”)
Private Student
student;
@Autowired
Spring bean container එකේ එකිනෙකට depend වන beans දෙකක්
එකිනෙක connect කරන්න
තමා යොදා ගන්නේ.
BeanFactory Container is basic container, it can
only create objects and inject Dependencies.
Beanfactory Container will not create a bean object until the request
time. It means Beanfactory Container loads beans lazily.
While ApplicationContext Container creates objects of Singleton bean
at the time of loading only.
Container තුල ඇති bean වලට scope දෙකකි,
singleton
prototype
Default එන්නේ
singleton කියන එක.නමුත් prototype එක enable කරගන්න
ඕනෙනම්,
@Component(“stu”)
@Scope(value=”prototype”)
Class Student{}
ලෙස
දෙනුලබයි.
........................................................................................................................................
Comments
Post a Comment