tree: 4c8b1c718bdaf8d606113f8b5a65ce56fb0f47e2 [path history] [tgz]
  1. src/
  2. pom.xml
  3. README.md
dubbo-samples-annotation/README.md

This sample shows how to use Dubbo by annotation-driven configuration.

Provider Configuration

First, there have to be an overall configuration of provider:

@Configuration
@EnableDubbo(scanBasePackages = org.apache.dubbo.samples.annotation.implation.impl")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static class ProviderConfiguration {
}

@EnableDubbo will enable Spring to scan org.apache.dubbo.samples.annotation.impl package to find anything annotated by Dubbo annotation.

As a provider, the interface implementation class have to be annotated by @Service:

@Service
public class AnnotatedGreetingService implements GreetingService {

    public String sayHello(String name) {
        System.out.println("greeting service received: " + name);
        return "hello, " + name;
    }

}

Consumer Configuration

The overall configuration for consumer is very smilier to provider's:

@Configuration
@EnableDubbo(scanBasePackages = org.apache.dubbo.samples.annotation.actionion.action")
@PropertySource("classpath:/spring/dubbo-consumer.properties")
@ComponentScaorg.apache.dubbo.samples.annotation.actionles.annotation.action"})
static class ConsumerConfiguration {

}

And you can use @Reference annotation to autowire the provider into consumer:

@Component("annotatedConsumer")
public class GreetingServiceConsumer {

    @Reference
    private GreetingService greetingService;
    
    ...
}