while(1) work();
반응형
11. 기타 기능들

@Qualifier @Qualifier 어노테이션을 통해 같은 타입의 빈이 여러개인 경우 주입받을 빈을 지정할 수 있다. package com.sample.spring.repository; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Repository; @Repository @Qualifier("primaryRepository") public class SampleRepositoryImpl2 implements SampleRepository { public int getLastUserId() { return 200; } } package com.sample.spring..

10. Component Scan과 생성자 주입

ComponentScan 만약 프로그램의 규모가 커져서 등록해야 할 빈이 수백, 수천개라면 어떻게 해야할까? 의존관계 설정파일을 관리하는데 어머어머한 시간이 소요될 것이다. Spring context에서는 빈을 자동으로 찾아 등록(주입이 아니다)하는 Component scan 기능을 제공한다. 설정 파일에 @ComponentScan 어노테이션을 붙임으로써, Component scan을 사용할 수 있다. package com.sample.spring; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @Compon..

9. 의존관계 자동 주입 (@Autowired)

Spring context에서는 설정 파일을 통해 의존관계를 주입하는 방법 외에도 @Autowired 어노테이션을 통해 주입하는 방법을 지원한다. Setter 주입 package com.sample.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import com.sample.spring.repository.SampleRepository; import com.sample.spring.repository.SampleRepositoryImpl1..

8. 빈 생명 주기

빈 생성-소멸 과정 Spring 컨테이너는 아래와 같은 순서로 빈을 생성한다. 스프링 빈 생성 의존관계 주입 PostConstruct 콜백 객체 사용 PreDestory 콜백 객체 소멸 (단 생성자 주입을 사용하는 경우 의존관계 주입은 (당연히) 스프링 빈 생성과 동시에 일어난다) 위와 같은 생명주기 하에서, 빈이 생성되고 나서(post construct) 또는, 빈이 소멸되기 전(pre destroy)에 실행될 메서드를 지정할 수 있다. 어노테이션을 이용한 생명주기 콜백 JSR-250 표준 어노테이션인 @PostConstruct와 @PreDestroy 를 이용해 특정 생명주기에서 실행될 메서드를 지정할 수 있다. 다만 @PostConstruct와 @PreDestroy 어노테이션은 java.xml.ws...

article thumbnail
7. Singleton Pattern

Java Singleton 자바에서 Singleton 패턴을 구현하려면 아래와 같은 귀찮은 코드들이 필요하다. public class Singleton { private final static Singleton instance = new Singleton(); private Singleton() { // 객체 생성 방지하기 위해 private 생성자 사용 } public static Singleton getInstance() { return instance; } public void logic() { ... } } Singleton obj1 = Singleton.getInstance(); Singleton obj2 = Singleton.getInstance(); obj1 == obj2 //true Spr..

6. 모든 빈 조회

package com.sample.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); for (String beanName : context.getBeanDefinitionNames()) { System.out.println(beanName); }..

5. 진정한 DI

지금까지 구현한 코드에는 사실 여러 문제가 있다. 코드가 Spring Framework에 지나치게 의존하고 있다. getBean 메서드가 제공되지 않는(다른 이름으로 제공되는) DI 프레임워크로 교체해야한다면 모든 코드를 다 바꾸어야 한다. 생성된 ApplicationContext를 계속 전달하며 공유해야 한다. 사실 지금까지는 의존성을 주입받았다고 보기는 어렵다. 생성자를 통해 ApplicationContext를 주입 받았고, 그로부터 SampleRepository 타입 객체를 가져와 사용했을 뿐이다. 사전 준비 그 전에 먼저, 코드를 간소화하기 위해 Application class도 Bean으로 등록시켜주자. package com.sample.spring; import org.springframewo..

4. Annotation Context와 두 개의 Bean

Annotation을 이용한 빈 설정 XML을 이용해 빈을 설정하는 것은 상당히 번거롭다. Spring 3.2 버전부터 Java의 Annotation 문법을 사용해 빈 설정이 가능하도록 지원한다. package com.sample.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; //import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(Str..

반응형

검색 태그