본문 바로가기

Web[웹]/Spring Framework

[Spring] 어노테이션을 이용한 스프링 설정

 

xml파일을 Java파일로 변경하기

 

기존에 xml에서 스프링 설정파일을 제작했던 것을 뒤이어, 이번엔 Java 파일을 이용해서 설정파일을 만들어 볼 것인데, 이 때 어노테이션을 사용할 것이다.

 

<!-- applicationContext.xml -->
<bean id="studentDao" class="ems.member.dao.StudentDao" ></bean>
	
<bean id="registerService" class="ems.member.service.StudentRegisterService">
	<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
	
<bean id="modifyService" class="ems.member.service.StudentModifyService">
	<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
	
<bean id="deleteService" class="ems.member.service.StudentDeleteService">
	<constructor-arg ref="studentDao" ></constructor-arg>
</bean>

<bean id="selectService" class="ems.member.service.StudentSelectService">
	<constructor-arg ref="studentDao" ></constructor-arg>
</bean>

<bean id="allSelectService" class="ems.member.service.StudentAllSelectService">
	<constructor-arg ref="studentDao" ></constructor-arg>
</bean>

<bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
	<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
	<property name="userId" value="scott" />
	<property name="userPw" value="tiger" />
</bean>
	
<bean id="dataBaseConnectionInfoReal" class="ems.member.DataBaseConnectionInfo">
	<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.0.1:1521:xe" />
	<property name="userId" value="masterid" />
	<property name="userPw" value="masterpw" />
</bean>
	
<bean id="informationService" class="ems.member.service.EMSInformationService">
	<property name="info">
		<value>Education Management System program was developed in 2015.</value>
	</property>
	<property name="copyRight">
		<value>COPYRIGHT(C) 2015 EMS CO., LTD. ALL RIGHT RESERVED.</value>
	</property>
	<property name="ver">
		<value>The version is 1.0</value>
	</property>
	<property name="sYear">
		<value>2015</value>
	</property>
	<property name="sMonth">
		<value>1</value>
	</property>
	<property name="sDay">
		<value>1</value>
	</property>
	<property name="eYear" value="2015" />
	<property name="eMonth" value="2" />
	<property name="eDay" value="28" />
	<property name="developers">
		<list>
			<value>Cheney.</value>
			<value>Eloy.</value>
			<value>Jasper.</value>
			<value>Dillon.</value>
			<value>Kian.</value>
		</list>
	</property>
	<property name="administrators">
		<map>
			<entry>
				<key>
					<value>Cheney</value>
				</key>
				<value>cheney@springPjt.org</value>
			</entry>
			<entry>
			<key>
					<value>Jasper</value>
				</key>
				<value>jasper@springPjt.org</value>
			</entry>
		</map>
	</property>
	<property name="dbInfos">
		<map>
			<entry>
				<key>
					<value>dev</value>
				</key>
				<ref bean="dataBaseConnectionInfoDev"/>
			</entry>
			<entry>
				<key>
					<value>real</value>
				</key>
				<ref bean="dataBaseConnectionInfoReal"/>
			</entry>
		</map>
	</property>
</bean>

 

위 파일이 바로 xml로 짜여진 스프링 설정 파일인데, 위 코드를 하나하나 매칭해가면서 변환시켜 보자.

 

스프링 설정 파일로 사용할 클래스를 하나 만들어 준 후, 클래스 제일 위에 "@Configuration"을 선언해준다. 이 선언문이 있으면 스프링 설정 파일로 사용이 가능하다.

 

@Configuration
public class MemberConfig {
}

 

이제 xml 파일을 한번 변경해보면 다음과 같이 할 수 있다.

 

//<bean id="studentDao" class="ems.member.dao.StudentDao" ></bean>
@Bean
public StudentDao studentDao() {
    return new StudentDao();
}

 

 

또 이 역시 빈 객체로 만들어 질 것이라는 "@Bean" 어노테이션을 명시해줘야 한다. 

 

이번엔 <contructor-arg> 관계에 있는 것을 어떻게 나타내는지 보자.

 

