[ 개념 ]
- 빈으로 등록된 클래스들을 스캔해서 빈으로 등록해주는 것
- @Component
- @Controller
- @Service
- @Repository
[ 사용 방법 ]
1. xml
<!-- applicationContext.xml -->
<context:component-scan base-package="com.ssafy.model"></context:component-scan>
// GuestBookController.java
**ApplicationContext context = new ClassPathXmlApplicationContext("com/ssafy/configuration/applicationContext.xml");**
GuestBookService guestBookService = context**.getBean(**"gbService", GuestBookServiceImpl.class**)**;
2. java
// ApplicationConfig.java
**@Configuration**
**@ComponentScan(basePackages = {"com.ssafy.model"})**
public class ApplicationConfig {
@Bean
public DataSource getDataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass(com.mysql.cj.jdbc.Driver.class);
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/ssafyweb?serverTimezone=UTC&useUniCode=yes&characterEncoding=UTF-8");
dataSource.setUsername("ssafy");
dataSource.setPassword("ssafy");
return dataSource;
}
}
// GuestBookController.java
**ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);**
GuestBookService guestBookService = context**.getBean(**"gbService", GuestBookServiceImpl.class**)**;