본문 바로가기
⚙️Backend/Spring

[Spring] DI (Dependency Injection) part 4

by Bㅐ추 2020. 5. 30.
728x90
반응형

 

2020/05/30 - [🌎Web Application/Spring] - [Spring] DI (Dependency Injection) part 1

2020/05/30 - [🌎Web Application/Spring] - [Spring] DI (Dependency Injection) part 2

2020/05/30 - [🌎Web Application/Spring] - [Spring] DI (Dependency Injection) part 3

 

스프링에서는 'ApplicationContext'라는 존재가 필요한 객체들을 생성하고 필요한 객체들을 주입하는 역할을 해주는 구조이다. 이때 ApplicationContext가 관리하는 객체들을 'Bean'이라 부르고 빈과 빈 사이의 의존관계를 처리하는 방식으로 XML방식과 Annotation방식 등이 있다. 

 

1. XML 방식

 

beans.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 스프링이 bean을 생성해서 관리할 수 있겠금, 설정을 해준다. -->
<!-- 스프링이 bean을 준비할 수 있다. -->
	<bean id="korean" class="com.test03.MessageBeanKo"></bean>
	<bean id="english" class="com.test03.MessageBeanEn"></bean>
	
</beans>

main.java

ApplicationContext factory = new ClassPathXmlApplicationContext("com/test03/beans.xml");

- ClassPathXmlApplicationContext : 지정된 classpath에서 xml파일을 읽어 생성

MessageBean bean = (MessageBean)factory.getBean("korean");
bean.sayHello("스프링");
bean = (MessageBean)factory.getBean("english");
bean.sayHello("Spring");

 

이미 스프링이 만들어서 준비해놓은 Bean을 우리가 interface MessageBean 에 주입을 시킨 것이다.

 지금까지는 매번 객체가 필요할 때마다 우리가 new연산자로 생성해서 사용을 했지만,

이제는 스프링이 bean이라는 이름으로 객체를 미리 준비시켜서

사용자가 필요할 때마다, 스프링이 생성한 bean을 getBean을 통해 사용할 수 있게 함. => 주입하였다. DI

 

<bean>태그에 사용되는 속성들

id 빈 객체의 고유 이름. id를 이용해 빈에 접근
name 객체의 별칭
class 생성할 클래스. 패키지의 이름까지 입력.
constructor-arg 생성자를 이용해 값을 주입할 때 사용.
property setter를 이용해 값을 주입할 때 사용.

 

( 1-1 ) Construcotr-arg

 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- 아무런 설정을 해주지 않으면, 기본 생성자로 bean이 생성된다. -->
	<bean id="banana" class="com.test01.MessageBeanImpl"></bean>
	
	<bean id="apple" class="com.test01.MessageBeanImpl">
		<!-- 생성자를 이용해서 객체를 생성될 때 데이터를 집어 넣겠다. 이때 순서는 매개변수 순서가 같아야한다. -->
		<constructor-arg>
			<value>apple</value>
		</constructor-arg>
		<constructor-arg value="10000"></constructor-arg>
	</bean>
	
	<bean id="mango" class="com.test01.MessageBeanImpl">
		<!-- index 컬럼으로 매개변수 순서를 지정해줄 수 있다. -->
		<constructor-arg index="1" value="15000"></constructor-arg>
		<constructor-arg  index="0" value="mango"></constructor-arg>
	</bean>
	
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="lee" class="com.test02.Address">
		<!-- name속성은 parameter의 이름과 같아야 한다. -->
		<constructor-arg name="name" value="이순신" ></constructor-arg>
		<constructor-arg  name="addr" value="서울시 강남구" ></constructor-arg>
		<constructor-arg name="phone" value="010-1111-1111" ></constructor-arg>
	</bean>
	
	<!-- id: hong, name: 홍길동, addr:경기도 화성시, phone:010-2222-2222 -->
	<bean id="hong" class="com.test02.Address">
		<constructor-arg index="0" value="홍길동"></constructor-arg>
		<constructor-arg index="1" value="경기도 화성시"></constructor-arg>
		<constructor-arg index="2" value="010-2222-2222"></constructor-arg>
	</bean>

</beans>

 

( 1-2 ) Property

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 이번에는 constructor 대신에 , setter로! -->	
	<bean id="lee" class="com.test03.Address">
		<property name="name" value="이순신"></property>
		<property name="addr" value="서울시 강남구"></property>
		<property name="phone" value="010-1111-1111"></property>
	</bean>
	
	<bean id="hong" class="com.test03.Address">
    <!-- name은 parameter의 이름과 같게 ! -->
		<property name="name" value="홍길동"></property>
		<property name="addr" value="경기도 화성시"></property>
		<property name="phone" value="010-2222-2222"></property>
	</bean>
	
</beans>

( 1-3 ) 생성자와 setter를 혼용

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="cass" class="com.test03.Developer">
		<constructor-arg index="0" value="카스"></constructor-arg>
		<constructor-arg index="1" value="2500000"></constructor-arg>
		<property name="dept" value="개발"></property>
	</bean>
	
	<bean id="terra" class="com.test03.Engineer">
		<constructor-arg name="name" value="테라"></constructor-arg>
		<constructor-arg name="salary" value="2500000"></constructor-arg>
		<property name="dept" value="개발"></property>
	</bean>
	
</beans>

 

( 1-4 ) 객체 참조

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- value 에 다른 bean를 어떻게 넣을까? -->
	
	<!-- Emp 클래스 bean 생성 -->
	<!-- 1. 이순신 3000000 2. 홍길동 3500000 -->
	<bean id="lee" class="com.test04.Emp">
		<property name="name" value="이순신"></property>
		<property name="salary" value="3000000"></property>
	</bean>
	
	<bean id="hong" class="com.test04.Emp">
		<property name="name" value="홍길동"></property>
		<property name="salary" value="3500000"></property>
	</bean>
	
	<!-- 객체는 ref="id"로 >>참조<<한다. -->
	<bean id="less" class="com.test04.Developer">
		<constructor-arg index="0" ref="lee"></constructor-arg>
		<constructor-arg index="1" value="개발"></constructor-arg>
	</bean>
	
	<bean id="honggd" class="com.test04.Engineer">
		<constructor-arg index="0" ref="hong"></constructor-arg>
		<constructor-arg index="1" value="기술"></constructor-arg>
	</bean>
	
	
</beans>

( 1-5 ) Namespace p or c

<?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:c="http://www.springframework.org/schema/c"
	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">

	<!-- c: constructor -->
	<bean id="kim" class="com.test05.Emp" c:name="김갑수" c:salary="3000000" />
	<bean id="kimgap" class="com.test05.Developer" c:emp-ref="kim" c:dept="개발팀"></bean>
	
	<!-- p: property -->
	<bean id="yu" class="com.test05.Emp" p:name="유리" p:salary="3500000" />
	<bean id="yuli" class="com.test05.Engineer" p:emp-ref="yu" p:dept="기술팀"></bean>
	
</beans>

 

728x90
반응형