This article strives to be concise and hopes to explain the integration of easyjweb and guice containers through a simple demo application.
With the super IOC container provided by easyjweb, you can easily integrate the guice container to manage the dependencies at the business layer. easyjweb is only responsible for the performance. Let's look at the following Configuration:
<? 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>
Anyone familiar with easyjweb knows that the configuration above can access the guiceaction service through/guice. EJF. Specifically, we have configured a "guicecontainer" bean here, which is responsible for integrating easyjweb with guice. Each class of the attribute modules inherits the abstractmodule class of guice to implement custom Assembly logic. The stage attribute represents the runtime environment of the guice container, both the production environment and the development environment. Next, let's take a look at the implementation of the guicemodule in the above configuration file :/***//**
*
* @ Author ecsoftcn@hotmail.com
*
* @ Version $ ID: guicemodule. Java, 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 ());
}
}
As for the specific Assembly logic of this class, I will not describe it in detail here. The reader only needs to know that it is bound with an interceptor. If you are interested, refer to the guice documentation. In the configure method, you can implement any customized Assembly logic as you like. Next, let's take a look at the implementation of the above guiceaction :/***//**
*
* @ Author ecsoftcn@hotmail.com
*
* @ Version $ ID: guiceaction. Java, 03:15:47 Tony exp $
*/
@ Sessionscoped
Public class guiceaction implements iwebaction {
@ Inject
Private 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) throws exception {
Count ++;
Form. addresult ("message", guiceservice. sayhellotoguice ());
Form. addresult ("count", count );
Return Module. findpage ("Index ");
}
}
Note that in the above Code, guiceaction implements the iwebaction interface of easyjweb. The difference is that there are several more annotation: @ sessionscoped, @ inject and @ logging in this class ,: @ sessionscoped indicates the scope of the class; @ inject indicates that the class references a bean [guiceservice] In the guice container; @ logging indicates that the method needs to record logs, which is implemented through the interceptor just now. The following shows the extreme implementation class of guiceservice :/***//**
*
* @ Author ecsoftcn@hotmail.com
*
* @ Version $ ID: guiceservice. Java, 03:16:20 Tony exp $
*/
@ Implementedby (guiceserviceimpl. Class)
Public interface guiceservice {
Public String sayhellotoguice ();
}
Implementation :/***//**
*
* @ Author ecsoftcn@hotmail.com
*
* @ Version $ ID: guiceserviceimpl. Java, 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! ";
}
}
Both classes use the annotation of guice to declare Assembly principles. For more information, see guice documentation.
So far, even if you have finished introducing a simple demo, you are welcome to discuss it.