KIC/Spring

day62 - Spring Framework(스프링, Annotation)

바차 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