목록분류 전체보기 (309)
진취적 삶
실행시간을 출력하려면 메서드의 시작과 끝에서 시간을 구하고 두 시간의 차이를 출력 for 반복문의 경우 public class ImpeCalculator implements Calculator{ @Override public long factorial(long num) { long start = System.currentTimeMillis(); long result =1 ; for (long i =1 ; i
Aspect 로 사용할 클래스에 @Aspect 붙인다 @Pointcut 공통 기능을 적용할 Pointcut을 정의 공통 기능을 구현한 메서드에 @Around 적용 7.3.1 AOP 구현 공통 기능을 제공하는 Aspect 구현 클래스를 만들고 자바 설정을 이용해서 Aspect를 어디에 적용할지 설정 @Aspect public class ExeTimeAspect { @Pointcut("execution(public * chap07..*(..))") private void publicTarget() { } @Around("publicTarget()") public Object measure(ProceedingJoinPoint joinPoint) throws Throwable { long start = Syst..
스프링은 AOP 를 위한 프록시 객체를 생성할 때 실제 생성할 빈 객체가 인터페이스를 상속하면 인터페이스를 이용해서 프록시를 생성한다. RecCalculator 클래스가 calculator 인터페이스를 상속하므로 , calculator 인터페이스를 상속받은 프록시 객체를 생성 인터페이스가 아닌 클래스를 이용해서 프록시를 생성하고 싶다면 @Configuration @EnableAspectJAutoProxy(proxyTargetClass =true) public class AppCtx { 7.4.1 execution 명시자 표현식 @Pointcut("execution(public * chap07..*(..))") private void publicTarget() { } execution 명시자는 Advice..
스프링 컨테이너는 초기화와 종료라는 라이프 사이클을 갖는다. public class Main2 { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppContext.class); Greeter g = ctx.getBean("greeter", Greeter.class); String msg = g.greet("스프링"); System.out.println(msg); ctx.close(); } } AnnotationConfigApplicationContext 생성자를 이용해서 컨텍스트 객체를 생성할때 스프링 컨테이너를 초기화 한다. clos..
빈 객체의 라이프 사이클은 객체 생성 → 의존 설정 → 초기화 → 소멸 6.2.1 빈 객체의 초기화와 소멸: 스프링 인터페이스 InitializingBean , DisposableBean InitializingBean 은 초기화 과정에서 afterPropertiesSet() 메서드 실행 DisposableBean 은 소멸 과정에서 destroy() 메서드 실행 초기화와 소멸 의 예 데이터 베이스 커넥션 풀 커넥션 풀을 위한 빈 객체는 초기화 과정에서 데이터베이스 연결을 생성한다. 빈객체를 소멸할 때 사용중인 데이터베이스 연결을 끊어야 한다. 채팅 클라이언트 서버와 연결을 생성하고 끊는 작업은 초기화와 소멸 @Configuration public class AppCtx { @Bean public Clien..
Client client1 = ctx.getBean("client",Client.class) Client client2 = ctx.getBean("client",Client.class) //client1 == client2 true 한 식별자에 대해 한 개의 객체만 존재하는 빈은 싱글톤 범위를 갖는다. 빈의 범위를 프로토타입으로 지정하면 빈 객체를 구할때마다 매번 새로운 객체를 생성한다. 프로토타입 생성 @Bean @Scope("prototype") public Client client() { Client client = new Client() client.setHost("host") return client ; } 싱글톤의 경우 @Bean @Scope("singleton") public Client c..
스프링이 검색해서 빈으로 등록할수 있으려면 클래스에 @Component 을 붙여야한다 ChangePasswordService MemberDao MemberInfoPrinter MemberListPrinter MemberRegisterService Component 에 속성값 지정 안하는 경우 클래스 이름의 첫글자를 소문자로 바꾼 이름 속성값을 지정하는 경우 해당 속성값을 이름으로 사용하면 된다. @Component("listPrinter") public class MemberListPrinter { ....
@Configuration @ComponentScan(basePackages = {"spring"}) public class AppCtx { @Bean @Primary public MemberPrinter memberPrinter1() { return new MemberPrinter(); } @Bean public MemberPrinter memberPrinter2() { return new MemberSummaryPrinter(); } @Bean public VersionPrinter versionPrinter() { VersionPrinter versionPrinter = new VersionPrinter(); versionPrinter.setMajorVersion(5); versionPrinter...