사용자 도구

사이트 도구


spring:home

문서의 이전 판입니다!


Spring

DI(Dependency Injection)와 IOC컨테이너

  1. DI(Dependency Injection) Dependency을 외부에서 만들어 넣어줌
  2. IOC컨테이너

사실 작은 규모의 프로젝트에서는 스프링의 DI사용을 하는 것 보다 일반적인 방법을 사용하여 개발하는 것이 더욱 빠르고, 개발에 따른 스트레스를 줄일 수 있습니다. 하지만 규모가 어느 정도 커지고, 추후 유지보수 업무가 발생시에는 DI를 이용한 개발의 장점을 느낄 수 있습니다.

Spring을 이용한 객체 생성과 조립

public static void main(String[] args) {
//		MyCalculator cal = new MyCalculator();
//		cal.setFirstNum(10);
//		cal.setSecondNum(2);
//		cal.add();
//		cal.sub();
//		cal.multi();
//		cal.divide();
  String configLocation = "classpath:applicationCTX.xml";
  AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLocation);
  MyCalculator myCalculator = ctx.getBean("myCalculator", MyCalculator.class);
  myCalculator.add();
  myCalculator.sub();
  myCalculator.multi();
  myCalculator.divide();
}
<?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">
 
	<!-- Root Context: defines shared resources visible to all other web components -->
 
	<bean id="calculator" class="com.taekgu.ex.Calculator"/>
	<bean id="myCalculator" class="com.taekgu.ex.MyCalculator">
		<property name="calculator">
			<ref bean="calculator"/>
		</property>
		<property name="firstNum" value="10"/>
		<property name="secondNum" value="2"/>
	</bean>
</beans>

Spring Property

Spring Container Life cycle

public class Student implements InitializingBean, DisposableBean {
.....
 
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet()");
	}
 
	@Override
	public void destroy() throws Exception {
		System.out.println("destroy()");
	}
}

Spring Bean Scope

	<bean id="otherStudent" class="com.taekgu.ex.OtherStudent"
		init-method="initMethod" destroy-method="destoryMethod"
		scope="prototype|singleton">
		<constructor-arg value="홍길동"/>
		<property name="age">
			<value>10</value>
		</property>
		<property name="hobbys">
			<list>
				<value>Game</value>
				<value>Reading</value>
			</list>
		</property>
	</bean>

외부파일을 이용한 설정

  • Environment객체를 이용해서 스프링 빈 설정을 합니다.
  • Environment객체를 사용하지 않고 프로퍼티 파일을 직접 이용하여 스프링 빈을 설정하는 방법에 대해서 살펴 봅니다.
  • 동일한 스프링 빈을 여러 개 만들어 놓고 상황(환경)에 따라서 적절한 스프링 빈을 사용할 수 있습니다. profile 속성을 사용하면 됩니다.
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- Root Context: defines shared resources visible to all other web components -->
 
	<context:property-placeholder location="classpath:admin.properties, classpath:sub_admin.properties" />
	<bean id="calculator" class="com.taekgu.ex.Calculator"/>
	<bean id="myCalculator" class="com.taekgu.ex.MyCalculator">
		<property name="calculator">
			<ref bean="calculator"/>
		</property>
		<property name="firstNum" value="10"/>
		<property name="secondNum" value="2"/>
	</bean>
	<bean id="otherStudent" class="com.taekgu.ex.OtherStudent"
		init-method="initMethod" destroy-method="destoryMethod"
		scope="prototype">
		<constructor-arg value="${adminUserName}"/>
		<property name="age">
			<value>10</value>
		</property>
		<property name="hobbys">
			<list>
				<value>Game</value>
				<value>Reading</value>
			</list>
		</property>
	</bean>
</beans>
spring/home.1535894408.txt.gz · 마지막으로 수정됨: 2025/04/15 10:05 (바깥 편집)