ApplicationContext 인터페이스는
BeanFactory의 모든 기능 제공(BeanFactory 인터페이스를 상속)
권장하는 방식으로 스프링 DI 뿐 아니라 트랜잭션, AOP 서비스, 국제화를 위한 메시지 소스, 애플리케이션 이벤트 처리
Spring Framework2.5 이상부터 ApplicationContext 설정시 자바 애노테이션을 지원
XML ? Annotation 방식
- XML 설정 방식을 쓰면 모든 설정을 자바코드로 부터 외부 XML에 둠
- 애노테이션 방식의 경우 자바코드 내에서 DI 설정을 정의
- 둘 동시에 사용되는 경우 XML 설정이 우선
- 주로 데이터소스, 트랜잭션매니저, JMX, JMS커넥션팩터리등 앤플리케이션
인프라스트럭처에는 XML 파일에 저장하고 DI 설정은 애노테이션에서 정의.
Context 시작 시 모든 Singleton Bean을 미리 로딩(preloading) 시킴->a 초기에 설정 및 환경에 대한 에러 발견 가능함
다수의 ApplicationContext 구현 클래스 제공, 다양한 Resource 로딩 방법 제공
(XmlWebApplicationContext,FileSystemXmlApplicationContext,ClassPathXmlApplicationContext)
org.springframework.context
Spring의 AOP기능, 메시지 자원 핸들링, 이벤트 위임, 웹 어플리케이션에서 사용하기 위한 WebApplicationContext와 같은 특정 어플리케이션 컨텍스트를 이용한 통합과 같은 다른 기능을 추가 제공한다
<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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Example configuration to get you started.</description>
<context:annotation-config/>
<context:component-scan base-package="onj.mall.app" />
</beans>
- beans를 기본 네임스페이스로 설정
- context 네임스페이스 : ApplicationContext 설정지원
- p 네임스페이스 : 세터주입을 쉽게 하기 위한 DI설정 제공
- c 네임스페이스 : 생성자주입을 쉽게 하기 위한 DI설정 제공
- util 네임스페이스 : DI설정에 유용한 유틸리티 제공
아래는 그 예제 이니 참고하자.
1
[CarMaker 인터페이스]
package onj.mall.app;
public interface CarMaker {
public Car sell(Money money);
}
[Car 클래스]
package onj.mall.app;
public class Car {
private String name;
public Car(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
[Money클래스]
package onj.mall.app;
import org.springframework.stereotype.Service;
@Service("money")
public class Money {
private int amount;
public Money() {}
public Money(int amt) {
this.amount = amt;
}
public int getAmount() {
return this.amount;
}
public void setAmount(int amt) {
this.amount = amt;
}
}
[Audi 클래스]
package onj.mall.app;
import org.springframework.stereotype.Service;
@Service("audi")
public class Audi implements CarMaker{
public Car sell(Money money) {
money.setAmount(1000);
Car car = new Car("Audi A6");
return car;
}
}
[/META-INF/spring/app-context.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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Example configuration to get you started.</description>
<context:annotation-config/>
<context:component-scan base-package="onj.mall.app" />
</beans>
[AnnotationExam.java]
package onj.mall.app;
import org.springframework.context.support.GenericXmlApplicationContext;
public class AnnotationExam {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:META-INF/spring/app-context.xml");
ctx.refresh();
CarMaker carMaker = ctx.getBean("audi", Audi.class);
Money money = ctx.getBean("money", Money.class);
money.setAmount(1000);
Car c = carMaker.sell(money);
System.out.println("I sold a car... " + c.getName());
}
}
[결과]
I sold a car... Audi A6
![]() | ![]() ![]() | 12-27 | 2810 | |
![]() | ![]() ![]() | 12-11 | 2043 | |
53 | ![]() ![]() | 03-15 | 1867 | |
52 | ![]() ![]() | 01-31 | 2024 | |
51 | ![]() ![]() | 01-31 | 2988 | |
50 | ![]() ![]() | 01-31 | 1560 | |
49 | ![]() ![]() | 01-19 | 1882 | |
48 | ![]() ![]() | 01-11 | 1756 | |
47 | ![]() ![]() | 01-03 | 2332 | |
46 | ![]() ![]() | 12-27 | 2810 | |
45 | ![]() ![]() | 12-19 | 1969 | |
44 | ![]() ![]() | 12-14 | 1949 | |
43 | ![]() ![]() | 12-11 | 2043 | |
42 | ![]() ![]() | 12-09 | 1589 | |
41 | ![]() ![]() | 12-01 | 1813 | |
40 | ![]() ![]() | 12-01 | 2039 | |
39 | ![]() ![]() | 12-01 | 1465 |
댓글 없음:
댓글 쓰기