2008/03/03 14:45
[Spring] Annotation을 이용한 Spring과 Webwork Action간의 Injection
2008/03/03 14:45 in Framework/Spring

Spring는 Webwork와 연동이 된다.
하지만 Spring의 IoC를 이용하려면 Auto wire를 이용해야만 한다.
applicationContext*.xml에서 지정해서 사용할 수 없다는 뜻이 되겠다.
좀 더 자세히 설명하자면 Spring은 Webwork의 Action이 만들어 질때
BO같은 서비스 객체들을 생성할때 Spring의 Auto wire(자동으로 Injection되는)를 사용해야만 한다는 뜻이다.
이건 좀 맘에 안들어 다른 방법을 알아보았다.
Inject.java
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Inject {
public String beanId();
}
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Inject {
public String beanId();
}
이렇게 만들어 줌으로 인해...
아래와 같이 Action에서 Annotation을 지정해 줄 수 있다.
public class AccountMgrAction extends UserBaseAction {
private AccountService accountService;
@Inject(beanId="accountService")
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
...
private AccountService accountService;
@Inject(beanId="accountService")
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
...
한데 저렇게만 해준다고 Injection이 되는건 아니다... 당연히 구현해준게 하나도 없으니...
현재는 일단은 @Inject를 사용할 수 있다는 것이다.
이제 Injection해주는 부분을 구현해 주어야 한다.
Webwork의 interceptor부분에 넣어줄껀데... interceptor는 Action이 실행되기 전, 후에 실행되는 Class이다.
AOP의 개념이 좀 들어간 그런 클래스이다.
InjectInterceptor.java
public class InjectInterceptor implements Interceptor
// public만 리턴된다.
Method[] methods = invocation.getAction().getClass().getMethods();
for(Method method : methods)
{
doInject(invocation.getAction(), method);
}
private void doInject(Object action, Method method) throws Exception
{
Inject injectAnnotation = method.getAnnotation(Inject.class);
Object bo = SpringBeanFactory.getBean(injectAnnotation.beanId());
method.invoke(action, new Object[] {bo});
//여기서 Annotation이 선언된 action클래스의 setter메소드가 실행이 되어 bo가
//셋팅되는 것이다.
}
...
// public만 리턴된다.
Method[] methods = invocation.getAction().getClass().getMethods();
for(Method method : methods)
{
doInject(invocation.getAction(), method);
}
private void doInject(Object action, Method method) throws Exception
{
Inject injectAnnotation = method.getAnnotation(Inject.class);
Object bo = SpringBeanFactory.getBean(injectAnnotation.beanId());
method.invoke(action, new Object[] {bo});
//여기서 Annotation이 선언된 action클래스의 setter메소드가 실행이 되어 bo가
//셋팅되는 것이다.
}
...
이렇게 되면 Action이 호출되면....
그 전에 Interceptor(InjectInterceptor)가 실행이 되어 해당 Action의 Annotation을 파악하여
해당 setter에 bo객체를 Injection해주게 된다. 그리고 나서 Action이 실행되게 되므로...
Annotation을 이용해서 Injection을 하게 된다.
중요한건 Bo는 applicationContext*.xml에 bo나 dao같은 객체는 정의가 되어있어야 한다.
그래야 SpringBeanFactory.getBean(..)으로 가져올 것 아닌가...
'Framework > Spring' 카테고리의 다른 글
| [Spring] Annotation을 이용한 Spring과 Webwork Action간의 Injection (2) | 2008/03/03 |
|---|---|
| applicationContext-code.xml 설명 (0) | 2008/01/16 |
| applicationContext-jdbc.xml TransactionManager 설정 (0) | 2008/01/16 |
| ApplicationContextAware interface (0) | 2007/12/25 |
| InitializingBean interface (0) | 2007/12/25 |
| [Spring in Action - Struts] Struts와 통합(2) ContextLoaderPlugIn (0) | 2007/11/11 |





Prev
Rss Feed