記錄

Spring) bean's Life Cycle (bean의 생명주기) 본문

Web/Spring framework

Spring) bean's Life Cycle (bean의 생명주기)

surhommejk 2018. 4. 30. 00:01







예제코드 1 (기본)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--
            Client client = new Client("192.168.0.115");
            client.setHost("192.168.1.1");
    -->
    <bean id="client" class="spring.Client">
        <constructor-arg value="192.168.0.115" />
        <property name="host" value="192.168.1.1" />
    </bean>
</beans>


public class Client implements InitializingBean , DisposableBean {
    public Client() {
        System.out.println("Client Default");
    }
    
    private String defaulthost;
    public Client(String defaulthost){
        this.defaulthost = defaulthost;
        System.out.println("Client Overloading :" + this.defaulthost);
    }
    
    private String host;
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
        System.out.println("Client setHost() 호출 : " + this.host);
    }

    public void send(){
        System.out.println("데이터 보내기....");
    }
     
    //초기화
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Client 초기화 함수 호출");     
    }

    //소멸
    @Override
    public void destroy() throws Exception {
        System.out.println("Client 소멸자 함수 호출");
        
    }
}


public class Program {

    public static void main(String[] args) {
        
        //Client client = new Client("192.168.0.115");
        //client.setHost("192.168.1.1");
        
        
        GenericXmlApplicationContext context =
                new GenericXmlApplicationContext("classpath:ApplicationContext.xml");
        Client client = context.getBean("client", Client.class);
        client.send();
        context.close(); //컨테이너 종료
        
    }
}



콘솔결과

Client Overloading :192.168.0.115
Client setHost() 호출 : 192.168.1.1
Client 초기화 함수 호출
데이터 보내기....
Client 소멸자 함수 호출















예제코드 2 (사용자 정의 코드)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--
            Client2 client = new Client2("192.168.0.115");
            client.setHost("192.168.1.1");
            
            사용자가 임의 만들 함수
            Client2_init() : 객체 초기화
            Client2_close() : 객체 소멸자
    -->
    <bean id="client2" class="spring.Client2"
     init-method="Client2_init" destroy-method="Client2_close" >
        <constructor-arg value="192.168.0.115" />
        <property name="host" value="192.168.1.1" />
    </bean>
</beans>


public class Client2 {
    public Client2() {
        System.out.println("Client2 Default");
    }
    
    private String defaulthost;
    public Client2(String defaulthost){
        this.defaulthost = defaulthost;
        System.out.println("Client2 Overloading :" + this.defaulthost);
    }
    
    private String host;
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
        System.out.println("Client2 setHost() 호출 : " + this.host);
    }

    public void send(){
        System.out.println("데이터 보내기....");
    }
    
    //개발자가 임의로 만든 함수 > 객체의 초기화
    public void Client2_init(){
        System.out.println("사용자 정의 초기화 함수 호출");
    }
    //개발자가 임의로 만든 함수 > 객체의 소멸자
    public void Client2_close(){
        System.out.println("사용자 정의 소멸자 함수 호출");
    }
}


public class Program2 {

    public static void main(String[] args) {
        
    
        GenericXmlApplicationContext context =
                new GenericXmlApplicationContext("classpath:ApplicationContext2.xml");
        Client2 client = context.getBean("client2", Client2.class);
        client.send();
        context.close();        
    }
}




콘솔결과

Client2 Overloading :192.168.0.115
Client2 setHost() 호출 : 192.168.1.1
사용자 정의 초기화 함수 호출
데이터 보내기....
사용자 정의 소멸자 함수 호출



Comments