-
day62 - Spring Framework(스프링, Annotation)KIC/Spring 2021. 9. 10. 23:19반응형
[Main3_xml.java]
package ch02_mainDI; import org.springframework.context.support.GenericXmlApplicationContext; import di03.AuthException; import di03.AuthenticationService; import di03.PasswordChangeService; import di03.UserNotFoundException; public class Main3_xml { public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:ch02_DIXML/Container3.xml"); PasswordChangeService pcw = (PasswordChangeService) ctx.getBean("pwChangeSvc"); System.out.println(pcw); AuthenticationService authSvc = ctx.getBean("authenticationService", AuthenticationService.class); runAuthAndCatchAuthEx(authSvc, "bkchoi", "1111"); runAuthAndCatchAuthEx(authSvc, "bkchoi", "11111"); runAuthAndCatchAuthEx(authSvc, "bkchoi", "111111"); try { authSvc.authenticate("bkchoi2", "1111"); } catch (UserNotFoundException ex) { } authSvc.authenticate("bkchoi", "1234"); System.out.println("======================="); PasswordChangeService pwChgSvc = ctx.getBean(PasswordChangeService.class); pwChgSvc.changePassword("bkchoi", "1234", "5678"); runAuthAndCatchAuthEx(authSvc, "bkchoi", "1234"); authSvc.authenticate("bkchoi", "5678"); ctx.close(); } private static void runAuthAndCatchAuthEx( AuthenticationService authSvc, String userId, String password) { try { authSvc.authenticate(userId, password); } catch (AuthException ex) { } } }
[Main4_xmlAnno.java]
package ch02_mainDI; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; import ch02_DIXML.ContainerAnno; import di03.AuthException; import di03.AuthenticationService; import di03.PasswordChangeService; import di03.UserNotFoundException; public class Main4_xmlAnno { public static void main(String[] args) { useBean(new GenericXmlApplicationContext("classpath:ch02_DIXML/Container3.xml")); System.out.println("===================="); useBean(new AnnotationConfigApplicationContext(ContainerAnno.class)); } static void useBean(AbstractApplicationContext ctx) { PasswordChangeService pcw = (PasswordChangeService) ctx.getBean("pwChangeSvc"); System.out.println(pcw); AuthenticationService authSvc = ctx.getBean("authenticationService", AuthenticationService.class); runAuthAndCatchAuthEx(authSvc, "bkchoi", "1111"); runAuthAndCatchAuthEx(authSvc, "bkchoi", "11111"); runAuthAndCatchAuthEx(authSvc, "bkchoi", "111111"); try { authSvc.authenticate("bkchoi2", "1111"); } catch (UserNotFoundException ex) { } authSvc.authenticate("bkchoi", "1234"); System.out.println("======================="); PasswordChangeService pwChgSvc = ctx.getBean(PasswordChangeService.class); pwChgSvc.changePassword("bkchoi", "1234", "5678"); runAuthAndCatchAuthEx(authSvc, "bkchoi", "1234"); authSvc.authenticate("bkchoi", "5678"); ctx.close(); } private static void runAuthAndCatchAuthEx( AuthenticationService authSvc, String userId, String password) { try { authSvc.authenticate(userId, password); } catch (AuthException ex) { } } }
300x250'KIC > Spring' 카테고리의 다른 글
day65 - Spring Framework(스프링, Annotation) (0) 2021.09.15 day64 - Spring Framework(스프링, AOP) (0) 2021.09.14 day63 - Spring Framework(스프링, AOP) (0) 2021.09.13 day61 - Spring Framework(스프링, DI) (0) 2021.09.10 day60 - Spring Framework(스프링, DI 시작) (0) 2021.09.08