Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- JavaScript
- 암호화
- Servlet
- docker
- autowired
- 알고리즘
- model1
- jQuery
- 블록체인
- websocket
- 배포
- SQL
- 비트코인
- PL/SQL
- 웹게임
- 도커
- express
- 웹소켓
- tiles.xml
- node.js
- Cookie
- Spring
- JSP
- RDS
- HTML
- Ajax
- phaser
- AWS
- EC2
- CSS
Archives
- Today
- Total
記錄
Spring) bean's Life Cycle (bean의 생명주기) 본문
예제코드 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
사용자 정의 초기화 함수 호출
데이터 보내기....
사용자 정의 소멸자 함수 호출
'Web > Spring framework' 카테고리의 다른 글
Spring) Spring Framework MVC Basic Flow (0) | 2018.04.30 |
---|---|
Spring) prototype을 이용한 새로운 객체 생성 (0) | 2018.04.30 |
Spring) @Configuration을 사용한 xml의 대체 (0) | 2018.04.28 |
Spring) @Resource의 활용 (0) | 2018.04.28 |
Spring) @Autowired의 활용 ( + @Qualifier 사용) (0) | 2018.04.28 |
Comments