[SPRING] 제 07강. 생명 주기(life cycle)와 범위(scope)

07-1. 스프링 컨테이너 생명 주기

07-2. 스프링 빈 생명 주기

07-3. 스프링 빈 범위





07-1. 스프링 컨테이너 생명 주기


MainClass.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.javalec.ex;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
 
    public static void main(String[] args) {
        
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();    // 생성
 
        ctx.load("classpath:applicationCTX.xml");                                // 설정
        
        ctx.refresh();
        
        Student student = ctx.getBean("student", Student.class);                // 사용
        System.out.println("이름 : " + student.getName());
        System.out.println("나이 : " + student.getAge());
        
        ctx.close();                                                            // 종료
        
    }
    
}
 
cs



Student.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.javalec.ex;
 
import java.util.ArrayList;
 
public class Student {
 
    private String name;
    private int age;
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
    
}
 
cs


applicationCTX.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?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 id="student" class="com.javalec.ex.Student">
        <constructor-arg value="홍길순"></constructor-arg>
        <constructor-arg value="30"></constructor-arg>
    </bean>
 
</beans>
 
cs




07-2. 스프링 빈 생명 주기


MainClass.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.javalec.ex;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
 
    public static void main(String[] args) {
        
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
 
        ctx.load("classpath:applicationCTX.xml");
        
        ctx.refresh();
        
        ctx.close();
        
    }
    
}
 
cs



Student.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.javalec.ex;
 
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
 
 
 
public class Student implements InitializingBean, DisposableBean{
 
    private String name;
    private int age;
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
 
    // 초기화할 때 인터페이스로 쓰고 싶다
    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("afterPropertiesSet()");
    }
 
    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("destroy()");
    }
    
}
 
cs



OtherStudent.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.javalec.ex;
 
import javax.annotation.*;
 
public class OtherStudent  {
 
    private String name;
    private int age;
    
    public OtherStudent(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
    
    // 어노테이션으로 쓰고 싶을때
    @PostConstruct
    public void initMethod() {
        System.out.println("initMethod()");
    }
    
    @PreDestroy
    public void destroyMethod() {
        System.out.println("destroyMethod()");
    }
 
}
 
cs



applicationCTX.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd">
    
    <context:annotation-config />
    
    <bean id="student" class="com.javalec.ex.Student">
        <constructor-arg value="홍길순"></constructor-arg>
        <constructor-arg value="30"></constructor-arg>
    </bean>
    
    <bean id="otherStudent" class="com.javalec.ex.OtherStudent">
        <constructor-arg value="홍길자"></constructor-arg>
        <constructor-arg value="50"></constructor-arg>
    </bean>
    
 
 
</beans>
 
cs



07-3. 스프링 빈 범위


MainClass.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.javalec.ex;
 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
 
    public static void main(String[] args) {
        
        AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationCTX.xml");
        
        Student student1 = ctx.getBean("student", Student.class);
        System.out.println("이름 : " + student1.getName());
        System.out.println("나이 : " + student1.getAge());
        
        System.out.println("==============================");
        
        Student student2 = ctx.getBean("student", Student.class);
        student2.setName("홀길자");
        student2.setAge(100);
        
        System.out.println("이름 : " + student1.getName());
        System.out.println("나이 : " + student1.getAge());
        
        System.out.println("==============================");
 
        if(student1.equals(student2)) {
            System.out.println("student1 == student2");
        } else {
            System.out.println("student1 != student2");
        }
        
        ctx.close();
        
    }
    
}
 
cs



Student.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.javalec.ex;
 
public class Student {
 
    private String name;
    private int age;
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
}
 
cs


applicationCTX.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?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 id="student" class="com.javalec.ex.Student" scope="singleton">
        <constructor-arg value="홍길순"></constructor-arg>
        <constructor-arg value="30"></constructor-arg>
    </bean>
 
</beans>
 
cs


Posted by 너래쟁이
: