resources > config.xml 파일 생성
→ 사용할 빈을 이곳에 설정해준다.
config.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="car" class="com.fastcampus.ch3.Car"/>
<bean id="engine" class="com.fastcampus.ch3.Engine"/>
<bean id="door" class="com.fastcampus.ch3.Door"/>
</beans>
→ ApplicationContext라는 저장소에서 getBean(key)를 통해 value를 가져올 수 있다.
byName
: getBean("car")
byName
: getBean("car", Car.class);
buType
: getBean(Car.class)
@Autowired
는 타입으로 찾지만, 타입이 여러개인 경우에는 이름으로 검색하기 때문에 타입이 여러개이어도 상관이 없다.기본적으로 SingpleTon이라 하나의 객체를 만들고 그것을 재사용하는데,
만약 매번 다른 객체를 생성해야한다면 다음과 같이 config.xml을 작성해주면된다.
<bean id="car" class="com.fastcampus.ch3.Car" scope="prototype"/>
<bean id="engine" class="com.fastcampus.ch3.Engine" scope="singleton"/> <!--기본타입-->
public static void main(String[] args) {
ApplicationContext ac = new GenericXmlApplicationContext("config.xml");
Car car = (Car) ac.getBean("car");
Engine engine = (Engine) ac.getBean("engine");
Door door = (Door) ac.getBean("door");
car.setColor("red");
car.setOil(100);
car.setEngine(engine);
car.setDoors(new Door[]{ac.getBean("door", Door.class), ac.getBean("door", Door.class), ac.getBean("door", Door.class)});
System.out.println("car = " + car);
}
→ property를 이용한다.
property 태그가 setter 메소드를 이용한다.
config.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="car" class="com.fastcampus.ch3.Car" scope="prototype">
<property name="color" value="red"/>
<property name="oil" value="100"/>
<property name="engine" ref="engine"/>
<property name="doors">
<array value-type="com.fastcampus.ch3.Door">
<ref bean="door"/>
<ref bean="door"/>
</array>
</property>
</bean>
<bean id="engine" class="com.fastcampus.ch3.Engine" scope="singleton"/>
<bean id="door" class="com.fastcampus.ch3.Door" scope="prototype"/>
</beans>
→ constructor-arg를 이용한다.
config.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="car" class="com.fastcampus.ch3.Car" scope="prototype">
<constructor-arg name="color" value="red"/>
<constructor-arg name="oil" value="100"/>
<constructor-arg name="engine" ref="engine"/>
<constructor-arg name="doors">
<array value-type="com.fastcampus.ch3.Door">
<ref bean="door"/>
<ref bean="door"/>
</array>
</constructor-arg>
</bean>
<bean id="engine" class="com.fastcampus.ch3.Engine" scope="singleton"/>
<bean id="door" class="com.fastcampus.ch3.Door" scope="prototype"/>
</beans>
Car 클래스에 무조건 생성자가 존재해야 한다.
Car class
class Car {
public Car(String color, int oil, Engine engine, Door[] doors) {
this.color = color;
this.oil = oil;
this.engine = engine;
this.doors = doors;
}
...
Bean이란, Spring Container가 관리하는 객체를 말한다.
Spring Container란, Bean 저장소로 Bean을 저장하고 관리(생성, 소멸, 연결)한다.
Spring Container의 Bean 관리
제어의 역전 IoC - 제어의 흐름을 전통적인 방식과 다르게 뒤바꾸는 것
Inversion of Control(제어의 역전)
(flow control: if, for)
사용이유: 코드 변경 줄이기
의존성 주입 DI - 사용할 객체를 외부에서 주입받는 것
Dependency Injection
인스턴스 변수(iv), setter, 참조형 매개변수를 가진 생성자, 메서드에 적용
@Autowired
public Car(@Value("red") String color, @Value("100") int oil, Engine engine, Door[] doors) {
this.color = color;
this.oil = oil;
this.engine = engine;
this.doors = doors;
}
class Car {
@Autowired
Engine engine;
@Autowired
Engine[] engines;
@Autowired(required = false)
SuperEngine superEngine;
@Autowired
public void setEngineAndDoor(Engine engine, Door[] doors) {
this.engine = engine;
this.doors = doors;
}
@Resource는 Spring Container에서 이름으로 빈을 검색해서 참조 변수에 자동 주입(DI)한다.
일치하는 이름의 빈이 없으면, 예외가 발생한다.
class Car {
@Resource(name="superEngine")
Engine engine;
여러개의 타입이 있으면 Qualifier로 구분
class Car {
@Autowired
@Qualifier("superEngine")
Engine engine;
이름 생략 가능
class Car {
@Resource
Engine engine;
<\component-scan>로 @Component가 클래스를 자동 검색해서 빈으로 등록