/*
<bean id="registerService" class="ems.member.service.StudentRegisterService">
	<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
*/
@Bean
public StudentRegisterService registerService() {
    return new StudentRegisterService(studentDao());
}

 

 

여러 property 값이 있는 값은 다음과 같이 나타내면 된다.

 

/*
<bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
	<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
	<property name="userId" value="scott" />
	<property name="userPw" value="tiger" />
</bean>
*/
@Bean
public DataBaseConnectionInfo dataBaseConnectionInfoDev() {
    DataBaseConnectionInfo infoDev = new DataBaseConnectionInfo();
    infoDev.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:xe");
    infoDev.setUserId("scott");
    infoDev.setUserPw("tiger");
		
    return infoDev;
}

 

 

value, key 쌍으로 나온 Map 데이터는 다음과 같이 나타낸다.

 

/*
<bean id="informationService" class="ems.member.service.EMSInformationService">
	<property name="info">
		<value>Education Management System program was developed in 2015.</value>
	</property>
	<property name="copyRight">
		<value>COPYRIGHT(C) 2015 EMS CO., LTD. ALL RIGHT RESERVED.</value>
	</property>
	<property name="ver">
		<value>The version is 1.0</value>
	</property>
	<property name="sYear">
		<value>2015</value>
	</property>
	<property name="sMonth">
		<value>1</value>
	</property>
	<property name="sDay">
		<value>1</value>
	</property>
	<property name="eYear" value="2015" />
	<property name="eMonth" value="2" />
	<property name="eDay" value="28" />
	<property name="developers">
		<list>
			<value>Cheney.</value>
			<value>Eloy.</value>
			<value>Jasper.</value>
			<value>Dillon.</value>
			<value>Kian.</value>
		</list>
	</property>
	<property name="administrators">
		<map>
			<entry>
				<key>
					<value>Cheney</value>
				</key>
				<value>cheney@springPjt.org</value>
			</entry>
			<entry>
			<key>
					<value>Jasper</value>
				</key>
				<value>jasper@springPjt.org</value>
			</entry>
		</map>
	</property>
	<property name="dbInfos">
		<map>
			<entry>
				<key>
					<value>dev</value>
				</key>
				<ref bean="dataBaseConnectionInfoDev"/>
			</entry>
			<entry>
				<key>
					<value>real</value>
				</key>
				<ref bean="dataBaseConnectionInfoReal"/>
			</entry>
		</map>
	</property>
</bean>
*/

@Bean
public EMSInformationService informationService() {
    EMSInformationService info = new EMSInformationService();
    info.setInfo("Education Management System program was developed in 2015.");
    info.setCopyRight("COPYRIGHT(C) 2015 EMS CO., LTD. ALL RIGHT RESERVED.");
    info.setVer("The version is 1.0");
    info.setsYear(2015);
    info.setsMonth(1);
    info.setsDay(1);
    info.seteYear(2015);
    info.seteMonth(2);
    info.seteDay(28);
		
    ArrayList<String> developers = new ArrayList<String>();
    developers.add("Cheney.");
    developers.add("Eloy.");
    developers.add("Jasper.");
    developers.add("Dillon.");
    developers.add("Kian.");
    info.setDevelopers(developers);
		
    Map<String, String> administrators = new HashMap<String, String>();
    administrators.put("Cheney", "cheney@springPjt.org");
    administrators.put("Jasper", "jasper@springPjt.org");
    info.setAdministrators(administrators);
		
    Map<String, DataBaseConnectionInfo> dbInfos = new HashMap<String, DataBaseConnectionInfo>();
    dbInfos.put("dev", dataBaseConnectionInfoDev());
    dbInfos.put("real", dataBaseConnectionInfoReal());
    info.setDbInfos(dbInfos);
		
    return info;
}

 

이제 이를 Main에서 스프링 설정 파일을 받아올때는 다음과 같이 해준다.

 

//GenericXmlApplicationContext ctx = 
//				new GenericXmlApplicationContext("classpath:/*xml파일명*/");
AnnotationConfigApplicationContext ctx = 
				new AnnotationConfigApplicationContext(/*class파일명*/);