Programming/Spring

Spring (3) - Container에 객체(Bean) 설정하기1 : 프로젝트 생성

코딩하는 포메라니안 2022. 4. 22. 17:21

Container에 객체를 설정하는 방법을 살펴보기 앞서 테스트를 위한 프로젝트를 먼저 생성해야 한다.

 

Spring Boot가 아닌 일반 Spring Project는 [Spring Legacy Project]로 만든다.

하지만, '여기서는 Web을 만들지 않아도 되기 때문에, MVC패턴이 없는 Spring Project를 썼다.

굳이 쓰지 않는 파일까지 만들지 않고 보기 편하게 하기 위함이다.

 

 

 

1. Java Project 생성

 

 

 

 

2. Maven Project로 전환

Spring에서 필요한 library들을 Maven으로 쉽게 받아오기 위해, 전환한다.

생성한 Java Project에 마우스 우클릭 > Configure > Conver to Maven Project > Finish (수정 필요 X)

 

 

 

 

3. pom.xml에 (라이브러리)의존 관계 설정하기

아래 사이트에서 코드를 복사한 후, <dependencies></dependencies>를 만들어 태그 사이에 붙여넣는다.

의존 관계들은 이제 이 태그 안에 모두 작성할 것이다.

https://mvnrepository.com/artifact/org.springframework/spring-context/5.3.19

 

저장을 하면 context 뿐만 아니라 이와 의존 관계에 있는 라이브러리까지 함께 받아온 것을 확인할 수 있다.

여기서 의존 관계란 context 내부적으로 다음과 같은 라이브러리들에 있는 요소가 쓰인다는 것이다.

 

 

여기까지하면, Spring library를 쓰는 세팅이 끝난다.

 

 

 

4. test에 필요한 객체 생성하기

전체 구조는 아래와 같고, 각 클래스는 테스트를 위해 기본적인 메서드로만 구성했다.

 

각 클래스의 내용은 아래와 같으며, 설명이 없는 파일은 변경사항이 없는 것이다.

 

 

1) TestDao.java : interface

package com.test.di1;

public interface TestDao {
	public void testPrint();
}

 

2) TestDaoImpl1.java

package com.test.di1;

public class TestDaoImpl1 implements TestDao {

	public TestDaoImpl1() {
		System.out.println("DAO1 생성자!");
	}

	public void init() {
		System.out.println("DAO1 초기화!");
	}
	
	@Override
	public void testPrint() {
		System.out.println("저는 DAO1입니다.");
	}
}

 

3) TestDaoImpl2.java

package com.test.di1;

public class TestDaoImpl2 implements TestDao {

	public TestDaoImpl2() {
		System.out.println("DAO2 생성자!");
	}
	
	@Override
	public void testPrint() {
		System.out.println("저는 DAO2입니다.");
	}
}

 

4) TestService.java

package com.test.di1;

public class TestService {
	
	private TestDao td1 = new TestDaoImpl1(); 
	private TestDao td2 = new TestDaoImpl2(); 
	
	public TestService() {
		System.out.println("TestService 생성자!");
	}
	
	public void TestPrint() {
		td1.testPrint();
		td2.testPrint();
	}
}

 

5) TestMain.java

마지막에 이 파일을 실행시켜, 정상적으로 동작하는지 확인할 것이다.

package com.test.di1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {

	public static void main(String[] args) {
        
		//1. Container 객체 생성 <= 만든 설정 파일을 인자로 줌
		ApplicationContext context = new ClassPathXmlApplicationContext("com/test/di1/application.xml"); //xml은 파일이라서 '/'로 구분
		
		//2. Container에서 객체를 받아와서 사용
		System.out.println("********");
		TestService testService1 = context.getBean("ts", TestService.class); //id가 ts인 객체를 Object로 받아와서, TestService형으로 형변환
		TestService testService2 = context.getBean("ts", TestService.class);
		testService1.TestPrint();
		System.out.println("ts1 : "+testService1.hashCode() + " == ts2 : "+testService2.hashCode()); //기본이 singleton패턴이므로 같은 객체를 반환한다.
	}
}

 

 


 

 

Container에 객체를 등록하는 방법은 크게 3가지가 있다.

이 3가지 방법은 섞어 사용될 수 있으며, 주로 XML과 Annotation을 함께 사용한다.

 

1) XML

2022.04.22 - [웹프로그래밍/Spring] - Spring (3) - Container에 객체(Bean) 설정하기2 : XML

 

2) Annotation

2022.04.23 - [웹프로그래밍/Spring] - Spring (3) - Container에 객체(Bean) 설정하기3 : Annotation

 

3) Java

2022.04.23 - [웹프로그래밍/Spring] - Spring (3) - Container에 객체(Bean) 설정하기4 : Java