Skip to content

Commit 9fc5d8f

Browse files
update README.md
1 parent e4c9d21 commit 9fc5d8f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Vault, and more.
3636
- **Provider Order Configuration**: Specify the order in which providers are executed for secret retrieval using
3737
configuration properties
3838
- **Type Conversion for Secrets**: Retrieve secrets by key and origin, converting the value to the specified type for seamless integration with your application.
39+
- **@SecretValue Annotation**: Annotate fields to automatically inject secrets from the configured providers
3940

4041
## Installation
4142

@@ -142,6 +143,50 @@ public class SecretsProviderCustom extends AbstractSecretsProvider {
142143
143144
```
144145

146+
### Using `@SecretValue` Annotation
147+
148+
The `@SecretValue` annotation allows you to inject secret values directly into your Spring beans. It simplifies the process of retrieving secrets by automatically resolving and injecting them from the configured secret providers.
149+
150+
#### Example 1: Injecting a String Secret
151+
```java
152+
import io.github.open_source_lfernandes.spring_secret_starter.annotations.SecretValue;
153+
import org.springframework.stereotype.Component;
154+
155+
@Component
156+
public class MyComponent {
157+
158+
@SecretValue("${example.secret-key}")
159+
private String secretValue;
160+
161+
public void printSecret() {
162+
System.out.println("Secret Value: " + secretValue);
163+
}
164+
}
165+
```
166+
167+
#### Example 1: Injecting a Custom Object Secret
168+
```java
169+
import io.github.open_source_lfernandes.spring_secret_starter.annotations.SecretValue;
170+
import org.springframework.stereotype.Component;
171+
172+
@Component
173+
public class MyComponent {
174+
175+
@SecretValue(value = "${example.credential}", type = Credential.class)
176+
private Credential credential;
177+
178+
public void printCredential() {
179+
System.out.println("Username: " + credential.getUsername());
180+
System.out.println("Password: " + credential.getPassword());
181+
}
182+
}
183+
```
184+
185+
#### How It Works
186+
- The @SecretValue annotation retrieves the secret value from the configured providers based on the key specified in the value attribute.
187+
- The type attribute allows you to specify the class type for type conversion (default is String).
188+
- The secret is automatically injected into the annotated field during the Spring context initialization.
189+
145190
## Configuration Properties
146191

147192
The following table explains the configuration properties available for the Spring Secret Starter:

0 commit comments

Comments
 (0)