Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 문자열
- Eclipse
- Controller
- JDBC
- 조건문
- git
- 이클립스
- Database
- React
- Oracle
- Scanner
- jquery
- mysql
- string
- html
- rpa
- API
- Java
- Array
- 상속
- Board
- jsp
- db
- Thymeleaf
- Uipath
- MVC
- View
- spring
- SpringBoot
- 배열
Archives
- Today
- Total
유정잉
@ComponentScan과 @Bean의 차이점 본문
📌 @ComponentScan을 사용하면 @Bean을 따로 지정해줄 필요가 있을까?
✅ 결론: @ComponentScan을 사용하면 @Bean을 따로 지정하지 않아도 됨!
✅ 단, @Component를 붙여야 Spring이 자동으로 Bean으로 등록해줌!
1️⃣ @Component만 사용한 경우 (@Bean 필요 없음!)
✅ Test1 클래스 (자동 등록 방식)
package beans;
import org.springframework.stereotype.Component;
@Component // "Test1" 클래스를 자동으로 Bean으로 등록!
public class Test1 {
public void sayHello() {
System.out.println("Hello from Test1!");
}
}
✅ AppConfig 클래스 (@ComponentScan 사용)
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "beans") // "beans" 패키지 안의 @Component 클래스를 자동으로 Bean 등록
public class AppConfig {
}
✅ 실행 코드 (Main.java)
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
// 🔥 Spring 컨테이너 실행 (AppConfig 설정을 읽어옴)
ApplicationContext context = new AnnotationConfigApplicationContext(config.AppConfig.class);
// 🚗 Test1 Bean 가져오기
Test1 test1 = context.getBean(Test1.class);
test1.sayHello(); // 출력: Hello from Test1!
}
}
✅ @ComponentScan 덕분에 Test1이 자동으로 Bean 등록됨!
✅ 따로 @Bean을 지정해 줄 필요가 없음!
2️⃣ @Bean이 필요한 경우는 언제일까?
✅ 만약 클래스에 @Component를 붙일 수 없는 경우 (@Component 없이 수동으로 등록할 때)
✅ Bean 이름을 바꾸거나, 특정 객체를 직접 생성해서 등록해야 할 때
✅ MyBean 클래스 (자동 등록 X, @Bean 필요!)
package beans;
public class MyBean { // @Component 없음 → 자동 등록되지 않음!
public void doSomething() {
System.out.println("Doing something...");
}
}
✅ AppConfig에서 @Bean을 사용하여 수동 등록
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() { // @ComponentScan이 없으므로 직접 Bean 등록
return new MyBean();
}
}
✅ 실행 코드 (Main.java)
ApplicationContext context = new AnnotationConfigApplicationContext(config.AppConfig.class);
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething(); // 출력: Doing something...
✅ MyBean 클래스는 @Component가 없으므로 자동으로 등록되지 않음.
✅ 대신, @Bean을 사용해서 직접 Spring 컨테이너에 등록해야 함.
🔥 결론
✅ @ComponentScan을 사용하면 @Component가 붙은 클래스는 자동으로 Bean 등록됨 → @Bean 필요 없음
✅ @Component가 없는 클래스는 @Bean을 사용해서 직접 등록해야 함
✅ 특정 Bean 이름을 지정하거나, 직접 생성해야 할 경우 @Bean을 사용해야 함
728x90
'개발자 공부 > ♡ Spring XML' 카테고리의 다른 글
Spring MVC와 JSP를 이용 (0) | 2025.02.04 |
---|---|
@Autowired, @Qualifier("이름"), @Resource(name="이름") (0) | 2025.02.04 |
Spring의 의존성 주입(DI) 4가지 방식 [ @Autowired, 생성자, 컬렉션, Setter ] (1) | 2025.02.03 |
Spring XML 설정으로 [ Constructor DI(생성자 주입) ] (0) | 2025.02.03 |
Spring Bean의 라이프사이클 [ init-method와 destroy-method ] (0) | 2025.02.03 |