The idea is that you need to teach Tomcat how to instantiate your
Servlets.

You need: guice-2.0.jar, guice-servlet-2.0.jar, aopalliance.jar.

In war/WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd";>

<web-app>

        <filter>
                <filter-name>guiceFilter</filter-name>
                
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
        </filter>

        <filter-mapping>
                <filter-name>guiceFilter</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>

        <listener>
                
<listener-class>your.pakage.server.GuiceContextListener</listener-
class>
        </listener>

        <welcome-file-list>
                <welcome-file>TikPak.html</welcome-file>
        </welcome-file-list>

</web-app>

Then in the server package.

File: GuiceContextListener.java

package your.pakage.server;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;

public class GuiceContextListener extends GuiceServletContextListener
{

  @Override
protected Injector getInjector() {
    return Guice.createInjector(new UniqueModule());
  }
}

File: GuiceRemoteServiceServlet.java

package your.pakage.server;

import
com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;

@Singleton
public class GuiceRemoteServiceServlet extends RemoteServiceServlet {
        private static final long serialVersionUID = 1L;
        private Injector injector;

        @Inject
        public GuiceRemoteServiceServlet(Injector injector) {
                this.injector = injector;
        }

        @Override
        public String processCall(String payload) throws
SerializationException {
                try {
                        RPCRequest rpcRequest = RPC.decodeRequest(payload, 
null, this);
                        RemoteService service =
getServiceInstance(rpcRequest.getMethod().getDeclaringClass());
                        return RPC.invokeAndEncodeResponse(service, 
rpcRequest.getMethod(),
rpcRequest.getParameters(), rpcRequest.getSerializationPolicy(),
rpcRequest.getFlags());
                }
                catch (IncompatibleRemoteServiceException ex) {
                        log("An IncompatibleRemoteServiceException was thrown 
while
processing this call.", ex);
                        return RPC.encodeResponseForFailure(null, ex);
                }
        }

        private RemoteService getServiceInstance(Class<?> serviceClass) {
                return (RemoteService) injector.getInstance(serviceClass);
        }
}

File: UniqueModule.java

package your.pakage.server;

import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import your.pakage.server.transactions.DaoModule;
import your.pakage.shared.rpc.SetupRpc;

import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.google.inject.servlet.ServletModule;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class UniqueModule extends ServletModule {

        @Override
        protected void configureServlets() {
                serve("/tikpak/GWT.rpc").with(GuiceRemoteServiceServlet.class);

                bind(SetupRpc.class).to(Setup.class);
                bind(SourceCodeViewRpc.class).to(SourceCode.class);
        }
}

I hope I'm not forgetting anything important. With this setup you no
longer need to extend from RemoteServiceServlet.
My services looks like:

@RemoteServiceRelativePath("GWT.rpc")
public interface SetupRpc extends RemoteService {
        public SetupData initClient();
}

public class Setup implements SetupRpc {
        private QuerySystemDao dao;

        @Inject
        public Setup(QuerySystemDao dao) {
                this.dao = dao;
        }
...


On Jun 9, 7:54 am, gangurg gangurg <gang...@gmail.com> wrote:
> Sry it was an incomplete post . Here is my full post .
>
> I am new to Injection . What I wanted to do was a simple Interface Injection
> in my GWT IMpl . Can anyone tell me how do a Field Injection or a
> construction based Injection .Assume Everything in this snippet works fine
> .
>
> public Interface Payment(
> public void pay();
>
> }
>
> public class PaymentImpl implements Payment {
>
> public void pay() {
> System.out.println("I'll pay with a credit card");
>
> }
> }
>
> //My Guice Module
> public class TestGuiceModule extends AbstractModule {
>   @Override
>   protected void configure() {
>     bind(Payment.class).to(PaymentImpl.class);
>
>   }
>
> }
>
> //My Actual GWT Servlet
>
>  public class MyServiceImpl extends RemoteServiceServlet implements
> MyService {
>
>  private Payment payment;
> �...@inject
> public MyServiceImpl (Payment payment)
> {
> Injector injector = Guice.createInjector(new TestGuiceModule());
> payment= injector.getInstance(Payment.class);
>                this.payment = payment
>
>              // This works in terms of i get the instance of the object ,
> but I am sure this is not the right apprach . Can anyone suggest whats the
> correct method of doing
>
>
>
> }

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to google-web-tool...@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to