Acorn academy 02/01 Spring

Bae Seong Jun·2024년 2월 1일

Acorn academy

목록 보기
43/70

기본생성자를 이용한 주입

  • 멤버변수 설정 없이 객체 생성

생성자를 이용한 주입

  • 존재하는 생성자의 인자명, 갯수와 일치하게 생성한다.
  • <constructor-arg name="" value=""/>
  • <constructor-arg name="" ref=""/>
  • 기본형은 value를 이용하여 입력.
  • 객체형은 ref를 이용하여 입력.
  • 객체형을 주입하기 위해서는 객체가 bean으로 생성되어야 한다.

기본생성자 + set 함수를 이용한 주입

  • 기본생성자로 생성하고 setXxx() 메서드를 이용하여 객체의 멤버변수를 설정하는 방식이다.
  • <property name="존재하는 세터함수 뒷부분 이름(대부분 멤버변수명)(xxx)" value="값"></property>
  • <property name="xxx"><value>값</value</property>
	<bean class="com.spring.EchoBean" id="echoBean">
		<property name="xxx" ref="anotherBean"></property>
	</bean>
	<bean class="com.spring.EchoBean" id="echoBean2">
		<property name="xxx">
			<ref bean="anotherBean2"></ref>
		</property>
	</bean>

p태그 사용

xml파일 -> namespace로 가서 -> p 체크후 저장
그럼 이제 p:태그를 사용할 수 있다.
p태그를 이용하여 멤버변수를 쉽게 설정할 수 있다.

  • id는 자동완성시 객체이름(소문자) 형태로 완성된다.
    xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- namespace: p 설정 -->
	<bean class="com.sprnig.Cat" id="cat" p:catName="야옹이" p:catAge="10"></bean>
	<bean class="com.sprnig.Person" id="person" p:username="홍길동" p:age="20" p:cat-ref="cat"></bean>
</beans>

bean을 이용한 Main - Service - DAO 패턴 숙지

  • xml 에서 dao객체와 service객체를 순서대로 생성후 메인에서 service로 setDao()를 통해서 dao객체 전달하여 사용

value로 list, map 등의 Object를 넘기는 방법

list

constructor-arg나 property 태그의 내부에 list태그를 이용하여 생성이 가능하다.

	<bean class="com.spring.EchoBean" id="echoBean">
		<constructor-arg name="valueList">
			<!-- list 주입 -->
			<list>
				<value>10</value>
				<value>20</value>
				<value>30</value>
			</list>
		</constructor-arg>
	</bean>

리스트의 인자로 객체가 오는 경우 + 프로퍼티는 추가적으로 사용이 가능

	<bean class="com.spring.AnotherBean" id="anotherBean1">
		<property name="name" value="test1"></property>
	</bean>
	<bean class="com.spring.AnotherBean" id="anotherBean2">
		<property name="name" value="test2"></property>
	</bean>
	<bean class="com.spring.AnotherBean" id="anotherBean3">
		<property name="name" value="test3"></property>
	</bean>
	
	<bean class="com.spring.EchoBean" id="echoBean">
		<constructor-arg name="anotherBean" ref="anotherBean1"></constructor-arg>
		<property name="valueList">
			<list>
				<ref bean="anotherBean1"/>
				<ref bean="anotherBean2"/>
				<ref bean="anotherBean3"/>
			</list>
		</property>
	</bean>

util 사용 (재활용이 가능한 리스트를 만들 수 있음)

namespace에서 util 체크후 사용 가능
리스트를 외부에서 만들고 id를 부여하여 여러곳에서 사용이 가능

	<bean class="com.spring.AnotherBean" id="anotherBean1">
		<property name="name" value="test1"></property>
	</bean>
	<bean class="com.spring.AnotherBean" id="anotherBean2">
		<property name="name" value="test2"></property>
	</bean>
	<bean class="com.spring.AnotherBean" id="anotherBean3">
		<property name="name" value="test3"></property>
	</bean>
	
	<util:list id="list">
		<ref bean="anotherBean1"/>
		<ref bean="anotherBean2"/>
		<ref bean="anotherBean3"/>
	</util:list>
	
	<bean class="com.spring.EchoBean" id="echoBean">
		<constructor-arg name="anotherBean" ref="anotherBean1"></constructor-arg>
		<property name="valueList" ref="list"></property>
	</bean>
	

map

    	<property name="mapCat">
    		<map>
    			<entry key="one" value-ref="pet01"></entry>
    			<entry key="two">
    				<ref bean="pet02"></ref>
    			</entry>
    		</map>
    	</property>
profile
코딩 프로?

0개의 댓글