스프링 프레임워크 핵심 기술
포스팅 참조 정보
GitHub
공부한 내용은 GitHub에 공부용 Organizations에 정리 하고 있습니다
해당 포스팅에 대한 내용의 GitHub 주소
실습 내용이나 자세한 소스코드는 GitHub에 있습니다
포스팅 내용은 간략하게 추린 핵심 내용만 포스팅되어 있습니다
https://github.com/freespringlecture/spring-core-tech/tree/chap02-04-component
freespringlecture/spring-core-tech
백기선님의 스프링 프레임워크 핵심 기술 강의 내용 정리. Contribute to freespringlecture/spring-core-tech development by creating an account on GitHub.
github.com
해당 포스팅 참고 인프런 강의
https://www.inflearn.com/course/spring-framework_core/dashboard
스프링 프레임워크 핵심 기술 - 인프런
이번 강좌는 스프링 부트를 사용하며 스프링 핵심 기술을 학습합니다 따라서 스프링 부트 기반의 프로젝트를 사용하고 있는 개발자 또는 학생에게 유용한 스프링 강좌입니다. 초급 웹 개발 Java Spring 온라인 강의
www.inflearn.com
실습 환경
- Java Version: Java 11
- SpringBoot Version: 2.1.2.RELEASE
2-4. IoC 컨테이너 4부: @Component와 컴포넌트 스캔
애플리케이션 구동방법
1. 기본적인 static 메서드 사용
싱글톤 스코프의 Bean들을 초기에 다 생성함 등록할 Bean이 많다면 구동시에 초기 구동시간이 꽤 걸림
SpringApplication.run(Demospring51Application.class, args);
2. Builder를 활용한 방법
3. 인스턴스를 만들어 사용하는 방법
var app = new SpringApplication(Demospring51Application.class);
app.run(args);
4. Functional 방법
구동 시 리플렉션이나 Proxy 같은 cg라이브러리 기법을 안쓰므로 성능상의 이점이 있음
하지만 일일히 Bean들을 등록해주기에는 너무 불편함 @ComponentScan
을 사용하는 것에 비해 너무 불편함
lambda 적용 전
var app = new SpringApplication(Demospring51Application.class);
app.addInitializers(new ApplicationContextInitializer<GenericApplicationContext>() {
@Override
public void initialize(GenericApplicationContext ctx) {
ctx.registerBean(MyService.class);
ctx.registerBean(ApplicationRunner.class, new Supplier<ApplicationRunner>() {
@Override
public ApplicationRunner get() {
return new ApplicationRunner() {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("Funational Bean Definition!!");
}
};
}
});
}
});
app.run(args);
lambda 적용 후
var app = new SpringApplication(Demospring51Application.class);
app.addInitializers((ApplicationContextInitializer<GenericApplicationContext>) ctx -> {
ctx.registerBean(MyService.class);
ctx.registerBean(ApplicationRunner.class, () -> args1 -> System.out.println("Funational Bean Definition!!"));
});
app.run(args);
컴포넌트 스캔 주요 기능
- 스캔 위치 설정: 어디부터 어디까지 scan할 것인가에 관한 설정
- 필터: 어떤 애노테이션을 스캔 할 지 또는 하지 않을 지 scan하는 중에 어떤 것을 걸러낼 것 인가에 관한 설정
Component
@Repository
@Service
@Controller
@Configuration
@ComponentScan
@ComponentScan
은 스캔할 패키지와 애노테이션에 대한 정보
다른 Bean 들을 등록하기 전에 먼저 Bean을 등록 해줌
실제 스캐닝 ConfigurationClassPostProcess 라는 BeanFactoryPostProcessor에 의해 처리됨
- 주의 사항
@ComponentScan
이나@SpringBootApplication
위치를 확인 위치에 따라 다른 패키지라면 Bean으로 등록되지않음@Component
로 지정이되어있는지 확인@ComponentScan
에 excludeFilters(예외)로 지정된 사항은 Bean으로 등록되지 않음
'개발강의정리 > Spring' 카테고리의 다른 글
[스프링 프레임워크 핵심 기술] 2-6. IoC컨테이너-profile property (0) | 2019.10.06 |
---|---|
[스프링 프레임워크 핵심 기술] 2-5. IoC컨테이너-bean scope (0) | 2019.10.06 |
[스프링 프레임워크 핵심 기술] 2-3. IoC컨테이너-Autowired (0) | 2019.10.06 |
[스프링 프레임워크 핵심 기술] 2-2. IoC컨테이너-ApplicationContext와 다양한 빈 설정 방법 (0) | 2019.10.06 |
[스프링 프레임워크 핵심 기술] 2-1. loC컨테이너-스프링 IoC 컨테이너와 빈 (0) | 2019.10.06 |
댓글