本文力求簡(jiǎn)潔,希望通過一個(gè)簡(jiǎn)單的demo應(yīng)用講解EasyJWeb與Guice容器的集成。

通過EasyJWeb提供的超級(jí)IoC容器,你可以非常輕松的把Guice容器集成進(jìn)來(lái),讓Guice來(lái)管理業(yè)務(wù)層的依賴關(guān)系,EasyJWeb只負(fù)責(zé)表現(xiàn)。我們看下面的配置:

<?xml version="1.0" encoding="utf-8"?>
<easyjf-web>
    
<modules inject="auto">
        
<module name="guice" path="/guice" form=""  scope="request" action="com.easyjf.demo.action.GuiceAction" defaultPage="index" >
            
<page name="index" url="/guice.html" type="template"/>
        
</module>
    
</modules>
    
<beans>    
        
<bean name="GuiceContainer" class="com.easyjf.container.impl.GuiceContainer" scope="singleton">
            
<property name="modules">
                
<list>
                    
<value>com.easyjf.demo.module.GuiceModule</value>
                
</list>    
            
</property>
            
<property name="stage">

                
<value>DEVELOPMENT</value>

            
</property>
        
</bean>
    
</beans>
</easyjf-web>

熟悉EasyJWeb的朋友都知道,上面的配置通過/guice.ejf便可以訪問GuiceAction業(yè)務(wù)。特別之處在于我們?cè)谶@里配置了一個(gè)“GuiceContainer”的bean,該bean負(fù)責(zé)集成EasyJWeb與Guice。屬性modules的每個(gè)類都要繼承Guice的AbstractModule類,以實(shí)現(xiàn)客戶化的裝配邏輯;stage屬性代表Guice容器的運(yùn)行環(huán)境,既生產(chǎn)環(huán)境或開發(fā)環(huán)境。下面我們看一下上面配置文件中GuiceModule的實(shí)現(xiàn):
/**
 * 
 * 
@author ecsoftcn@hotmail.com
 *
 * 
@version $Id: GuiceModule.java, 2007-4-23 上午03:31:33 Tony Exp $
 
*/

public class GuiceModule extends AbstractModule {

    
/* 
     * @see com.google.inject.AbstractModule#configure()
     
*/

    @Override
    
protected void configure() {
        
        
this.bindInterceptor(any(), annotatedWith(Logging.class), new LoggingInterceptor());

    }


}
至于這個(gè)類的具體裝配邏輯我在此不詳細(xì)描述,讀者只要知道是綁定攔截器就可以了,有興趣的可以參閱Guice的文檔。在configure方法中,你可以隨心所欲的實(shí)現(xiàn)任何客戶化的裝配邏輯。接下來(lái)我們?cè)倏匆幌律厦鍳uiceAction的實(shí)現(xiàn):
/**
 * 
 * 
@author ecsoftcn@hotmail.com
 * 
 * 
@version $Id: GuiceAction.java, 2007-4-23 上午03:15:47 Tony Exp $
 
*/

@SessionScoped
public class GuiceAction implements IWebAction {

    @Inject
    
private GuiceService    guiceService;

    
private int                count    = 0;

    
/*
     * @see com.easyjf.web.IWebAction#execute(com.easyjf.web.WebForm, com.easyjf.web.Module)
     
*/

    @Logging
    
public Page execute(WebForm form, Module module) throws Exception {

        count
++;

        form.addResult(
"message", guiceService.sayHelloToGuice());

        form.addResult(
"count", count);

        
return module.findPage("index");
    }


}
注意到上面的代碼中,GuiceAction實(shí)現(xiàn)了EasyJWeb的IWebAction接口,不同的地方是這個(gè)類中多了幾個(gè)Annotation:@SessionScoped,@Inject和@Logging,:@SessionScoped代表該類的作用域;@Inject代表該類引用了Guice容器中的一個(gè)bean[GuiceService];@Logging代表這個(gè)方法需要記錄日志,就是通過剛才的攔截器實(shí)現(xiàn)的。下面給出GuiceService極其實(shí)現(xiàn)類:
/**
 * 
 * 
@author ecsoftcn@hotmail.com
 *
 * 
@version $Id: GuiceService.java, 2007-4-23 上午03:16:20 Tony Exp $
 
*/

@ImplementedBy(GuiceServiceImpl.
class)
public interface GuiceService {
    
    
public String sayHelloToGuice();

}
實(shí)現(xiàn):
/**
 * 
 * 
@author ecsoftcn@hotmail.com
 *
 * 
@version $Id: GuiceServiceImpl.java, 2007-4-23 上午03:17:24 Tony Exp $
 
*/

@Singleton
public class GuiceServiceImpl implements GuiceService {

    
/* 
     * @see com.easyjf.demo.GuiceService#sayHelloToGuice()
     
*/

    @Logging
    
public String sayHelloToGuice() {
        
        System.out.println(
"Execute GuiceServiceImpl#sayHelloToGuice()");
        
        
return "Hello, Guice!";

    }


}
這兩個(gè)類中都使用了Guice的Annotation來(lái)聲明裝配原則,具體含義請(qǐng)參考Guice文檔。

到此為止,一個(gè)簡(jiǎn)單的Demo就算介紹完了,歡迎交流。