๐ŸŒฑ Spring์˜ ApplictionContext ๊ณผ DI

๋ค๋ฒจ๋กœํผยท2024๋…„ 4์›” 24์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
1/1
post-thumbnail

๐Ÿ“Œ ์Šคํ”„๋ง ๋นˆ(Bean) ์ด๋ž€?

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
๋นˆ์€ ์ธ์Šคํ„ด์Šคํ™”๋œ ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ๋นˆ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

<bean id="hello" class="com.example.Hello"/>
์ด๋ ‡๊ฒŒ xml ํŒŒ์ผ ๋นˆ ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด hello ์ธ์Šคํ„ด์Šค๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Hello hello = new Hello()
์ฆ‰ new ํ‚ค์›Œ๋“œ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ!

๐Ÿง ApplicationContext ์ด๋ž€?

ApplicationContext๋Š” Spring Container์˜ ๊ตฌํ˜„์ฒด ์ค‘ ํ•˜๋‚˜๋กœ, ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ ๋ผ์ดํ”„์‚ฌ์ดํด์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋นˆ(Bean)๋“ค์„ ๊ด€๋ฆฌํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

  1. ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ ๋ผ์ดํ”„์‚ฌ์ดํด ๊ด€๋ฆฌ: ApplicationContext๋Š” ์„ค์ •๋œ ๋นˆ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฐœ๋ฐœ์ž๋Š” ๋นˆ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์— ๊ด€์—ฌํ•˜์ง€ ์•Š๊ณ ๋„ ์•ˆ์ •์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. Configuration Metadata ํ•„์š”: ApplicationContext๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋นˆ๋“ค์€ Spring์˜ ์„ค์ • ๋ฌธ์„œ ๋˜๋Š” Configuration Metadata๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” XML ํŒŒ์ผ์ด๋‚˜ JavaConfig ๊ฐ™์€ ์„ค์ • ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์ •์˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„ค์ • ๋ฌธ์„œ์—๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉ๋  ๋นˆ์˜ ์ •๋ณด, ์˜์กด์„ฑ, ๋นˆ์˜ ์Šค์ฝ”ํ”„ ๋“ฑ์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค.
  3. ๋ฏธ๋ฆฌ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๋†“๋Š”๋‹ค (์‚ฌ์ „ ์ดˆ๊ธฐํ™”): ApplicationContext๋Š” ๋นˆ์„ ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•˜๊ณ  ํ•„์š”ํ•  ๋•Œ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ApplicationContext๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ์„ค์ •๋œ ๋นˆ๋“ค์€ ๋ฏธ๋ฆฌ ์ƒ์„ฑ๋˜์–ด ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋  ๋•Œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋“ค์ด ์ค€๋น„๋˜์–ด ์žˆ์–ด์•ผ ํ•  ๋•Œ ์†๋„์™€ ํšจ์œจ์„ฑ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.
  4. Scope ์„ค์ •: ApplicationContext๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋˜๋Š” ๋นˆ๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹ฑ๊ธ€ํ†ค(Singleton) ์Šค์ฝ”ํ”„๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ์ด๋Š” ApplicationContext๊ฐ€ ๋‹จ์ผ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, Prototype ์Šค์ฝ”ํ”„๋ฅผ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ๊ฐ ์š”์ฒญ์— ๋Œ€ํ•ด ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋นˆ์˜ ์Šค์ฝ”ํ”„๋ฅผ ์„ค์ •ํ•˜์—ฌ ๊ฐ์ฒด์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿงช Test 1 : ๋นˆ์˜ ์Šค์ฝ”ํ”„ "singleton" vs "prototype"

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("๋ด‰");

applicationContext.yml

<?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>
  • Bean ํ•˜๋‚˜๋‹น ํ•œ๊ฐœ์˜ ๊ฐ์ฒด ์ƒ์„ฑ ( ๊ธฐ๋ณธ๊ฐ’์ด singleton )
  • scope = "prototype" ์œผ๋กœ ํ•˜๋ฉด ๊ฐ ๋‹ค๋ฅธ ๊ฐ์ฒด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค.
    • ๋ฏธ๋ฆฌ ์ƒ์„ฑ์„ ํ•˜์ง€์•Š๊ณ  ์ง€์—ฐ์ดˆ๊ธฐํ™”(์ง€์—ฐ๋กœ๋”ฉ)
    • getBean() ์š”์ฒญํ• ๋•Œ ๋งˆ๋‹ค ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์ƒˆ๋กœ์šด ๊ฐ์ฒด ๋งŒ๋“ค์–ด ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ko ์™€ ko2๋Š” ๊ฐ๊ฐ ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๊ฒƒ!
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

๐Ÿงช Test 2 : ์—ฌ๋Ÿฌ ์ƒ์„ฑ์ž ํ…Œ์ŠคํŠธ

Member.java

@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) ์ƒ์„ฑ์ž");

    }
}

MemberDao.java

public class MemberDao {
    public MemberDao() {
        System.out.println("MemberDao ์ƒ์„ฑ์ž์ž…๋‹ˆ๋‹ค.");
    }

    public void insert(Member member) {
        System.out.println("MemberDao์˜ insert ํ˜ธ์ถœ๋จ....");
        System.out.println("member = " + member);
    }
}

MemberService

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);
    }
}

springDI.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">

    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=์„œ์šธ)
profile
๐Ÿ’ช ์ ์ง„์  ๊ณผ๋ถ€ํ•˜๋กœ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€