์คํ๋ง ์ปจํ
์ด๋๊ฐ ๊ด๋ฆฌํ๋ ์๋ฐ ๊ฐ์ฒด๋ฅผ ์๋ฏธํฉ๋๋ค.
๋น์ ์ธ์คํด์คํ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ฉฐ, ์คํ๋ง ์ปจํ
์ด๋์ ๋ฑ๋ก๋ ๊ฐ์ฒด๋ฅผ ์คํ๋ง ๋น์ด๋ผ๊ณ ํฉ๋๋ค.
<bean id="hello" class="com.example.Hello"/>
์ด๋ ๊ฒ xml ํ์ผ ๋น ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ฉด hello ์ธ์คํด์ค๋ฅผ ์คํ๋ง ์ปจํ
์ด๋๊ฐ ๊ด๋ฆฌํ๊ฒ ๋ฉ๋๋ค.
Hello hello = new Hello()
์ฆ new ํค์๋ ๋์ ์ฌ์ฉํ๋ ๊ฒ!
ApplicationContext๋ Spring Container์ ๊ตฌํ์ฒด ์ค ํ๋๋ก, ๊ฐ์ฒด์ ์์ฑ๊ณผ ์๋ฉธ ๋ผ์ดํ์ฌ์ดํด์ ๊ด๋ฆฌํฉ๋๋ค. ์ฆ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋น(Bean)๋ค์ ๊ด๋ฆฌํ๊ณ ํ์ํ ๊ฐ์ฒด๋ฅผ ์ ๊ณตํ๋ ์ญํ ์ ํฉ๋๋ค.
public interface MessageBean {
void sayHello(String name);
}
1. MessageBeanEnImpl
public class MessageBeanEnImpl implements MessageBean{
public MessageBeanEnImpl() {
System.out.println("MessageBeanEnImpl ์์ฑ์ ํธ์ถ๋จ");
}
public void sayHello(String name) {
System.out.println("nice to meet you!" + name);
}
}
2. MessageBeanKoImpl
public class MessageBeanKoImpl implements MessageBean{
public MessageBeanKoImpl() {
System.out.println("MessageBeanKoImpl ์์ฑ์ ํธ์ถ๋จ");
}
public void sayHello(String name) {
System.out.println(name + " ๋ ๋ฐฉ๊ฐ๋ฐฉ๊ฐ");
}
}
๊ธฐ์กด java์ ์์ฑ ๋ฐฉ์
MessageBean bean = new MessageBeanEnImpl();
bean.sayHello("Bong");
bean = new MessageBeanKoImpl();
bean.sayHello("๋ด");
<?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 class="sample01.MessageBeanKoImpl" id="ko" scope="singleton"></bean>
<bean class="sample01.MessageBeanKoImpl" id="ko2"/>
<bean class="sample01.MessageBeanEnImpl" id="en"/>
</beans>
public class MainTest {
public static void main(String[] args) {
//์คํ๋ง ์ปจํ
์ด๋๋ ์ค์ ๋ฌธ์ ๊ธฐ๋ฐ์ผ๋ก ๋ง๋ค์ด์ง๋๊ฒ ! (classPath๋ฅผ ๊ธฐ์ค ๊ฒฝ๋ก)
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
MessageBean bean = context.getBean("en", MessageBean.class); // context.getBean(String Id, class)
bean.sayHello("Bong");
bean = context.getBean("ko", MessageBean.class);
bean.sayHello("๋ด");
//getBean ํ ๋๋ง๋ค ์๋กญ๊ฒ ๋ง๋๋?? ์๋๋ฉด ๋ง๋ ๊ฑฐ ์ฌ์ฉํ๋๊ฑด๊ฐ?? - ๋์ผํ ๊ฐ์ฒด!
MessageBean bean2 = context.getBean("ko", MessageBean.class); // id๊ฐ ๋์ผํ๊ฒ getBean()
System.out.println("bean = " + bean);
System.out.println("bean2 = " + bean2);
//์ฃผ์๊ฐ์ด ๊ฐ์! ์ฆ, ์ฑ๊ธํค์ผ๋ก ๊ด๋ฆฌ๋๋ ๊ฐ์ฒด์ ๊ฐ์ (static class)
//์ฌ์ ์ด๊ธฐํ , ๋ฏธ๋ฆฌ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋์ !
//bean ํ๋๋น 1๊ฐ์ฒด
}
}
- ๋ง์ฝ scope="prototype" ๋ผ๋ฉด -> prototype ์ผ๋ก ํ๋ฉด ๊ฐ ๋ค๋ฅธ ๊ฐ์ฒด ๋ง๋ค์ด์ง! ์ฆ ๋ฏธ๋ฆฌ ์์ฑ์ ์ํจ! ์ง์ฐ์ด๊ธฐํ(์ง์ฐ๋ก๋ฉ)
* MessageBeanEnImpl ์์ฑ์ ํธ์ถ๋จ - En์ singleton
* MessageBeanKoImpl ์์ฑ์ ํธ์ถ๋จ - ํ์ํ ๋ ์์ฑ
* nice to meet you!Bong
* MessageBeanKoImpl ์์ฑ์ ํธ์ถ๋จ - ํ์ํ ๋ ์์ฑ
* ๋ด ๋ ๋ฐฉ๊ฐ๋ฐฉ๊ฐ
* MessageBeanKoImpl ์์ฑ์ ํธ์ถ๋จ - Ko ํ๋๋ ์์ฑ
* bean = sample01.MessageBeanKoImpl@23348b5d
* bean2 = sample01.MessageBeanKoImpl@70325e14
- ๋ง์ฝ scope = "singleton" ์ด๋ฉด -> ๋ฏธ๋ฆฌ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋์ (๊ธฐ๋ณธ๊ฐ)
* MessageBeanKoImpl ์์ฑ์ ํธ์ถ๋จ
* MessageBeanEnImpl ์์ฑ์ ํธ์ถ๋จ
* MessageBeanKoImpl ์์ฑ์ ํธ์ถ๋จ - ๋ฏธ๋ฆฌ ์์ฑ
* nice to meet you!Bong
* ๋ด ๋ ๋ฐฉ๊ฐ๋ฐฉ๊ฐ
* bean = sample01.MessageBeanKoImpl@2df3b89c
* bean2 = sample01.MessageBeanKoImpl@2df3b89c
@ToString
public class Member {
private String id;
private String pwd;
private int age;
private String addr;
public Member() {
System.out.println("Member ๊ธฐ๋ณธ์์ฑ์");
}
public Member(String id) {
System.out.println("Member(String id) ์์ฑ์");
this.id = id;
}
public Member(String id ,String pwd) {
System.out.println("Member(String id ,String pwd) ์์ฑ์");
this.id = id;
this.pwd = pwd;
}
public Member(String id ,int age) {
System.out.println("Member(String id ,int age) ์์ฑ์");
this.id = id;
this.age = age;
}
public Member(String id, int age, String addr) {
System.out.println("Member(String id, int age, String addr) ์์ฑ์");
this.id = id;
this.age = age;
this.addr = addr;
}
public Member(String id, String pwd, int age, String addr) {
this.id = id;
this.pwd = pwd;
this.age = age;
this.addr = addr;
System.out.println("Member(String id, String pwd, int age, String addr) ์์ฑ์");
}
}
public class MemberDao {
public MemberDao() {
System.out.println("MemberDao ์์ฑ์์
๋๋ค.");
}
public void insert(Member member) {
System.out.println("MemberDao์ insert ํธ์ถ๋จ....");
System.out.println("member = " + member);
}
}
public class MemberService {
private MemberDao memberDao;
private Member member;
public MemberService() {
System.out.println("MemberService ์์ฑ์ ํธ์ถ");
}
public MemberService(MemberDao memberDao, Member member) {
this.memberDao = memberDao;
this.member = member;
System.out.println("MemberService(MemberDao memberDao, Member member) ์์ฑ์ ํธ์ถ");
System.out.println("memberDao =" + memberDao);
System.out.println("member =" + member);
}
public void serviceInsert() {
System.out.println("MemberService์ serviceInsert ํธ์ถ๋จ...");
memberDao.insert(member);
}
}
<?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">
1. ๊ธฐ๋ณธ์์ฑ์
<bean class="sample02.Member" id="m1"/>
2. ์ธ์ 1๊ฐ ๋ฐ๋ ์์ฑ์
<bean class="sample02.Member" id="m2">
<constructor-arg value="jang"/>
</bean>
3. ์ธ์ 2๊ฐ ๋ฐ๋ ์์ฑ์
<bean class="sample02.Member" id = "m3">
<constructor-arg value="kim"/>
<constructor-arg value="20" type="int"/>
</bean>
์ธ์๊ฐ 2๊ฐ ๋ฐ๋ ์์ฑ์๊ฐ 2๊ฐ! -> ๊ฐฏ์๊ฐ 2๊ฐ์ธ ์ ๋ค ๋จผ์ ์ฐพ์
type ๋ช
์ ์ํ๋ฉด ์ฝ๋์ ๊ฐ์ฅ ๋จผ์ ์๋ ์ธ์ 2๊ฐ ๋ฐ๋ ์์ฑ์ ํธ์ถ
4. ์ธ์ 4๊ฐ ๋ฐ๋ ์์ฑ์
<bean class="sample02.Member" id ="m4">
<constructor-arg value="king" index="0" />
<constructor-arg value="30" index="2"/>
<constructor-arg value="์์ธ" index="3"/>
<constructor-arg value="1234" index="1"/>
</bean>
์์ ์๋ง์ถ๋ฉด ์๋ฌ ๋จ์ด์ง index ์จ์ค์ ๋ง์ถฐ์ฃผ๊ธฐ!
5. MemberDao ์์ฑ์
<bean class="sample02.MemberDao" id="dao"/>
6. MemberService ์ธ์ 2๊ฐ ๋ฐ๋ ์์ฑ์
<bean class="sample02.MemberService" id="service">
<constructor-arg ref="dao"/>
<constructor-arg ref="m4"/>
</bean>
๊ธฐ๋ณธ์์ฑ์๋ก ์์ฑํ๋ฉด Member์ MemberDAO๊ฐ ์ด๊ธฐํ๋์ง ์์
์์ฑ์๋ฅผ ์ด์ฉํ ์ฃผ์
(DI) - m4 ๋ฃ์ด์ค
1) value : ์ผ๋ฐํ์
(ํ๋ฆฌ๋ฏธํธ ํ์
)
2) ref : ๊ฐ์ฒด
</beans>
๊ธฐ์กด ๋ฐฉ์
Member member = new Member("jang", "1234", 20, "์์ธ");
MemberDao memberDao = new MemberDao();
MemberService service = new MemberService(memberDao, member);
service.serviceInsert();
public class MainTest {
public static void main(String[] args) {
ApplicationContext context
= new ClassPathXmlApplicationContext("springDI.xml");
System.out.println("---------------------------");
MemberService service = context.getBean("service", MemberService.class);
service.serviceInsert();
}
}
1. Member ๊ธฐ๋ณธ์์ฑ์
2. Member(String id) ์์ฑ์
3. Member(String id ,int age) ์์ฑ์
4. Member(String id, String pwd, int age, String addr) ์์ฑ์
5. MemberDao ์์ฑ์์
๋๋ค.
6. MemberService(MemberDao memberDao, Member member) ์์ฑ์ ํธ์ถ
memberDao = MemberDao()
member = Member(id=king, pwd=1234, age=30, addr=์์ธ)
-------------------------------------------
MemberService์ serviceInsert ํธ์ถ๋จ...
MemberDao์ insert ํธ์ถ๋จ...
member = Member(id=king, pwd=1234, age=30, addr=์์ธ)