> For the complete documentation index, see [llms.txt](https://hanfak.gitbook.io/workspace/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hanfak.gitbook.io/workspace/languages/java/framework/spring/patterns/decorator.md).

# Decorator Pattern in Spring

* <https://blog.tratif.com/2018/04/12/decorating-spring-components/>

```java
public class BasicProductService implements ProductService {

  private final ProductRepository productRepo;

  public BasicProductService(ProductRepository productRepo) {
    this.productRepo = productRepo;
  }

  @Override
  public Product createProduct(String productName) {
    // simplified implementation for brevity
    Product product = new Product(productName);
    productRepo.save(product);
    return product;
  }
}
```

```java
public class FullTextSearchIndexedDecorator implements ProductService {

  private final ProductService wrappedProductService;
  private final FullTextSearchIndexService fullTextSearchIndexService;

  public FullTextSearchIndexedDecorator(
      ProductService wrappedProductService,
      FullTextSearchIndexService fullTextSearchIndexService) {
    this.wrappedProductService = wrappedProductService;
    this.fullTextSearchIndexService = fullTextSearchIndexService;
  }

  @Override
  public Product createProduct(String productName) {
    Product product = wrappedProductService.createProduct(productName);
    fullTextSearchIndexService.index(product);
    return product;
  }
}
```

Multiple implementations of an interface in a single Spring context

```java
@Configuration
public class AppConfiguration {

  @Bean(name = "basicService")
  ProductService basicService(ProductRepository productRepository) {
    return new BasicProductService(productRepository);
  }

  @Bean(name = "decoratedService")
  ProductService decoratedService(
      @Qualifier("basicService") ProductService basicProductService,
      ElkFullTextSearchIndexService indexService) {
    return new FullTextIndexedDecorator(basicProductService, indexService);
  }

  @Bean
  ServiceRequiringDecoratedService serviceRequiringDecoratedService(
      @Qualifier("decoratedService") ProductService productService) {
    return new ServiceRequiringDecoratedService(productService);
  }

  @Bean
  ServiceRequiringBasicService serviceRequiringBasicService(
      @Qualifier("basicService") ProductService productService) {
    return new ServiceRequiringBasicService(productService);
  }
}
```

A single implementation of an interface in Spring context

* only the basic implementation is created as a bean, other components can use base implementation and decorate it if needed

```java
@Configuration
public class AppConfiguration {

  @Bean
  ProductService basicService(ProductRepository productRepository) {
    return new BasicProductService(productRepository);
  }

  @Bean
  ServiceRequiringDecoratedService serviceRequiringDecoratedService(
      ProductService productService,
      ElkFullTextSearchIndexService indexService) {
    ProductService decoratedProductService = new FullTextIndexedDecorator(productService, indexService);
    return new ServiceRequiringDecoratedService(decoratedProductService);
  }

  @Bean
  ServiceRequiringBasicService serviceRequiringBasicService(ProductService productService) {
    return new ServiceRequiringBasicService(productService);
  }
}
```

* only the decorated implementation is created as a bean, all other components which are using auto-wiring will get the decorated implementation

```java
@Configuration
public class AppConfiguration {

  @Bean
  ProductService decoratedService(
      ProductRepository productRepository,
      ElkFullTextSearchIndexService indexService) {
    ProductService productService = basicService(productRepository);
    return new FullTextIndexedDecorator(productService, indexService);
  }

  @Bean
  ServiceRequiringDecoratedService serviceRequiringDecoratedService(ProductService productService) {
    return new ServiceRequiringDecoratedService(productService);
  }
}
```

* <https://github.com/amit169/decoratorPatternSpringBoot>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hanfak.gitbook.io/workspace/languages/java/framework/spring/patterns/decorator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
