#Bean
Spring Ioc Container에 의해 생성되고 관리되는 객체들을 의미
#BeanFactory
Bean 생성 및 의존성 주입, 생명주기 관리등 기능을 제공한다
#ApplicationContext
BeanFactory 인터페이스를 상속받음
BeanFactory가 제공하는 기능외에 여러가지 기능을 제공함
#bean 등록(xml)
<beans>
<bean id="" class="풀경로"/>
</beans>
#bean 이름
id가 중복되면 컴파일시 에러, name이 중복되면 런타임에러 발생
이름을 지정하지 않으면 오브젝트의 이름으로 생성함
#lazy-init 속성
"true" : was가 실행될때 빈을 읽어드림
"false" : 나중에 bean이 호출될 때 빈을 읽어드림
### 의존성 주입 ###
Constructor Injection과 Setter Injection 방식
#Constructor Injection
java -->
package x.y;
public class Foo{
public Foo(Bar bar, Baz baz){
//..
}
}
xml -->
<beans>
<bean name="foo" class="x.y.Foo">
<Constructor-args>
<bean class="x.y.Bar"/>
</Constructor-args>
<Constructor-args>
<bean class="x.y.Baz"/>
</Constructor-args>
</bean>
</beans>
-----------------------------------------------------------------------------------------------------------------------------------
java -->
package examples;
public class ExampleBean{
private int years;
private String ultimateAnswer;
public ExampleBean(int years, String ultimateAnswer){
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
xml -->
<!-- 타입지정 -->
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-args type="int" value="750000"/>
<constructor-args type="String" value="42"/>
</bean>
<!-- 순서지정 -->
or
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-args index="0" value="750000"/>
<constructor-args index="1" value="42"/>
</bean>
# Setter Injection
1. Setter에서의 의존성 주입은 <constructor-args>태그가 아닌 <property>태그를 사용
2. 메소드에 set을 빼고 첫글자를 소문자로 지정
java -->
package examples;
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne){
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo){
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i){
this.i = i;
}
}
xml -->
<bean id="AnotherExampleBean" class="examples.AnotherBean">
<bean id="YetAnotherBean" class="exampes.YetAnoterBean">
<bean id="exampleBean" class="examples.ExampleBean">
<property name="beanOne">
<ref bean="AnoterExampleBean"/>
</property>
<property name="beanTwo" ref="YetAnotherBean"/>
<property name="integerProperty" value="5"/>
</bean>
#p-namespace
기존-->
<bean name="join-classic" class="com.example.Person">
<property name="name" value="Jone Deo"/>
<property name="spouse" ref="jane"/>
</bean>
<bean name="jane" class="com.example.Person">
<proeprty name="name" value="Jane Doe"/>
</bean>
p.namespace -->
<bean name="join-modern" class="com.example.Person" p:name="Jone Deo" p:spouse:"jane"/>
<bean name="jane" class="com.example.Person">
<property name="name" value="Jane Doe"/>
</bean>