@Configuration 해주냐 안해주느냐의 차이

govlmo91·2019년 6월 29일
0

토비의 스프링

목록 보기
3/6

CountingConnectionMaker


public class CountingConnectionMaker implements ConnectionMaker{
	int counter = 0;
	private ConnectionMaker realConnectionMaker;
	
	public CountingConnectionMaker(ConnectionMaker connectionMaker) {
		this.realConnectionMaker = connectionMaker;
	}

	public Connection makeConnection() throws ClassNotFoundException, SQLException {
		this.counter++;
		return realConnectionMaker.makeConnection();
	}

	public int getCounter() {
		return counter;
	}
}

CountungDaoFactory


@Configuration
public class CountingDaoFactory {
	@Bean
	public UserDao userDao() {
		return new UserDao(connectionMaker());
	}
	
	@Bean
	public ConnectionMaker connectionMaker() {
		return new CountingConnectionMaker(realConnectionMaker());
	}
	
	@Bean
	public ConnectionMaker realConnectionMaker() {
		return new ImplementsConnectionMaker();
	}
}

CountungConnectionTest Class


public class CountingConnectionTest {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
//		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CountingDaoFactory.class);
		ApplicationContext context = new AnnotationConfigApplicationContext(CountingDaoFactory.class);
		UserDao dao = context.getBean("userDao", UserDao.class);
		UserDao dao2 = context.getBean("userDao", UserDao.class);
    
		//DAO사용코드.
		User user = dao.get("judah123");
		User user2 = dao.get("judah123");
//		System.out.println(user);
		
		CountingConnectionMaker ccm = context.getBean("connectionMaker", CountingConnectionMaker.class);
		System.out.println(ccm.getCounter());
	}
}

@Configuration ?
이 어노테이션은 컨테이너만들 때 참고할 설정정보다 라고 명시해주는 걸로 알고있음.
근데 저 어노테이션 안붙여줘도 Test에서 user정보를 잘 갖고오고, (dao == dao2) ? true 인걸로 봐서는 AC에 등록된 Bean인 게 확실해보이는데..
@Configuration를 안붙여주면 count가 되지 않는 건지?

profile
안녕하세요!

0개의 댓글