在Web容器外的Java 6中使用Guice 3和JAX-WS的方法如下:
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.10</version>
</dependency>
</dependencies>
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
public class MyGuiceModule extends AbstractModule {
@Override
protected void configure() {
bind(MyService.class).annotatedWith(Names.named("MyServiceImpl")).to(MyServiceImpl.class);
}
}
import javax.jws.WebService;
@WebService
public interface MyService {
String sayHello(String name);
}
public class MyServiceImpl implements MyService {
public String sayHello(String name) {
return "Hello, " + name;
}
}
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import com.google.inject.Guice;
import com.google.inject.Injector;
@WebService(endpointInterface = "com.example.MyService")
public class MyServiceEndpoint implements MyService {
private MyService myService;
public MyServiceEndpoint() {
Injector injector = Guice.createInjector(new MyGuiceModule());
myService = injector.getInstance(MyService.class);
}
@Override
public String sayHello(String name) {
return myService.sayHello(name);
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/MyService", new MyServiceEndpoint());
}
}
import javax.xml.ws.Service;
public class MyServiceClient {
public static void main(String[] args) throws Exception {
Service service = Service.create(
new URL("http://localhost:8080/MyService?wsdl"),
new QName("http://example.com/", "MyServiceEndpointService")
);
MyService myService = service.getPort(MyService.class);
String response = myService.sayHello("World");
System.out.println(response);
}
}
以上就是在Web容器外的Java 6中使用Guice 3和JAX-WS的方法。
领取专属 10元无门槛券
手把手带您无忧上云