DEVLOG

어노테이션을 이용한 스프링 설정 - 1 본문

Spring

어노테이션을 이용한 스프링 설정 - 1

BINTHEWORLD 2022. 6. 8. 11:53

어노테이션을 이용하여 스프링 설정파일을 applicationContext.xml 파일 대신 .java파일을 스프링 설정파일로 사용할 수 있다.

.xml -> .java 변환방법

@configuration 어노테이션 (스프링 설정파일로 사용하겠다는 의미)과 @Bean 어노테이션(빈 객체로 사용하겠다는 의미)을 사용하며, 메소드 반환형은 .xml의 빈(Bean) 객체의 데이터타입, 메소드명은 빈(Bean) 객체의 id로 지정하면 된다.

applicationContext.xml 

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

↓ MemberConfig.java로 변환

	@Bean
	// 메소드 반환형 : xml의 빈(Bean) 객체의 데이터타입, 메소드명 : 빈(Bean) 객체의 id 
	public StudentRegisterService registerService() {
		// 생성자 파라미터에는 .xml 빈(Bean) 객체의 constructor-arg ref의 값이 할당됨
		return new StudentRegisterService(studentDao()); 
	}

1. property 변환

해당 데이터타입의 객체 생성 후, setPropery() 메소드로 객체의 프로퍼티를 추가하면 된다.

applicationContext.xml 

	<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>

↓ MemberConfig.java로 변환

	@Bean
	public DataBaseConnectionInfo dataBaseConnectionInfoReal() {
		DataBaseConnectionInfo infoReal = new DataBaseConnectionInfo(); // 객체생성 먼저
		infoReal.setJdbcUrl("jdbc:oracle:thin:@192.168.0.1:1521:xe");
		infoReal.setUserId("masterid");
		infoReal.setUserPw("masterpw");
		
		return infoReal; // 객체 리턴
	}

2. property 변환 중 List태그 변환

ArrayList<>() 이용

applicationContext.xml 

	<bean id="informationService" class="ems.member.service.EMSInformationService">
		...
		<property name="developers">
			<list>
				<value>Cheney.</value>
				<value>Eloy.</value>
				<value>Jasper.</value>
				<value>Dillon.</value>
				<value>Kian.</value>
			</list>
		</property>
        	...
	</bean>

↓ MemberConfig.java로 변환

	@Bean
	public EMSInformationService informationService() {
    		...
        	ArrayList<String> developers = new ArrayList<String>(); // ArrayList<자료형> property name명 : <list> 태그
		developers.add("Cheney."); // add("<list> 태그의 value값")
		developers.add("Eloy.");
		developers.add("Jasper.");
		developers.add("Dillon.");
		developers.add("Kian.");
		info.setDevelopers(developers);
		...
       		return info; // 객체 리턴
	}

3. property 변환 중 Map태그 변환

applicationContext.xml 

	<bean id="informationService" class="ems.member.service.EMSInformationService">
		...
		<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>
        	...
	</bean>

EMSInformationService.java 

	public Map<String, String> getAdministrators() {
		return administrators;
	}

	public void setAdministrators(Map<String, String> administrators) {
		this.administrators = administrators;
	}

↓ MemberConfig.java로 변환

	@Bean
	public EMSInformationService informationService() {
    		...
           	Map<String, String> administrators = new HashMap<String, String>(); // Map(Key, Value)구조
		administrators.put("Cheney", "cheney@springPjt.org");
		administrators.put("Jasper", "jasper@springPjt.org");
		info.setAdministrators(administrators);
        	...
       		return info; // 객체 리턴
	}

4. property 변환 중 제네릭이 객체일 경우 Map태그 변환

applicationContext.xml 

	<bean id="informationService" class="ems.member.service.EMSInformationService">
		...
		<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>

EMSInformationService.java 

	public Map<String, DataBaseConnectionInfo> getDbInfos() {
		return dbInfos;
	}

	public void setDbInfos(Map<String, DataBaseConnectionInfo> dbInfos) {
		this.dbInfos = dbInfos;
	}

↓ MemberConfig.java로 변환

	@Bean
	public EMSInformationService informationService() {
    		...
		Map<String, DataBaseConnectionInfo> dbInfos = new HashMap<String, DataBaseConnectionInfo>();
		dbInfos.put("real", dataBaseConnectionInfoReal()); //dataBaseConnectionInfoReal 객체 리턴해주는 메소드를 value 값으로 설정
		info.setDbInfos(dbInfos);
        	...
       		return info; // 객체 리턴
	}

 

출처

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%94%84%EB%A0%88%EC%9E%84%EC%9B%8C%ED%81%AC_renew 

 

자바 스프링 프레임워크(renew ver.) - 신입 프로그래머를 위한 강좌 - 인프런 | 강의

스프링 프레임워크 기본부터 실전 사용법까지! 충실하고 폭넓은 설명과 예제를 통해 현장에 바로 투입되어 활약하는 개발자로 거듭나세요., - 강의 소개 | 인프런...

www.inflearn.com

 

'Spring' 카테고리의 다른 글

웹 프로그래밍 설계 모델  (0) 2022.06.08
어노테이션을 이용한 스프링 설정 - 2  (0) 2022.06.08
생명주기(Life Cycle)  (0) 2022.06.07
의존객체 선택  (0) 2022.06.07
의존객체 자동 주입  (0) 2022.06.07
Comments