단순 코드 기록/Spring Boot

SB_JavaConfig와 Component-Scan

일일일코_장민기 2024. 2. 26. 13:30
728x90
Component-Scan: @Controller / @Service / @Repository / @Component
JavaConfig: @Configuration + @Bean("아이디")
- Component-Scan + Autowired 사용하면 JavaConfig 역할 대체 가능

 

 

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;

import com.example.dao.DBOracleDAO;
import com.example.service.DBService;

@Configuration
public class JavaConfig {

//	@Bean								//dao에 @Repository로 대체
//	public DBOracleDAO dao() {
//		return new DBOracleDAO();
//	}

	@Bean("myService")
	public DBService service(DBOracleDAO xxx) {
		DBService service = new DBService();
		service.setDao(xxx);
		return service;
	}
	
}

 

 

JavaConfig, ComponentScan 없는 방식
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import com.example.dao.DBMYSQLDAO;
import com.example.dao.DBOracleDAO;
import com.example.service.DBService;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Application.class, args);

		DBService serv = new DBService();
		DBOracleDAO oDAO = new DBOracleDAO();
		serv.setDao(oDAO);
		System.out.println(serv.list());
		
		DBService serv2 = new DBService();
		DBMYSQLDAO mDAO = new DBMYSQLDAO();
		serv2.setDao(mDAO);
		System.out.println(serv2.list());
		
		
		
	
	
	
	}

}

'단순 코드 기록 > Spring Boot' 카테고리의 다른 글

SB_Properties(@Values)  (0) 2024.02.26
SB_서브 패키지  (0) 2024.02.26
SB_로그 환경 설정  (0) 2024.02.21