실습 3일차_Injection_Callback
Spring Injection_Callback
ExampleMain.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ExampleMain {
public static void main(String[] args) {
// IoC Container
//ApplicationContext ctx =
GenericXmlApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/conf.xml");
ctx.close();
}
}
ExampleBean.java
package examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ExampleBean {
private static final Logger logger = LoggerFactory.getLogger(ExampleBean.class);
String mesg; // null
public ExampleBean() {
logger.info("ExampleBean 생성자 호출");
}
// setter 메서드 주입 ==> xml에서는 <property name = "mesg"
public void setMesg(String mesg) {
logger.info("setMesg 메서드 호출");
this.mesg = mesg;
}
public String getMesg() {
return mesg;
}
// init-method에서 호출
public void init() {
logger.info("init 메서드 호출");
}
// destroy-method에서 호출
public void cleanup() {
logger.info("cleanup 메서드 호출");
}
}
conf.xml
<?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="exampleInitBean" class="examples.ExampleBean" init-method="init" destroy-method="cleanup">
<property name="mesg" value="helloworld" />
</bean>
</beans>
* 출력 화면
Spring Injection_Callback
ExampleMain.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ExampleMain {
public static void main(String[] args) {
// IoC Container
//ApplicationContext ctx =
GenericXmlApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/conf.xml");
ctx.close();
}
}
ExampleBean.java
package examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class ExampleBean implements InitializingBean, DisposableBean{
private static final Logger logger = LoggerFactory.getLogger(ExampleBean.class);
String mesg; // null
public ExampleBean() {
logger.info("ExampleBean 생성자 호출");
}
// setter 메서드 주입 ==> xml에서는 <property name = "mesg"
public void setMesg(String mesg) {
logger.info("setMesg 메서드 호출");
this.mesg = mesg;
}
public String getMesg() {
return mesg;
}
// init-method에서 호출
public void init() {
logger.info("init 메서드 호출");
}
// destroy-method에서 호출
public void cleanup() {
logger.info("cleanup 메서드 호출");
}
@Override
public void afterPropertiesSet() throws Exception {
logger.info("afterPropertiesSet 메서드 호출");
}
@Override
public void destroy() throws Exception {
logger.info("destroy 메서드 호출");
}
}
conf.xml
<?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="exampleInitBean" class="examples.ExampleBean">
<property name="mesg" value="helloworld" />
</bean>
</beans>
* 출력 화면
Spring Injection_Callback
ExampleMain.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ExampleMain {
public static void main(String[] args) {
// IoC Container
//ApplicationContext ctx =
GenericXmlApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/conf.xml");
ctx.close();
}
}
ExampleBean.java
package examples;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class ExampleBean {
private static final Logger logger = LoggerFactory.getLogger(ExampleBean.class);
String mesg; // null
public ExampleBean() {
logger.info("ExampleBean 생성자 호출");
}
// setter 메서드 주입 ==> xml에서는 <property name = "mesg"
public void setMesg(String mesg) {
logger.info("setMesg 메서드 호출");
this.mesg = mesg;
}
public String getMesg() {
return mesg;
}
@PostConstruct
public void populateMovieCache() {
logger.info("@PostConstruct 메서드 호출");
}
@PreDestroy
public void clearMovieCache() {
logger.info("@PreDestroy 메서드 호출");
}
}
conf.xml
<?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-4.3.xsd">
<!-- @ 어노테이션 활성화 -->
<context:annotation-config />
<bean id="exampleInitBean" class="examples.ExampleBean">
<property name="mesg" value="helloworld" />
</bean>
</beans>
* 출력 화면
출처
+ 강의 교재
'AI Bootcamp > Spring' 카테고리의 다른 글
[Spring] 실습 4일차_@Resource (0) | 2022.03.18 |
---|---|
[Spring] 실습 3일차_I18N_MessageSource (0) | 2022.03.18 |
[Spring] 실습 2일차_Injection_Collection(list & set & map &props) (0) | 2022.03.12 |
[Spring] 실습 2일차_Injection_Constructor & Setter 혼합 (0) | 2022.03.12 |
[Spring] 실습 2일차_Injection_shortcut_c (0) | 2022.03.11 |