All Products
Search
Document Center

Using Annotation

Last Updated: Aug 21, 2020

You can also publish and reference services by using the Annotation mode in SOFABoot.Similar to the XML mode, we provide @SofaService and @SofaReference. When multiple protocols are used, you can use @SofaServiceBinding and @SofaReferenceBinding annotations.

Publish a service

To publish an RPC service, add the @SofaService annotation to a Bean object to specify the interface and protocol types.

  1. @SofaService(interfaceType = AnnotationService.class, bindings = { @SofaServiceBinding(bindingType = "bolt") })
  2. @Component
  3. public class AnnotationServiceImpl implements AnnotationService {
  4. @Override
  5. public String sayAnnotation(String stirng) {
  6. return stirng;
  7. }
  8. }

Reference a service

If the Bean object needs to reference a remote service, you just need to add the Reference annotation to the attribute or method. Bolt, Dubbo, and REST protocols are supported.

  1. @Component
  2. public class AnnotationClientImpl {
  3. @SofaReference(interfaceType = AnnotationService.class, binding = @SofaReferenceBinding(bindingType = "bolt"))
  4. private AnnotationService annotationService;
  5. public String sayClientAnnotation(String str) {
  6. String result = annotationService.sayAnnotation(str);
  7. return result;
  8. }
  9. }