那麼這裡提供一個簡單的方法,首先先建立 AppContext.java
package org.util;
import org.springframework.context.ApplicationContext;
public class AppContext 
{
    private static ApplicationContext ctx;  
 
    public static void setApplicationContext(ApplicationContext applicationContext) {  
        ctx = applicationContext;  
    }  
 
    public static ApplicationContext getApplicationContext() {  
        return ctx;  
    }  
}
接著再透過一隻程式去實做 Spring 的 ApplicationContextAware 介面來達到 Spring 運用 XML 自動繫結
package org.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextProvider implements ApplicationContextAware {
 public void setApplicationContext(ApplicationContext ctx)
   throws BeansException {
  // TODO Auto-generated method stub
  AppContext.setApplicationContext(ctx);  
 }
}
最後在你的 applicationContext.xml 加入這隻程式的 bean 設定檔
<?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-2.5.xsd">
    <bean id="contextApplicationContextProvider" class="org.util.ApplicationContextProvider"></bean>  
</beans>
 
 
沒有留言:
張貼留言