spring container

Edit

Inversion of control 페이지 참조할 것

ApplicationContext가 BeanFactory보다 좋은점

빈정보 접근

생명주기

설정

Annotation 기반

DI관련 JSR

Scope

singleton, prototype, request, session, global-session

설정 샘플

퀴즈풀기

<bean id="info" class="a.b.c.Info" p:dao-ref="mydao" scope="prototype" />
<bean id="mydao" class="a.b.c.MyDao" />

Spring container API

Spring DI sample

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.SimpleJobRepository">
  <constructor-arg ref="mapJobInstanceDao" />
  <constructor-arg ref="mapStepExecutionDao" />
</bean>

<bean id="stockPremiunContentsBuildJob" parent="simpleJob">
  <property name="steps">
    <ref bean="recommendationBuildStep"/>
  </property>
</bean>
<constructor-arg><value>10</value></constructor-arg>
<constructor-arg><value type="long">10</value></constructor-arg>
<constructor-arg value="10"/>

List

<list value-type="java.lang.Double"></list>

<list>
  <ref bean="testStep"/>
  <bean class="test.testStep"/>
</list>

Map

<map>
  <entry>
    <key><value>test</value></key>
    <ref bean="testJob"/>
  </entry>
</map>

<map key-type="java.lang.Integer" value-type="java.lang.Double">
  <entry key="1" value="0.11"/>
</map>

Property

<properites>
  <prop key="name">jsh<prop>
</properties>

Set

<set value-type="java.lang.Integer">
  <value>1</value>
</set>

네임스페이스 활용

Factory

<bean id="exampleBean" class="examples.ExampleBean2" factory-method="createInstance"/>

<bean id="myFactoryBean" class="..."/>
<bean id="exampleBean" factory-bean="myFactoryBean" factory-method="createInstance"/>

AutoWire

<bean id="job" class="test.testJob" autowire="byName"/>

Spring annotation 기반 설정

@Required

@Autowired

@Autowired
@Qualifier("main")

빈객체의 수식어는 <qualifer>태그를 이용해 지정.

<bean>
   <qualifier value="main'/>
</bean>
@Autowired
publid void init(@Qulifier("testJob") Job job, Task task)

@Resource

@PostConstruct @PreDestroy

@Component

<context:component-scan
    base-package="com.nhncorp.smon.filemgr.action">
   <context:include-filter type="regex" expression=".*TestJob"/>
   <context:exclude-filter type="aspectj" expression="..*Task"/>
<context:component-scan>
@Component("testJob")
@Scope("prototype")
@Component
<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">

Spring context event

Spring customEditor

<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors">
    <map>
      <entry key="int[]">
        <bean class="org.springframework.batch.support.IntArrayPropertyEditor" />
      </entry>
      <entry key="org.springframework.batch.item.file.transform.Range[]">
        <bean class="org.springframework.batch.item.file.transform.RangeArrayPropertyEditor" />
      </entry>
      <entry key="java.util.Date">
        <bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
          <constructor-arg>
            <bean class="java.text.SimpleDateFormat">
              <constructor-arg value="yyyyMMdd" />
            </bean>
          </constructor-arg>
          <constructor-arg value="false" />
        </bean>
      </entry>
    </map>
  </property>
</bean>

Spring message

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basename" value="message.error"/>
</bean>

Bean객체에서 사용하기