์คํ๋ง ํ๋ ์์ํฌ
์๋ฐ ๊ธฐ๋ฐ์ ์ ํ๋ฆฌ์ผ์ด์ ํ๋ ์์ํฌ๋ก ์ํฐํ๋ผ์ด์ฆ๊ธ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ๊ธฐ ์ํ ๋ค์ํ ๊ธฐ๋ฅ ์ ๊ณต
์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์ ํ์ํ ๊ธฐ๋ฐ์ ์ ๊ณตํด์ ๊ฐ๋ฐ์๊ฐ ๋น์ฆ๋์ค ๋ก์ง ๊ตฌํ์๋ง ์ง์คํ ์ ์๊ฒ ํ๋ค
์คํ๋ง์ ํน์ง๊ณผ ๊ตฌ์กฐ
- ์ ์ด์ญ์ (IoC : inversion of control)
์คํ๋ง์ด ํ์ํ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ์ ๊ณตํด์ค๋ค.
- ์์กด์ฑ ์ฃผ์ (DI: dependency injection)
์ ์ด์ญ์ ๋ฐฉ๋ฒ ์ค ํ๋. ๊ฐ์ฒด ์ง์ ์์ฑ ์ ํ๊ณ ์ธ๋ถ ์ปจํ ์ด๋๊ฐ ์์ฑํ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ๋ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ์
@Autowired ์ด๋ ธํ ์ด์ ์ ํตํด ์์กด์ฑ ์ฃผ์ ๊ฐ๋ฅ
- ์์กด์ฑ ์ฃผ์ ์ผ๋ก ๋ณ๊ฒฝ
@Controller
public class DIController {
MyService myService;
@Autowired
public DIController(MyService myService) {
this.myService = myService;
}
}
- ํ๋ ๊ฐ์ฒด ์ ์ธ์ ํตํ ์์กด์ฑ ์ฃผ์
@Controller
public class FieldInjectionController {
@Autowired
private MyService myService;
}
- setter ๋ฉ์๋๋ฅผ ํตํ ์์กด์ฑ ์ฃผ์
@Controller
public class SetterInjectionController {
MyService myService;
@Autowired
public void setMyService(MyService myService) {
this.myService = myService;
}
}
IoC : DL(dependency lookup), DI(dependency injection)๋ก ๋๋ ์ง๋ค.
๋์ ๊ฐ์ฒด ์์ฑํ๋ ๊ฑด ๊ฐ์ ๋ฐ ๋ฐ์์ค๋ ๋ฐฉ์์ด ๋ค๋ฅธ๊ฑฐ.
DL : ๋ฐ์์ค๋ฉด ๋จ.
DI : setter injection, constructor injection, method injection ์ผ๋ก ๋๋์ด์ง. ๋ฉค๋ฒ๋ณ์๋ก๋ ๋จ.
Spring IoC ์ปจํ ์ด๋ : ์๋ธ๋ฆฟ ์ปจํ ์ด๋์ ๋น์ทํจ.
ApplicationContext context = new ClassPathXmlApplicationContext("๋น์ค์ XMLํ์ผ");
ApplicationContext context = new AnnotationConfigApplicationContext(๋น์ค์ ํด๋์ค๊ฐ์ฒด);
spring IoC ์ปจํ ์ด๋ ์ด๊ธฐํํด์ค
DL : ๋ฌ๋ผ๊ณ ํ ๋๋ง๋ค ์ ๋ฌ
DI : ๋ฏธ๋ฆฌ ๋ฌ๋ผ๊ณ ํจ.
scope="prototype"
๋ํดํธ๋ ์ฑ๊ธํค์ด๊ณ prototype์ผ๋ก ํ๋ฉด ์์ฑํ ๋๋ง๋ค ๋ง๋ค์ด์ง๋ค.
์์ฒญ์๋ง๋ค ๊ฐ์ฒด ์์ฑ์ด ๋๋ค.. (ex. ์๊ฐ์ ์์ฒญํ ๋ ์ฐ์ผ์ ์์) ์ฑ๊ธํค์ผ๋์ ๋ฌ๋ฆฌ
๋ฏธ๋ฆฌ ๊ฐ์ฒด ์์ฑํ์ง ์์(์ฑ๊ธํค์ผ๋๋ ๋ฏธ๋ฆฌ ๊ฐ์ฒด ์์ฑ)
<bean id="foo2" class="sample2.Foo">
<constructor-arg value="ABC"/>
<constructor-arg value="100"/>
</bean>
foo์๋ ๋ฌธ์์ดํ๋, ์ซ์ํ ํ๋๋ฅผ ๋ฐ๋ ํจ์๊ฐ ์๋ค.
<bean id="foo4" class="sample2.Foo">
<constructor-arg ref="bar"/>
</bean>
<bean id="bar" class="sample2.Bar"/>
์ฐธ์กฐ๊ฐ ์ ๋ฌํ ๋๋ ref์ ์ ๋ฌํ๋ค..
์ด๋ ๊ฒ xmlํ์ผ์ ๊ฐ์ฒด ์์ฑ ์จ๋์ผ๋ฉด ์์ ํ๊ธฐ ํธํจ. ๊ฐ๋ณ๊ณ .
getBean์ ๋ฆฌํด ๊ฐ์ object์ด๋ค.
xmlํ์ผ์
<bean id="userService" class="sample3.UserServiceImpl" scope="prototype"/>
<bean id="obj1" class="sample3.UserVo">
<constructor-arg value="Dooly"/>
</bean>
์ด๋ฐ์์ผ๋ก ์์ฑํ๊ณ
UserService u1=(UserService)factory.getBean("userService");
UserVo vo = (UserVo)factory.getBean("obj1");
u1.addUser(vo);
์ด๋ฐ์์ผ๋ก java ํ์ผ์์ ๋ฐ์์จ๋ค.
UserService u2=factory.getBean("userService", UserService.class);
์ด๋ ๊ฒ usersevice.class๋ผ๊ณ ํ๋ฉด ํ ๋ณํ์ํด๋ ๋๋ค.
factory-method="getInstance"
์ด ์์ฑ์์ผ๋ฉด ๋ฌด์กฐ๊ฑด ์์ฑ์๋ก ๊ฐ์ฒด์์ฑํ๋ค. ํฉํ ๋ฆฌ๋ฉ์๋์์์๋ง ๊ฐ์ฒด ์์ฑํ ์ ์๋ค..
public abstract class AbstractTest {
public static AbstractTest getInstance(){
<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 ์ค์ ํ์ผ..
constructor-arg ์์ผ๋ฉด ์๊ท๋จผํธ ์๋ ์์ฑ์๊ฐ ํธ์ถ๋๋ค.
<bean> ํ๊ทธ:
id: ์ฃผ์ ๋ฐ๋ ๊ณณ์์ ํธ์ถํ ์ด๋ฆ
class: ์ฃผ์ ํ ๊ฐ์ฒด์ ํด๋์ค๋ช
factory-method: ๊ฐ์ฒด ์์ฑ์ ์ฌ์ฉ๋ factory ๋ฉ์๋
scope: bean ๊ฐ์ฒด์ ์ ํจ๋ฒ์ ์ค์ (singleton, prototype ๋ฑ๋ฑ )
<property> ํ๊ทธ:
name ์์ฑ: ๊ฐ์ฒด๋๋ ๊ฐ์ ์ฃผ์ ํ property ์ด๋ฆ์ ์ค์
@Component ๋ฅผ ์ฃผ๋ฉด ์์์ bean ๊ฐ์ฒด๋ก ๋ฑ๋กํ๋ค.
<context:component-scan base-package="ํด๋น ํจํค์ง ์ด๋ฆ" />
@Component
@Scope(value="prototype")
์ด๋ฐ์์ผ๋ก ์ค์ ํ ์ ์๋ค.
@Qualifier ๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ผํ ํ์ ์ ๋น ์ค ํน์ ์ด๋ฆ์ bean์ ์ฃผ์ ํ๋๋ก ํ ์ ์๋ค.
@Autowired
@Qualifier("mytest")
private Test test1;
@Autowired
@Qualifier("mytest")
private Test test2;
์ด๋ฐ์์ผ๋ก..
@Resource
autowired์ qualifier๋ฅผ ํ๋๋ก ๋ฌถ์ด๋์ ๊ฑฐ์ ๋น์ทํ๋ค.
@Inject
์คํ๋ง3๋ถํฐ ์ฌ์ฉ๊ฐ๋ฅ. ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์์ด์ผ ํ๋ค.
@Autowired : ๋ฉค๋ฒ๋ณ์, setter ๋ฉ์๋, ์์ฑ์, ์ผ๋ฐ ๋ฉ์๋์ ์ ์ฉ ๊ฐ๋ฅ (์ฐ๊ฒฐ๋ฐฉ์ : ํ์ ์ ๋ง์ถฐ ์ฐ๊ฒฐ)
@Resource : ๋ฉค๋ฒ๋ณ์, setter ๋ฉ์๋์ ์ ์ฉ๊ฐ๋ฅ (์ฐ๊ฒฐ๋ฐฉ์ : ํ์ ์ ๋ง์ถฐ ์ฐ๊ฒฐ)
@Inject : ๋ฉค๋ฒ๋ณ์, setter ๋ฉ์๋, ์์ฑ์, ์ผ๋ฐ ๋ฉ์๋์ ์ ์ฉ ๊ฐ๋ฅํ๋ค. (์ฐ๊ฒฐ๋ฐฉ์ : ์ด๋ฆ์ผ๋ก ์ฐ๊ฒฐ)
@Component("myFood")
ํด๋์ค ์์ ์ด๋ ๊ฒ ํ๋ ๊ฑด ์ด ํด๋์ค๋ bean์ผ๋ก ๋ง๋ค์ด๋ฌ๋ผ๋ ๋ป. myFood๋ผ๋ ์ด๋ฆ์ผ๋ก ๋ง๋ค์ด๋ฌ๋ผ. ์ ์ ์ผ๋ฉด ํด๋์ค์ ์ด๋ฆ์ค ์ฒซ๊ธ์๋ฅผ ์๋ฌธ์๋ก ๋ฐ๊พผ bean ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ค๋ค.
<bean id="favoriteFood" class="sampleanno1.Food" >
..
</bean>
<bean id="unFavoriteFood" class="sampleanno1.Food">
..
</bean>
์ด๋ฐ ์์ผ๋ก ๊ฐ์ผ๋ฉด id๋ก ๊ตฌ๋ถํด์
@Autowired
private Food favoriteFood;
@Autowired
private Food unFavoriteFood;
autowired๋ฅผ ํด์ค์ ๋ฐ๋ก ๋ค์ด๊ฐ๊ฒ ๋๋ค.
<context:component-scan base-package="sampleanno1"/>
์ด๋ฐ ์์ผ๋ก component-scan์ ์ ํด์ฃผ๋ฉด
package๊ฐ sampleanno1์ธ ๊ฒ์ ์ค์บํด์ค๋ค.. ์ด๋ context๋ฅผ ์ด์ฉํ๋ ค๋ฉด bean ์ค์ ํ์ผ์
xmlns:context="http://www.springframework.org/schema/context"
์ด ์ฝ๋๋ฅผ ๋ฃ์ด์ค์ผํ๋ค.
@Component // name = vestTire
public class VestTire implements Tire{
์ด๋ฐ๋ค๋ฉด ์ด VestTire๋ฅผ bean์ผ๋ก ๋ฑ๋กํด์ฃผ๋๋ฐ, ๋ํดํธ ์ด๋ฆ(vestTire)์ผ๋ก ๋ฑ๋กํด์ค๋ค.
@Autowired(required = false)
public Car(Tire tire) {
System.out.println("Car ๊ฐ์ฒด ์์ฑ - "+tire);
}
vestTire ๊ฐ์ฒด๊ฐ ์๋ค๋ฉด ์ฃผ์ ํ๊ณ ์์ผ๋ฉด ๋ง์๋ผ๋ผ๋ ๋ป์ด๋ค. required๊ฐ false์ด๋ฏ๋ก..
์๋ฐ ๋ด์ฅ ๊ฐ์ฒด๋ annotation ์ธ์ ์์ผ๋ฏ๋ก <bean> ํ๊ทธ๋ฅผ ์จ์ผํ๋ค.
<bean id="userName" class="java.lang.String">
<constructor-arg value="์๊ธฐ"" />
</bean>
'์๊ธฐ'๋ผ๋ string ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
'๐ผ Full-Stack Study โก (feat. KOSA)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring Boot (1) | 2023.10.06 |
---|---|
Spring Boot & Thymeleaf (1) | 2023.10.05 |
AJAX (1) | 2023.10.03 |
ajax (1) | 2023.10.02 |
ServletJSP & MVC (0) | 2023.09.27 |