ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์˜์กด์„ฑ ์ฃผ์ž… - ์ƒ์„ฑ์ž, setter๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
    JAVA 2020. 11. 8. 17:05

    ๐ŸŽฏ Spring Framework์˜ ํŠน์ง•๊ณผ ์˜์กด์„ฑ ์ฃผ์ž… ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณธ๋‹ค.



     

     

     

    ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ

    ์ž๋ฐ” ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ(Java EE) ๊ฐœ๋ฐœ์„ ํŽธํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ์˜คํ”ˆ ์†Œ์Šค ๊ฒฝ๋Ÿ‰๊ธ‰ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋ ˆ์ž„์›Œํฌ

     

    *์—”ํ„ฐํ”„๋ผ์ด์ฆˆ๋ž€?

    ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜: ๊ธฐ์—…์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•˜๊ธฐ ์ ํ•ฉํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜

    ์ด๋Ÿฐ ๊ธฐ์—…์šฉ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋ณดํ†ต ๊ตฌ์กฐ๊ฐ€ ๋ณต์žกํ•œ ํŽธ์ธ๋ฐ, ์Šคํ”„๋ง์€ ๊ฐ„๋‹จํ•œ ๊ตฌ์„ฑ์œผ๋กœ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋Ÿ‰(light-weight) ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.

     

     

     

    ์™œ ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ• ๊นŒ?

    • POJO ๊ฐœ๋ฐœ์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • IoC(Inversion of Control) ์ปจํ…Œ์ด๋„ˆ: ๊ฐ์ฒด์˜ ๊ด€๋ฆฌ๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—๊ฒŒ ์•„์›ƒ์†Œ์‹ฑํ•œ๋‹ค. ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ฝ”๋“œ ๋Œ€์‹  ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์„œ IoC(์ œ์–ด์˜ ์—ญ์ „)์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. configuration.xml ํŒŒ์ผ์ด๋‚˜ ์ž๋ฐ” ์†Œ์Šค์ฝ”๋“œ ์•ˆ์— ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
    • ๋Š์Šจํ•œ ์—ฐ๊ฒฐ(๋””์ปคํ”Œ๋ง)์„ ์œ„ํ•œ ์˜์กด์„ฑ ์ฃผ์ž…(Dependency Injection): ํด๋ž˜์Šค ์‚ฌ์ด์˜ ์˜์กด ๊ด€๊ณ„๋ฅผ ๋นˆ(Bean) ์„ค์ • ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ž๋™์œผ๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ฒƒ
    • AOP ์ง€์› (๊ธฐ๋Šฅ๋ณ„ ๋ชจ๋“ˆํ™”)

     

     

     

    ์˜์กด์„ฑ ์ฃผ์ž…

    ๋‚œ ์ฐจ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ฐจ ๊ณต์žฅ(configuration.xml)์— ์ฐจ๋ฅผ ํ•˜๋‚˜ ๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ•œ๋‹ค. ๊ณต์žฅ์—์„œ ๋ชจ๋“ ๊ฑธ ๋งŒ๋“ค์–ด์„œ ์ค„๊ฑฐ๋‹ค.
    
    ์ฐจ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ชจ๋“  ์˜์กด์„ฑ(์—”์ง„, ํƒ€์ด์–ด, ์‹œํŠธ ๋“ฑ)์„ ๊ณต์žฅ์—์„œ ์ฃผ์ž…ํ•ด์ค€๋‹ค.

    A๊ฐ€ "๋‚˜๋Š” B๊ฐ€ ํ•„์š”ํ•ด"๋ผ๊ณ  ์˜์กด์„ฑ ์ฃผ์ž… ์‹ ํ˜ธ๋งŒ ๋ณด๋‚ด๋ฉด ApplicationContext๋ผ๋Š” ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•„์š”ํ•œ B๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฃผ์ž…ํ•ด์ค€๋‹ค. ์—ฌ๊ธฐ์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด๋ฅผ Bean ์ด๋ผ๊ณ  ๋งํ•˜๋ฉฐ ๋นˆ๊ณผ ๋นˆ์˜ ์˜์กด๊ด€๊ณ„๋Š” xml ๋˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. sts ๋ ˆ๊ฑฐ์‹œ ํ”„๋กœ์ ํŠธ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ xml ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.

     

     

     

    ์Šคํ”„๋ง ๊ฐœ๋ฐœ ๊ณผ์ •

    1. xml์— beans ์„ค์ • (๊ฐ์ฒด)
    2. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
      • ApplicationContext๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ
      • ๊ทธ๋ฐ–์— GenericWebApplicationContext ๋“ฑ์ด ์žˆ๋‹ค.
      • main์— ์ด ์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉ
    3. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ beans ์ฐพ๊ธฐ
      • context.getBean("๋นˆ์ฆˆ ์•„์ด๋””", ํด๋ž˜์Šค์ด๋ฆ„.class)
      • ์•„์ด๋””๋ฅผ ์ž˜๋ชป์ ์œผ๋ฉด ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค: "NoSuchBeanDefinitionException: No bean named 'stu1' available"
    ApplicationContext ac = new ClassPathXmlApplicationContext( 
                    "classpath:applicationContext.xml"); 
    
    UserBean userBean = (UserBean)ac.getBean("userBean");
    userBean.setName("kim");
    System.out.println(userBean.getName());

     

     

     

    ์˜์กด์„ฑ ์ฃผ์ž… 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•

    • ์ƒ์„ฑ์ž ์ฃผ์ž…
    • setter ์ฃผ์ž…
    • ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ ์˜คํ† ์™€์ด์–ด๋ง

     

     

     

    ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ๋ฒ•

    1. ์˜์กด์„ฑ ํด๋ž˜์Šค ์ •์˜
    2. ์ฃผ์ž…์„ ์œ„ํ•ด ์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ
      • ๋ฉค๋ฒ„๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑ์ž ์ธ์ž์— ๊ทธ๋Œ€๋กœ ๊ฐ–๊ณ ์˜ค๊ธฐ
    3. configuration.xml ํŒŒ์ผ์— ์˜์กด์„ฑ ์ฃผ์ž… ์„ค์ •ํ•˜๊ธฐ
      • helper Bean ๊ฐ์ฒด ๋จผ์ € ์ƒ์„ฑํ•˜๊ณ , ์ฃผ์ž… ๋ฐ›๋Š” Bean ๊ฐ์ฒด๋Š” ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹์œผ๋กœ helper ์ฐธ์กฐ

     

    constructor_injection1:

    com.dto.Student:

    private String name;
        public Student(String x) {
            System.out.println("์ธ์ž์žˆ๋Š” ์ƒ์„ฑ์ž");
            this.name = x;
        }
        public String getInfo() {
            return name;
        }

    stu.xml:

    <bean id="firstStudent" class="com.dto.Student">
        <constructor-arg name="x" value="ํ™๊ธธ๋™" />
    </bean>

    ์ด๊ฑธ ๋œฏ์–ด๋ณด๋ฉด Student firstStudent = new Student("ํ™๊ธธ๋™"); ๊ฐ์ฒด์ƒ์„ฑ ๋ฐฉ์‹๊ณผ ๋˜‘๊ฐ™๋‹ค.

    TestStudent:

    Student test = ctx.getBean("firstStudent", Student.class);
    System.out.println(test.getInfo());

     

     

    ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“œ๋Š” ์ด์œ ?

    ๊ธฐ๋ณธ์ƒ์„ฑ์ž(no-arg constructor)๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ผ๋‹จ ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. ๊ทผ๋ฐ ๋‚ด๊ฐ€ ์•„๋ฌด ์ƒ์„ฑ์ž๋‚˜ ์ •์˜ํ•ด๋†“์œผ๋ฉด(์ธ์ž์žˆ๋Š” ์ƒ์„ฑ์ž) ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋”ฐ๋กœ ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์ง€ ์•Š๋Š”๋‹ค.

     

     

    constructor_injection2: ์ธ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ธ ์ƒ์„ฑ์ž

    package com.dto;
    
    public class Student {
    
        private String name;
        private int age;
    
        public Student() {}
    
        public Student(String name) { //์ƒ์„ฑ์ž๋ฅผ ์ž‘์„ฑ
            this.name = name;
            System.out.println("1");
        }
    
        public Student(int age) { //์ƒ์„ฑ์ž๋ฅผ ์ž‘์„ฑ
            this.age = age;
            System.out.println("2");
        }
    
        public Student(String name, int age) { //์ƒ์„ฑ์ž๋ฅผ ์ž‘์„ฑ
            this.name = name;
            this.age = age;
            System.out.println("3");
        }
    
        public String getInfo() {
            return name+"\t"+age;
        }
    
        @Override
        public String toString() {
            return "Student [name=" + name + ", age=" + age + "]";
        }
    }

    ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ๋„ค์ž„๋งŒ ๋“ค์–ด๊ฐ„๊ฑฐ, ๋‚˜์ด๋งŒ ๋“ค์–ด๊ฐ„๊ฑฐ, ๋‘˜๋‹ค ๋“ค์–ด๊ฐ„๊ฑฐ

    stu.xml:

    <bean id="firstStudent" class="com.dto.Student">
        <constructor-arg name="name" value="ํ™๊ธธ๋™" />
    </bean>
    <bean id="secondStudent" class="com.dto.Student">
        <constructor-arg name="age" value="20" />
    </bean>
    <bean id="thirdStudent" class="com.dto.Student">
        <constructor-arg name="name" value="์œ ๋…ธ์œคํ˜ธ" />
        <constructor-arg name="age" value="20" />
    </bean>

    age๋ฅผ String ํƒ€์ž…์œผ๋กœ ์œผ๋กœ ์จ๋„ dto์— ์ง€์ •ํ•œ ๋Œ€๋กœ int ํƒ€์ž…์œผ๋กœ ์ž๋™ ํ˜•๋ณ€ํ™˜๋˜์–ด ๋“ค์–ด๊ฐ„๋‹ค. ์ด๋ ‡๊ฒŒ beans ๊ฐ์ฒด 3๊ฐœ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์ƒ์„ฑ์ž ์ธ์ž 2๊ฐœ ๋ฐ›๋Š”๊ฑด ํƒœ๊ทธ๋„ 2๊ฐœ ๋“ค์–ด๊ฐ”๋‹ค.

    TestStudent.java:

    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("stu.xml");
    
    Student test = ctx.getBean("firstStudent", Student.class);
    System.out.println(test.getInfo());
    
    Student test2 = ctx.getBean("secondStudent", Student.class);
    System.out.println(test2.getInfo());
    
    Student test3 = ctx.getBean("thirdStudent", Student.class);
    System.out.println(test3.getInfo());
    
    System.out.println(test+"\t"+test2+"\t"+test3);

     

     

    constructor_injection3: ๋‹ค๋ฅธ ๊ฐ์ฒด ์ฐธ์กฐํ•˜๊ธฐ

    package com.dto;
    
    public class Cat {
        private String catName;
        private int catAge;
    
        public Cat(String catName, int catAge) {
            this.catName = catName;
            this.catAge = catAge;
        }
    
        public String getCatInfo() {
            return catName+"\t"+catAge;
        }
    }

    ๊ณ ์–‘์ด ๊ฐ์ฒด ์ƒ์„ฑ

    public class Student {
    
        private String name;
        private int age;
        private Cat cat;
    
        public Student() {}
    
        public Student(String name, int age, Cat cat) { //์ƒ์„ฑ์ž๋ฅผ ์ž‘์„ฑ
            this.name = name;
            this.age = age;
            this.cat = cat;
        }

    ํ•™์ƒ์€ ๊ณ ์–‘์ด๋„ ํ•œ๋งˆ๋ฆฌ ๊ฐ–๊ณ ์žˆ๋‹ค. Cat ๊ฐ์ฒด๋ฅผ ์™ธ๋ถ€์—์„œ ์ฃผ์ž…๋ฐ›์•„์•ผํ•œ๋‹ค.

    <bean id="pet01" class="com.dto.Cat">
        <constructor-arg name="catAge" value="3" />
        <constructor-arg name="catName" value="๋‚˜๋น„" />
    </bean>
    
    <bean id="firstStudent" class="com.dto.Student">
        <constructor-arg name="name" value="ํ™๊ธธ๋™" />
        <constructor-arg name="age" value="30"></constructor-arg>
        <constructor-arg name="cat" ref="pet01"></constructor-arg>
    </bean>

    cat์„ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•ด ์บฃ ๋นˆ์ฆˆ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ฐธ์กฐ๋Š” ref ์†์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค.

    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("stu.xml");
    
    Student stu = ctx.getBean("firstStudent", Student.class);
    System.out.println(stu.getInfo());
    
    Cat cat = ctx.getBean("pet01", Cat.class);
    System.out.println(cat.getCatInfo());
    
    Cat cat1 = stu.getCat();
    System.out.println(cat1.getCatInfo());

     

     

    constructor_injection4: jdbc ์—ฐ์Šต

    DeptDAO.java:

    public class DeptDAO {
        public List<String> list(){
            return Arrays.asList("ํ™๊ธธ๋™", "์ด์ˆœ์‹ ", "์œ ๊ด€์ˆœ");
        }
    }

    dao์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    DeptService.java:

    public class DeptService {
        DeptDAO dao; //new๊ฐ€ ์‚ฌ๋ผ์ง€๊ณ  .xml์—์„œ ์ฃผ์ž…
    
        public DeptService(DeptDAO dao) {
            this.dao = dao;
        }
        public List<String> list(){
            return dao.list();
        }
    }

    ์„œ๋น„์Šค ๊ฐ์ฒด๋Š” dao ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹์œผ๋กœ ๋ฐ›๋Š”๋‹ค.

    stu.xml:

    <bean id="deptdao" class="com.dao.DeptDAO"></bean>
    <bean id="service" class="com.dao.DeptService">
        <constructor-arg name="dao" ref="deptdao"></constructor-arg>
    </bean>

    DAO Bean ๊ฐ์ฒด๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•˜๊ณ  service ๊ฐ์ฒด๋Š” ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹์„ ํ†ตํ•ด ํ•„์š”ํ•œ dao ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. 

    main:

    DeptService service = ctx.getBean("service", DeptService.class);
            List<String> list = service.list();
            for (String x : list) {
                System.out.println(x);
            }

    ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

     

    Setter ์ฃผ์ž… ๋ฐฉ๋ฒ•

    1. ํด๋ž˜์Šค์— ์ฃผ์ž…์„ ์œ„ํ•œ setter ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค.

    2. configuration.xml ํŒŒ์ผ์— ์˜์กด์„ฑ์„ ์„ค์ •ํ•œ๋‹ค

     

    setter_injection1:

    public class Student {
        private String name;
        public Student() {
            System.out.println("๊ธฐ๋ณธ์ƒ์„ฑ์žํ˜ธ์ถœ======");
        }
        public Student(String name) { 
            this.name = name;
        }
        public void setName(String name) {
            System.out.println("setNameํ˜ธ์ถœ======");
            this.name = name;
        }
        public String getInfo() {
            return name;
        }
    }

    ๋ฉค๋ฒ„๋ณ€์ˆ˜ name์˜ ๊ฐ’์„ ์…‹ํŒ…ํ•  ์ˆ˜ ์žˆ๋Š” setter ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ธ์ž์— ์žˆ๋Š” name ์ด๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ xml์—์„œ ์‚ฌ์šฉํ•  name์†์„ฑ์˜ ๊ฐ’์ด ๋œ๋‹ค. (setter ๋ฉ”์†Œ๋“œ๋ช…์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด๋„ ๋œ๋‹ค.)

    <bean id="firstStudent" class="com.dto.Student">
        <property name="name" value="ํ™๊ธธ๋™"></property>
    </bean>

    xml ์„ค์ •ํŒŒ์ผ์— ํ•™์ƒ ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. setter ์ฃผ์ž… ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ bean ๊ฐ์ฒด ์•ˆ์— <property> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์ด ํ”„๋กœํผํ‹ฐ ํƒœ๊ทธ๋Š” setter ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒ ๋‹ค๋Š” ๋œป์ด๋‹ค. name์†์„ฑ์€ setterํ•จ์ˆ˜์˜ ์ธ์ž ๋˜๋Š” ์ด๋ฆ„! value๋Š” ๋‚ด๊ฐ€ ์„ธํŒ…ํ•˜๊ณ  ์‹ถ์€ ๊ฐ’์„ ์ง€์ •ํ•œ๋‹ค.

    GenericXmlApplicationContext ctx = 
                    new GenericXmlApplicationContext("stu.xml");
    
            Student stu = ctx.getBean("firstStudent", Student.class);
            System.out.println(stu.getInfo());

    setํ•œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋• getter ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

     

     

    setter_injection2: setter ํ•จ์ˆ˜ ์—ฌ๋Ÿฌ๊ฐœ

    public class Student {
        private String name;
        private int age;
        //setter injection
        public void setAge(int age) {
            this.age = age;
        }
        public void setName(String name) {
            System.out.println("setNameํ˜ธ์ถœ======");
            this.name = name;
        }
        public String getInfo() {
            return name + "\t" + age;
        }
    }
    <bean id="firstStudent" class="com.dto.Student">
        <property name="name" value="ํ™๊ธธ๋™"></property>
        <property name="age" value="20"></property>
    </bean>

    setter ๋ฉ”์†Œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ง€์ •ํ–ˆ์„ ๊ฒฝ์šฐ ๊ฐ๊ฐ์— ํ•ด๋‹นํ•˜๋Š” ํ”„๋กœํผํ‹ฐ ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค. ํ”„๋กœํผํ‹ฐ๋กœ ์…‹ํ•จ์ˆ˜ ํ˜ธ์ถœํ•ด์„œ ๋‚˜์ด๋ฅผ 20์œผ๋กœ ์„ธํŒ…ํ•ด์คฌ๋‹ค.

     

     

    setter_injection3: ์ƒ์„ฑ์ž์™€ setter ํ•จ๊ป˜ ์‚ฌ์šฉ

    public class Student {
    
        private String name;
        private int age;
    
        //setter injection
        public void setName(String name) {
            this.name = name;
        }
    
        //constructor injection
        public Student(int age) {
            this.age = age;
        }
    
        public String getInfo() {
            return name + "\t" + age;
        }
    
    }

    ์ด๋ฒˆ์—” setํ•จ์ˆ˜๋Š” name์—๋งŒ ๋งŒ๋“ค์–ด์ฃผ๊ณ  age๋Š” ์ƒ์„ฑ์ž๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

    <bean id="firstStudent" class="com.dto.Student">
        <property name="name" value="ํ™๊ธธ๋™"></property>
        <constructor-arg name="age" value="20"></constructor-arg>
    </bean>

    ์„ค์ •ํŒŒ์ผ์—์„œ Stu ๊ฐ์ฒด๋Š” property๋ž‘ constructor-arg ํƒœ๊ทธ ๋‘˜๋‹ค ํ•„์š”ํ•˜๋‹ค.

     

     

    setter_injection4: ๋‹ค๋ฅธ ๊ฐ์ฒด ์ฐธ์กฐํ•˜๊ธฐ

    public class Cat {
        private String catName;
        private int catAge;
    
        public void setCatName(String catName) {
            this.catName = catName;
        }
    
        public void setCatAge(int catAge) {
            this.catAge = catAge;
        }
    
        public String getCatInfo() {
            return catName + "\t" + catAge;
        }
    }

    cat dto ์ƒ์„ฑ. setter ํ•จ์ˆ˜๋งŒ ๊ฐ–๊ณ ์žˆ๋‹ค.

    <bean id="pet01" class="com.dto.Cat">
        <property name="catName" value="๋‚˜๋น„"></property>
        <property name="catAge" value="3"></property>
    </bean>
    
    <bean id="firstStudent" class="com.dto.Student">
        <property name="name" value="ํ™๊ธธ๋™"></property>
        <property name="age" value="30"></property>
        <property name="cat" ref="pet01"></property>
    </bean>

    ํ•™์ƒ๊ฐ์ฒด๋Š” Cat๊ฐ์ฒด๋ฅผ ref ์†์„ฑ์œผ๋กœ ์ฐธ์กฐํ•œ๋‹ค.

    GenericXmlApplicationContext ctx = 
                    new GenericXmlApplicationContext("stu.xml");
    
            Student stu = ctx.getBean("firstStudent", Student.class);
            System.out.println(stu.getInfo());
            Cat cat = ctx.getBean("pet01", Cat.class);
            System.out.println(cat.getCatInfo());

     

     

    setter_injection5: p ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์‚ฌ์šฉ

    property ํƒœ๊ทธ๋ฅผ Bean ๊ฐ์ฒด์— ์†์„ฑ์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. p ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด xml ๋„ค์ž„์ŠคํŽ˜์ด์Šค์—์„œ p๋ฅผ ์ฒดํฌํ•œ๋‹ค.

    <bean id="firstStudent" class="com.dto.Student" p:name="ํ™๊ธธ๋™"/>

    property ํƒœ๊ทธ ์•ˆ์“ฐ๊ณ  bean ์•ˆ์— ์ธ๋ผ์ธ์œผ๋กœ ์ž‘์„ฑ. 

    <bean id="pet01" class="com.dto.Cat" p:catName="๋‚˜๋น„" p:catAge="2" />
    
    <bean id="firstStudent" class="com.dto.Student" p:name="ํ™๊ธธ๋™" p:age="20" p:cat-ref="pet01">

     

    ๋‹ค๋ฅธ bean ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋˜ cat๋„ name-ref ํ˜•์‹์˜ ์ธ๋ผ์ธ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

     

    ๐Ÿ˜œ NEXT : ์Šคํ”„๋ง scope, lifeCycle๊ณผ collection ๊ณต๋ถ€ํ•˜๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€