๐ข [Spring] ์คํ๋ง ๋น(Bean) ์ด๋?
๐ ์คํ๋ง ๋น(Bean) ์ด๋?
๋น(Bean)์ ์คํ๋ง ์ปจํ ์ด๋์ ์ํด ๊ด๋ฆฌ๋๋ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ํํธ์จ์ด ์ปดํฌ๋ํธ์ด๋ค.
์ฆ, ์คํ๋ง ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๋ ์๋ฐ ๊ฐ์ฒด๋ฅผ ๋ปํ๋ฉฐ, ํ๋ ์ด์์ ๋น(Bean)์ ๊ด๋ฆฌํ๋ค.
๋น์ ์ธ์คํด์คํ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ฉฐ, ์คํ๋ง ์ปจํ ์ด๋์ ๋ฑ๋ก๋ ๊ฐ์ฒด๋ฅผ ์คํ๋ง ๋น์ด๋ผ๊ณ ํ๋ค.
์ฝ๊ฒ ์ดํดํ์๋ฉด new ํค์๋ ๋์ ์ฌ์ฉํ๋ค๊ณ ๋ณด๋ฉด๋๋ค.
<bean id="helloService" class="com.example.myapp.di.HelloService"/>
IHelloService helloService = new IHelloService()
IHelloService ๋ผ๋ ์ธํฐํ์ด์ค๊ฐ ์กด์ฌํ๊ณ helloService ์ธ์คํด์ค๋ฅผ ๋ง๋ค๊ธฐ ์ํด์๋ new ๋ผ๋ ํค์๋๋ฅผ ์ฌ์ฉํด ์ธ์คํด์ค๋ฅผ ์์ฑํ์๋ค.
์คํ๋ง ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๋๋ก ํ๋ ๋ฐฉ๋ฒ์ ๋น ํ๊ทธ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
์ด๋ ๊ฒ xml ํ์ผ ๋น ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ฉด helloService ์ธ์คํด์ค๋ฅผ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๊ฒ ๋๋ค.
โ ์คํ๋ง ๋น(Bean) ์ฌ์ฉ์ด์ ?
๊ฐ์ฅ ํฐ ์ด์ ๋ ์คํ๋ง ๊ฐ ๊ฐ์ฒด๊ฐ ์์กด๊ด๊ณ๋ฅผ ๊ด๋ฆฌํ๋๋ก ํ๋ ๊ฒ์ ๊ฐ์ฅ ํฐ ๋ชฉ์ ์ด ์๋ค. ๊ฐ์ฒด๊ฐ ์์กด๊ด๊ณ๋ฅผ ๋ฑ๋กํ ๋ ์คํ๋ง ์ปจํ ์ด๋์์ ํด๋นํ๋ ๋น์ ์ฐพ๊ณ , ๊ทธ ๋น๊ณผ ์์กด์ฑ์ ๋ง๋ ๋ค.
๐ ์คํ๋ง ๋น(Bean) ๋ฑ๋ก ๋ฐฉ๋ฒ
Spring Bean์ ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ์ ๋ํ์ ์ผ๋ก 3๊ฐ์ง ์ ๋๊ฐ ์๋ค.
- xml์ ์ง์ ๋ฑ๋ก
- @Bean ์ด๋ ธํ ์ด์ ์ ์ด์ฉ
- @Component, @Controller, @Service, @Repository ์ด๋ ธํ ์ด์ ์ ์ด์ฉ
1. XML์ ์ง์ ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ
<bean> ํ๊ทธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
<bean id="helloService" class="com.example.myapp.di.HelloService"/>
<bean id="helloController" class="com.example.myapp.di.HelloController" p:helloService-ref="helloService">
</bean>
application-config.xml ์ resource ์๋์ ๋ง๋ค์ด ์ค๋ค. ๊ทธ๋ฆฌ๊ณ <beans> ํ๊ทธ ์์ <bean>์ ๋ง๋ค์ด ์ค๋ค. ์์ ์ฝ๋๋ ์์กด์ฑ ์ฃผ์ ๊น์ง ํ ์ฝ๋์ด๋ค.
2. @Bean ์ด๋ ธํ ์ด์ ์ ์ด์ฉ
package com.example.myapp.di;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@Configurable
@ComponentScan(basePackages= {"com.example.myapp"})
@ImportResource(value= {"classpath:application-config.xml"})
public class AppConfig {
@Bean
public IHelloService helloService() {
return new HelloService();
}
@Bean
public HelloController helloController() {
HelloController controller = new HelloController();
controller.setHelloService(helloService());
return controller;
}
}
์ธ์คํด์ค๋ฅผ ๋ฐํํ๊ณ ๋น์ ๋ฑ๋กํ๋ ์ฝ๋์ด๋ค.
๋ฉ์๋ ์์ @Beanํ๊ทธ๋ฅผ ์ฌ์ฉํ๊ณ AppConfig ๊ฐ์ฒด ์์ @Configurable, @ComponentScan, @ImportResource ์ด๋ ธํ ์ด์ ์ ์ ์ธํด์ค๋ค.
์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๊ธฐ ์ํด ๊ผญ application-xml์์ context๋ฅผ ์ถ๊ฐํด์ฃผ๋ ๊ฒ์ ๊น๋จน์ง ๋ง์ !!
package com.example.myapp.di;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class HelloMain {
public static void main(String[] args) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println("-----------------------");
HelloController controller = context.getBean("helloController", HelloController.class);
controller.hello("ํ๊ธธ๋");
System.out.println("=====================");
context.close();
}
}
๊ทธ๋ฆฌ๊ณ ๋ฉ์ธ์์ Annotation์ ์ฌ์ฉํ๊ธฐ ์ํด AnnotationConfigApplicationContext ๋ฅผ ์ฌ์ฉํ๋ค.
3. @Component, @Controller, @Service, @Repository ์ด๋ ธํ ์ด์ ์ ์ด์ฉ
<?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"
xmlns:p="http://www.springframework.org/schema/p"
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:component-scan base-package="com.example.myapp.hr"/>
</beans>
application-config.xml ํ์ผ
์ฌ๊ธฐ์ namespace์์ context ์ถ๊ฐํ๊ณ ์๋์ context๋ฅผ ์ถ๊ฐํด์ค๋ค.
package com.example.myapp.hr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
@Controller
public class EmpController {
private IEmpService empService;
@Autowired
public EmpController(IEmpService empService) {
this.empService = empService;
}
void printInfo() {
int count = empService.getEmpCount(50);
System.out.println("์ฌ์์ ์ : " + count);
}
}
@Controller์ @Autowired๋ฅผ ์ด์ฉํด ์์กด์ฑ ์ฃผ์ ๊น์ง ํ ์ฝ๋์ด๋ค.