Spring 에게 Bean 생성 요청 ( 컴포넌트 스캔 )
-변경 전 : @Bean annotation 을 사용하여 수동으로 Bean 을 생성
#--------------------------------------------------------------------------------------------------------------------------
# App03GamingSpringBeans.java
#--------------------------------------------------------------------------------------------------------------------------
@Configuration
public class App03GamingSpringBeans {
@Bean
public GamingConsole game() {
var game = new PackmanGame();
return game;
}
public static void main(String\[\] args) {
try (var context = new AnnotationConfigApplicationContext( GamingConfiguration.class)) {
context.getBean(GamingConsole.class).up();
}
- 변경 후 :②@Component 어노테이션을 PackmanGame 에 선언
- → 클래스패스 스캐닝 시 Auto wiring 됨
- ① ComponentScan
#--------------------------------------------------------------------------------------------------------------------------
# App03GamingSpringBeans.java
# - @ComponentScan annotation 추가
#--------------------------------------------------------------------------------------------------------------------------
@Configuration
@ComponentScan("스캔대상 package 경로")
public class App03GamingSpringBeans {
public static void main(String\[\] args) {
try (var context = new AnnotationConfigApplicationContext( GamingConfiguration.class)) {
context.getBean(GamingConsole.class).up();
}
\--------------------------------------------------------------------------------------------------------------------------
#PacmanGame.java
# - @Component annotation 추가
#--------------------------------------------------------------------------------------------------------------------------
@Component
@Primary // 동일한 bean 이 있을때 우선권 부여
public class PacmanGame implements GamingConsole {
...
}
@Primary 와 @Qualifier 에 대해 알아보기
① @Primary : 최우선권을 가진다.
여러 후보가 자격이 있는 경우 특정 Bean 에게 우선구너을 주는 것
② @Qualifier : @Primary 보다 더 우선순위를 가지고 autowire 할때 사용
특정 Bean 이 auto wired 되어야 할 때 사용
@Component
public class PackmanGame implements GamingConsole {
...
@Component
@Primary // 최우선권을 가지는 Bean
public class MarioGame implements GamingConsole {
@Component
@Qualifier("SuperContraGameQualifier")
public class SuperConrtraGame implements GamingConsole {
@Component
public class GameRunner {
private GamingConsole game;
public GameRunner(@Qualifier("SuperContraGameQualifier") GamingConsole game) {
this.game = game;
}
3가지 타입의 Dependency Injection
Constructo-based : 생성자 기반의 의존성 주입 (가장 추천)
Setter-based : setter method를 이용한 의존성 주입
Field
@Component
calss YourBusinessClass {
@Autowired // Field Injection
Dependency1 dependency1;
Dependency2 dependency2;
@Autowired // Setter Injection
public void setDependency2(Dependency2 dependency2) {
this.dependency2 = dependency2;
}
Dependency3 dependency3;
// Constructor Injection
//@Autowired // 생성자 주입은 @Autowired가 없어도 의존성 주입이 된다
public YourBunessClass(Dependency3 dependency3) {
super();
this.dependency3 = dependency3;
}
public String toString() {
return "Using " + dependency1 + " and " + dependency2;
}
}
@Component
calss YourBusinessClass {
}
@Component
calss YourBusinessClass {
}
@Configuration
@ComponentScan
public class DepInjectionLauncherApplication {
public static void main(String[] args) {
try (var context =
new AnnotationConfigApplicationContent(DepInjectionLauncherApplication.class)) {
Arrays.stream(context.getBeanDefinitionNames())
.forEach(System.out::println);
System.out.println(context.getBean(YourBusinessClass.class));
}
}
}
'IT양아치' 카테고리의 다른 글
Git 초기 설정 (0) | 2024.11.13 |
---|---|
MariaDB 접속방법과 테이블 생성 (0) | 2024.11.09 |
Section 2. Java Spring Framework 시작하기 (0) | 2024.11.04 |
Docker Maria DB 설치 및 실행 (0) | 2024.10.26 |
sudo 없이 docker 그룹에 사용자 추가 (0) | 2024.10.26 |