Download - Spring IoC
- 1. IOC & Spring Framework
([email protected])
(http://cafe.naver.com/architect1.cafe)
2. 3. 4. 5. 6. 7. Inversion of Control
invoke
dont call me I will call you
Dependency injection
Runtime .
Bean .
Lookup .
.
Unit Test .
8. Non-IoC / Dependency Injection
Source: Spring Documentation
9. publicclassCampaignServiceImplimplementsCampaignService{
public CampaignupdateCampaign(Campaign campaign) throws
CampaignException{
try{
CampaignDAO campaign = new CampaignDAOImpl();
..
OpportunityDAOoppDAO= new OpportunityDAOImpl();
// Alternatively, initialize thru factory
// OpportunityDAOoppDAO = OppFactory.getOpp();
oppDAO.save(campaign);
..
}catch(Exception e){
}
}
Non-IoC Service Object
10. IoC / Dependency Injection
Source: Spring Documentation
11. publicclassCampaignServiceImplimplementsCampaignService{
public Campaign updateCampaign(Campaign campaign) throws
CampaignException{
try{
oppDAO.save(campaign);
}catch(Exception e){
}
}
// Spring sets the value thru runtime injection!
private setOpportunityDAO(OpportunityDAOoppDao){
this.oppDAO = oppDao;
}
IoC Service Object
12. , , , .
: Servlet Container, EJB Container
?
Service
Init
Create
Destory
Container
A
B
C
D
13. POJO(Plain old Java Object)
Servlet EJB API .
POJO .
Spring & POJO
Spring POJO
Spring POJO Life Cycle IoC .
POJO?
14. IoC( DI) Container
POJO , , POJO .
POJO , Container .
Transaction, Security . AOP Container
IoC Container
Service
Init
Create
Destory
IoC( DI) Container
POJO A
POJO B
POJO C
POJO D
15. IoC
IoC : Inversion of Control
DI : Dependency Injection
DP : Dependency Pull
- EJB
16. SpringDP
IoC
Setter Inj
DI
Constructor Inj
- Spring
17. PicoContainerMethod Inj
18. A lightweight non-intrusive framework which addresses various
tiers in a J2EE application.
What is Spring Framework?
19. Not a J2EE container. Doesnt compete with J2EE app servers.
Simply provides alternatives.
POJO-based, non-invasive framework which allows a la carte usage of
its components.
Promotes decoupling and reusability
Reduces coding effort and enforces design discipline by providing
out-of-box implicit pattern implementations such as singleton,
factory, service locator etc.
Removes common code issues like leaking connections and more
Support for declarative transaction management
Easy integration with third party tools and technologies.
Spring Benefits
20. IoC
IoC POJO .
POJO IoC Spring Framework
Spring FrameWorkPOJO-based, non-invasive framework
IoC Spring Framework
21. Spring IOC
22.
publicclassCreateCreditCardAccountimplementsCreateCreditCardAccountInterface
{
privateCreditLinkingInterfacecreditLinkingInterface;
publicvoidsetCreditLinkingInterface(
CreditLinkingInterfacecreditLinkingInterface) {
this.creditLinkingInterface = creditLinkingInterface;
}
publicCreditRatingInterfacegetCreditRatingInterface() {
returncreditRatingInterface;
}
publicvoidcreateCreditCardAccount(ICustomericustomer) throws
Exception{
booleancrediRating =
getCreditRatingInterface().getUserCreditHistoryInformation(icustomer);
icustomer.setCreditRating(crediRating);
//Good Rating
if(crediRating){
getCreditLinkingInterface().linkCreditBankAccount(icustomer);
}
getEmailInterface().sendEmail(icustomer);
}
Step1. DI
23.
http://localhost/creditLinkService
Step2.
24. ClassPathXmlApplicationContextappContext =
newClassPathXmlApplicationContext(new String[] {
"classpath:spring/springexample-creditaccount.xml});
CreateCreditCardAccountInterfacecreditCardAccount =
(CreateCreditCardAccountInterface)
appContext.getBean("createCreditCard");
creditCardAccount.createCreditCardAccount(icustomer);
Step3. Factory
25. @RunWith(MockitoJUnitRunner.class)
publicclassCreateCreditCardAccountTest {
CreateCreditCardAccountcreateCreditCardAccount;
@Mock
CreditLinkingInterfacecreditLinkingInterface;
@Mock
CreditRatingInterfacecreditRatingInterface;
@Mock
EmailInterfaceemailInterface;
@Before
publicvoid setup(){
createCreditCardAccount = newCreateCreditCardAccount();
createCreditCardAccount.setCreditLinkingInterface(creditLinkingInterface);
createCreditCardAccount.setCreditRatingInterface(creditRatingInterface);
createCreditCardAccount.setEmailInterface(emailInterface);
}
@Test
publicvoidtestCreateCreditCardAccount() throws Exception {
ICustomercusomer = mock(ICustomer.class);
when(creditRatingInterface.getUserCreditHistoryInformation(cusomer)).thenReturn(true);
createCreditCardAccount.createCreditCardAccount(cusomer);
verify(creditLinkingInterface,times(1)).linkCreditBankAccount(cusomer);
}
}
Step4. TestCase
26. Setter
Concrete Class Interface
XML
Spring Bean Factory
Mock
DI
27.
http://openframework.or.kr/Wiki.jsp?page=IntroducingtheSpringFramework12
http://www.javajigi.net/pages/viewpage.action?pageId=3664
http://www.ibm.com/developerworks/web/library/wa-spring1/