본문 바로가기

학원/스프링-학원

생명주기(LIFE CYCLE)와 범위(SCOPE)

728x90

스프링 컨테이너 생명 주기

 

예제

<클래스>

 

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
41
42
43
44
45
46
47
//Student 
package com.javalec.spring_7_1;
 
public class Student {
    private String name;
    private int age;
    
    //생성자
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
 
//MainClass 
package com.javalec.spring_7_1;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        //load()메서드를 사용해서 xml경로 받아옴
        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

 

<xml>

 

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

 

스프링 빈 생명주기

 

 

 

 

- 빈이 생성된 다음에 호출

- 컨터이너가 종료되기 전에 호출

 

 

 

어노테이션 오류날 때

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

 

위의 내용 복사해서 사용

 

예제

<클래스>

 

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Student
package com.javalec.spring_7_2;
 
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
 
public class Student implements InitializingBean, DisposableBean{
    private String name;
    private int age;
    
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet()");
        
    }
    
    @Override
    public void destroy() throws Exception {
        System.out.println("destroy()");
        
    }
    
    //생성자
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
 
//OtherStudent
package com.javalec.spring_7_2;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
 
public class OtherStudent {
    private String name;
    private int age;
    
    //이거 쓰려면 pom.xml에 해당하는 내용 추가해줘야 함
    //javax.annotation-api 관련
    @PostConstruct
    public void initMethod() {
        System.out.println("initMethod()");
    }
    
    @PreDestroy
    public void destoryMethod() {
        System.out.println("destoryMethod()");        
    }
    
    //생성자
    public OtherStudent(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
 
//MainClass
package com.javalec.spring_7_2;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("classpath:applicationCTX.xml");
        //refresh주석처리하면 인터페이스 상속받은 것들은 아무것도 출력되지 않음
        ctx.refresh();
        
        ctx.close();
    }
}
//빈 객체 생성 후 init메서드가 호출되고, 종료 전에 destory메서드 실행된 후 destory()메서드 호출
cs

 

<xml>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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.1.xsd">
    
    <!-- 이걸 설정해줘야 어노테이션들도 메인에서 출력됨 -->
    <context:annotation-config></context:annotation-config>
    
    <bean class="com.javalec.spring_7_2.Student" id="student">
        <constructor-arg value="홍길순"></constructor-arg>
        <constructor-arg value="30"></constructor-arg>
    </bean>
    
    <bean class="com.javalec.spring_7_2.OtherStudent" id="otherStudent">
        <constructor-arg value="홍길자"></constructor-arg>
        <constructor-arg value="50"></constructor-arg>
    </bean>
</beans>
cs

 

 

스프링 빈 범위(SCOPE)

 

xml에서 값을 세팅하고 객체를 가져오면 xml설정한 대로 출력됨

같은 객체를 가져온 다음 다른 값을 세팅하고 출력하면 바꾼 세팅대로 값 출력

다시 처음의 객체를 출력하면 바꾼 값으로 출력됨

 

예제

<클래스>

 

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//Student 
package com.javalec.spring_7_3;
 
public class Student {
    private String name;
    private int age;
    
    //생성자
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
 
//MainClass 
package com.javalec.spring_7_3;
 
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("이름 : "+student2.getName());
        System.out.println("나이 : "+student2.getAge());
        System.out.println("============");
        
        //위의 두 객체가 바라보고 있는 것이 같기 때문에 변경한 값으로 출력됨
        System.out.println("이름 : "+student1.getName());
        System.out.println("나이 : "+student1.getAge());
        
        if (student1.equals(student2)) {
            System.out.println("student1 == student2");
        }else {
            System.out.println("student1 != student2");            
        }
    }
}
 
cs

 

<xml>

 

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

 

 

 

728x90

'학원 > 스프링-학원' 카테고리의 다른 글

AOP(Aspect Oriented Programming)  (0) 2022.05.13
외부 파일을 이용한 설정  (0) 2022.05.11
DI 설정 방법  (0) 2022.05.10
DI - 2  (0) 2022.05.10
DI - 스프링을 이용한 객체 생성과 조립  (0) 2022.05.09