์ฝ๋ก๋ ํด์ ์ฆ์ผ๋ก ๊ธฐ๋ ฅ์์๊ณผ ์กฐ๊ธ๋ง ์์ง์ฌ๋ ์จ๋ชธ์ด ์ํ์ ๊ฒช๊ณ ์์ง๋ง, ์์์ ๊ณต๋ถํ๋ ๊ฑด ํฐ ๊ธฐ๋ ฅ์ด ๋ค์ง ์์ ์ด์ฐ์ ์ฐ ํ๊ณ ์๋ค. ์ค๋์ ๋ ธํธ๋ถ ์กด์ด ์๋ ์คํฐ๋์นดํ์์ ๊ณต๋ถ๋ฅผ ํ๋๋ฐ ์์ฃผ ์พ์ ํ๊ณ ์ข๋ค. ์ด๋ ํ์ผ ์ ๋ฒ๋ ๊ฐ ์๋์ง ์๋ฆฌ๋ฆญ- ํ๊ณ ์ธ์ด ์์ฐ์นํ์ ์ธ ๋๋์ด ๋ฌผ์ฌ๋๋ค. ํผ๊ณค์ ์ฉ์ด ๋คํฌ์ํด์ด ์ฝ๊น์ง ๋ด๋ ค์์ง๋ง ์ค๋๋ ํ์ดํ !!
๐งธ
๐งธ ๊ตฌ์กฐํ์ ๋ ์ธ ๊ผฌ์ฐ- ๐งธ
• lifecycle
ใด class : NetworkClient, beanLifeCycleTest
โ๏ธ ๊ฒฐ๊ณผ๊ฐ null
โบ BeanLifeCycleTest
@Configuration
static class LifeCycleConfig {
@Bean
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
@Configuration
: bean์ผ๋ก ๋ฑ๋กํ๊ธฐ์ํด ์ค์ ํด๋์ค์ ์ถ๊ฐํ๋ฉฐ bean์ด ๋ฑ๋ก๋ ๋ ์ฑ๊ธํค์ ๋ณด์ฅํด์ค๋ค.
@Bean
: ์คํ๋ง ์ปจํ ์ด๋์ ์ํด ๊ด๋ฆฌ๋๋ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ํํธ์จ์ด ์ปดํฌ๋ํธ(์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ๋ ๋ฆฝ๋ ์กฐ๊ฐ)์ด๋ค.
๋น์ NetworkClient ํ์ ์ networkClient๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง๊ณ ์๋ค. NetworkClient๋ ๊ฐ์ฒด ์์ฑ ํ ๋ฐ๋ก ๋ฐํ๋์ง ์๊ณ ์ ์ฐํ๊ณ ์ ์ฉํ๊ฒ ์ฌ์ฉํ๊ธฐ ์ํด ๊ฐ์ฒด๋ฅผ ๋ณ์์ ํ ๋นํ ํ ๋ฐํํ ๊ฒ์ด๋ค. ๊ทธ๋ ๊ฒ ๋ณ์ networkClient๋ฅผ ์ ์ธํ๋ค. NetworkClient๋ ๋ค๋ฅธ ํด๋์ค๋ก ์กด์ฌํ๋ฉฐ ๊ทธ ํด๋์ค ๋ด์๋ setter๋ฅผ ํตํด url์ ๋ฐ๊ณ ์ ํ ๊ฒ์ด๋ค. (๋ค๋ฅธ ํด๋์ค์ url์ ๋๊ธฐ๊ธฐ ์ํด์ ๋น์ ์ฌ์ฉํ๋ ๊ฒ!) ํด๋น url์ ์ค์ ํด์คํ networkClient๋ก ๋ฐํํ๋ฉด ๋๋ค.
Q. ๋ง์ฝ NetworkClient ํ์ ์ผ๋ก ๋น์ ์ค์ ํด๋๊ณ ํด๋์ค ์ด๋ฆ์ด NetWorkClient๋ก ๋ค๋ฅด๋ฉด ์ด๋ป๊ฒ ๋๋๊ฐ?
A. ๋น์ฐํ๊ฒ๋ ์ ๋๋ก ์ํ๋์ง ์๋๋ค. ๋น์ ํ์ ์ด ๋ค๋ฅด๊ฒ ์คํ๋ง ์ปจํ ์ด๋์ ๋ค์ด๊ฐ๊ธฐ ๋๋ฌธ์ ์๋๋๋ก ๋์๋์ง ์๋๋ค.
[Spring] @Configuration ์ด๋?
์ด๋ฒ ๊ธ์์๋ @Configuration์ ์ฌ์ฉํ๋ ์ด์ ์ ๋ํ์ฌ ์ ๋ฆฌํด๋ณผ๊น ํ๋ค. 1. @Configuration ์ด๋? " data-ke-type="html"> HTML ์ฝ์ ๋ฏธ๋ฆฌ๋ณด๊ธฐํ ์ ์๋ ์์ค Spring์์ Bean์ ์๋์ผ๋ก ๋ฑ๋กํ๊ธฐ ์ํด์๋, ์ค
blogshine.tistory.com
๐ข [Spring] ์คํ๋ง ๋น(Bean) ์ด๋?
๐ ์คํ๋ง ๋น(Bean) ์ด๋? ๋น(Bean)์ ์คํ๋ง ์ปจํ ์ด๋์ ์ํด ๊ด๋ฆฌ๋๋ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ํํธ์จ์ด ์ปดํฌ๋ํธ์ด๋ค. ์ฆ, ์คํ๋ง ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๋ ์๋ฐ ๊ฐ์ฒด๋ฅผ ๋ปํ๋ฉฐ, ํ๋ ์ด์์ ๋น(Bean)์ ๊ด๋ฆฌ
dev-wnstjd.tistory.com
โบ NetworkClient
public NetworkClient() {
System.out.println("์์ฑ์ ํธ์ถ, url = " + url);
connect();
call("์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง");
}
๊ฐ์ฅ ์ฒซ๋ฒ์งธ ์ฝ๋๋ก ์์ฑ์ ๋ถ๋ถ์์ connect( );๋ฅผ ์ํํ๋ค. ์์ง url์ setter๋ก ๋ฐ์์ค๊ธฐ์ connect( );๋ฅผ ์ํํ๊ธฐ ๋๋ฌธ์ ๋น์ฐํ๊ฒ๋ null ๊ฐ์ด ๋์ฌ ์ ๋ฐ์ ์๋ค.
Q. ์ด๊ธฐํ ์ฝ๋ฐฑ์ ํ๋ ์ด์ ๋?
A. ์คํ๋ง ๋ผ์ดํ ์ฌ์ดํด์ ์ ๋๋ก ์ง์ผ ์ค๋ฅ ๋ฐ์์ ๋ฐฉ์งํ๊ธฐ ์ํจ์ด๋ค.
โ๏ธ ์ด๊ธฐํ ์ฝ๋ฐฑ
โบ ์คํ๋ง ๋น์ ์ด๋ฒคํธ ๋ผ์ดํ ์ฌ์ดํด
์คํ๋ง ์ปจํ ์ด๋ ์์ฑ - ์คํ๋ง ๋น ์์ฑ - ์์กด๊ด๊ณ ์ฃผ์ - ์ด๊ธฐํ ์ฝ๋ฐฑ - ์ฌ์ฉ - ์๋ฉธ์ ์ฝ๋ฐฑ
: ์คํ๋ง ๋น์ด ์์ฑ๋ ํ ์์กด๊ด๊ณ ์ฃผ์ ์ด ์ด๋ค์ ธ์ผํ๋ค. ๊ทธ๋ฐ๋ฐ ์์ ์ฝ๋์ฒ๋ผ ์ฃผ์ ์ด ๋ชจ๋ ์ด๋ค์ง๊ณ url์ ๋ฐ์์ค์ง์๊ณ , ๋น์ด ์ฃผ์ ์ด ๋๊ธฐ ์ connect( )ํ๊ณ ์ ํ๋ค. ๊ฒฐ๊ณผ๊ฐ์ null๋ก ๋์ค๋ฉฐ ์๋๋๋ก ๋์ํ์ง ๋ชปํ๋ค. ๊ทธ๋ฐ ๋ถ๋ถ์ ์ค์ด๊ธฐ ์ํด ์ด๊ธฐํ ์ฝ๋ฐฑ์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค. ์ด๊ธฐํ ์ฝ๋ฐฑ์ ์ฝ๋์ ๋ฃ์ด ์์กด๊ด๊ณ๊ฐ ๋ชจ๋ ์ฃผ์ ๋๊ณ ๋์ ๊ทธ ํ์ ๋ค๋ฅธ ๋ถ๋ถ์ ๋์์ ์ํํ๋๋ก ํ๋ ๊ฒ์ด๋ค.
โบ ์ด๊ธฐํ ์ฝ๋ฐฑ์ ์ฅ์
1. ๋ผ์ดํ ์ฌ์ดํด์ ๋ฐ๋ผ ๋งค๋ฒ ๋น์ด ์ผ๊ด๋ ์ํ์์ ๋์ํ ์ ์๋๋ก ํด์ค๋ค.
2. ๋น์ด ํ๊ดด๋๊ธฐ ์ ๋ฆฌ์์ค๋ฅผ ์ ๋ฆฌํ ์๊ฐ์ ์ค๋ค.
3. ์ด๊ธฐํ ๋ก์ง์ ํ ๊ณณ์ ๋ชจ์๋ฌ์ ์ ์ง๋ณด์ํ๊ธฐ ์ข๋ค.
โ๏ธ ์ธํฐํ์ด์ค InitializingBean, DisposableBean
โบ NetworkClient
@Override
public void afterPropertiesSet() throws Exception {
connect();
call("์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง");
}
afterPropertiesSet( )
: ์ธํฐํ์ด์ค InitializingBean์ ์์๋ฐ์ผ๋ฉด ๊ทธ ์์ ์ ์๋ afterPropertiesSet ๋ฉ์๋๋ก ์์กด๊ด๊ณ ์ฃผ์ ์ด ๋๋๋ฉด ์ด๊ธฐํ๋ฅผ ์ง์ํ๋ค.
@Override
public void destroy() throws Exception {
disConnect();
}
destroy( )
: ์ธํฐํ์ด์ค DisposableBean์ ์์๋ฐ์ผ๋ฉด ๊ทธ ์์ ์ ์๋ destroy ๋ฉ์๋๋ก ๋น์ด ํ๊ดด๋ ๋ ๋ถ๋ฌ์จ๋ค.
๊ฒฐ๋ก : ์์๋ ์ธํฐํ์ด์ค์ ์ ์๋์ด์๋ ๋ฉ์๋๋ฅผ ๊ฐ์ ธ์ ์ฐ๊ธฐ ๋๋ฌธ์ ์ด๋ฆ์ ๋ณ๊ฒฝํ ์ ์๋ค. ๋ํ ์คํ๋ง์ ํนํ๋์ด์๋ ์ธํฐํ์ด์ค๋ผ์ ์คํ๋ง์ด ์ฌ์ฉ๋์ง ์๋ ์ธ๋ถ๋ผ์ด๋ธ๋ฌ๋ฆฌ์๋ ์ฌ์ฉํ ์ ์๋ค.
InitializingBean (Spring Framework 6.1.12 API)
Interface to be implemented by beans that need to react once all their properties have been set by a BeanFactory: e.g. to perform custom initialization, or merely to check that all mandatory properties have been set. An alternative to implementing Initiali
docs.spring.io
DisposableBean (Spring Framework 6.1.12 API)
Interface to be implemented by beans that want to release resources on destruction. A BeanFactory will invoke the destroy method on individual destruction of a scoped bean. An ApplicationContext is supposed to dispose all of its singletons on shutdown, dri
docs.spring.io
โ๏ธ ๋น ๋ฑ๋ก์ ์ด๊ธฐํ, ์๋ฉธ ๋ฉ์๋ ์ง์
โบ BeanLifeCycleTest
์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉํ๋ ๊ฒ์ ์ถ์ฒํ์ง ์๋๋ค.
@Bean(initMethod = "init", destroyMethod = "close")
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
initMethod
: ์ด๊ธฐํ ์ค์ ๋น ์ธ์คํฐ์ค์์ ํธ์ถํ ๋ฉ์๋์ ์ ํ์ ์ด๋ฆ์ด๋ค.
destroyMethod
: ์ดํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ๋ฅผ ๋ซ์ ๋ ๋น ์ธ์คํด์ค์์ ํธ์ถํ ๋ฉ์๋์ ์ ํ์ ์ด๋ฆ์ด๋ค. ๋น ๋ฉ์๋์์ ๋ฐํ๋๋ ๊ฐ์ฒด์ ๋ํด destroy๊ฐ ์๋์ง ์ ์ถํ๋ฉฐ close( ), shutdown( ) ์ด๋ผ๋ ์ด๋ฆ์ ์ธ์ ์๋ ๊ณต๊ฐ ๋ฉ์๋๋ง ๊ฐ์งํ์ฌ ์๋์ ์ผ๋ก ์คํํ๋ค.
- initMethod์ destroyMethod ์ถ์ฒ
Bean (Spring Framework 6.1.12 API)
The optional name of a method to call on the bean instance upon closing the application context, for example a close() method on a JDBC DataSource implementation, or a Hibernate SessionFactory object. The method must have no arguments but may throw any exc
docs.spring.io
โบ NetworkClient
public void init() {
System.out.println("NetworkClient.init");
connect();
call("์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง");
}
public void close() {
System.out.println("NetworkClient.close");
disConnect();
}
init( )
: initMethod๋ฅผ ํธ์ถํ ์ ํ์ ์ด๋ฆ์ด ๋๊ฒ ๋ค.
close( )
: destroyMethod๋ฅผ ํธ์ถํ ์ ํ์ ์ด๋ฆ์ด ๋๊ฒ ๋ค.
์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ฌ์ฉํ ์ ์๋ค. ๊ทธ ์ด์ ๋ ์คํ๋ง ์ฝ๋์ ์์กดํ๋ ๊ฒ์ด ์๋, ์ค์ ์ ๋ณด์ ์์กดํ๊ธฐ ๋๋ฌธ์ด๋ค. ํ์ง๋ง ์คํ๋ง์์ ๊ถ๊ณ ํ๋ ๋ฐฉ์์ ์๋๋ค. ๋ ๋์ ๋ฐฉ๋ฒ์ด ์๊ธฐ ๋๋ฌธ๋ ์์ง๋ง ์ด๋ฆ์ ๋ช ์ํ๋ ๊ฒ์์ ์ด๋ฆ์ด ๋ฌ๋ผ์ง ๊ฒฝ์ฐ ์ปดํ์ผ ์ค๋ฅ๋ฅผ ์ฐพ๋ ๊ฒ์ด ์ด๋ ต๊ธฐ ๋๋ฌธ์ด๋ค. (์ ์ง๋ณด์ ์ธก๋ฉด์์๋)
โ๏ธ ์ ๋ ธํ ์ด์ @PostConstruct, @PreDestroy
โบ NetworkClient
์คํ๋ง์์ ๊ฐ์ฅ ๊ถ์ฅํ๋ ๋ฐฉ์์ด๋ค.
@PostConstruct
public void init() {
System.out.println("NetworkClient.init"); connect();
call("์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง");
}
@PreDestroy
public void close() {
System.out.println("NetworkClient.close");
disConnect();
}
@PostConstruct
: ์์กด์ฑ ์ฃผ์ ์ด ์๋ฃ๋ ํ ์คํ๋๋ ๋ฉ์๋์ ์ฌ์ฉํ๋ฉฐ ์ฃผ์์ด ๋ฌ๋ฆฐ ๋ฉ์๋๋ ํ๋๋ง ๊ฐ๋ฅํ๋ค.
@PreDestroy
: ์ธ์คํด์ค๊ฐ ์ ๊ฑฐ๋๊ณ ์์์ ์๋ฆฌ๋ ์ฝ๋ฐฑ ์๋ฆผ์ด๋ฉฐ ์ฃผ์์ด ๋ฌ๋ฆฐ ๋ฉ์๋๋ ๋ณด์ ํ๊ณ ์๋ ๋ฆฌ์์ค๋ฅผ ํด์ ํ๋๋ฐ ์ฌ์ฉํ๋ค.
PostConstruct (Java(TM) EE 7 Specification APIs)
JavaScript is disabled on your browser. @Documented @Retention(value=RUNTIME) @Target(value=METHOD) public @interface PostConstruct The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any
docs.oracle.com
PreDestroy (Java(TM) EE 7 Specification APIs)
JavaScript is disabled on your browser. @Documented @Retention(value=RUNTIME) @Target(value=METHOD) public @interface PreDestroy The PreDestroy annotation is used on methods as a callback notification to signal that the instance is in the process of being
docs.oracle.com