Application properties

In DefaultMailManager, we use @Value("${app.mail-from}") to assign the value of the app.mail-from property that we add in application.properties. The following figure shows that the app.mail-from property is added to the top of application.properties:

Figure 10.18: Wavy line under unrecognized property

You can see that there is a wavy line under app.mail-from. That is because there is no metadata available for this property, and VS Code is complaining about this and lists it in the Problems tab. Even though the application will still function normally, leaving it like this will create a tendency to ignore the problems that VS Code finds and lists in the Problems tab, which is not a good practice. To fix this, we need to use the spring-boot-configuration-processor library provided by Spring Boot.

We will need to create a configuration class to specify the properties of our app, and after a successful installation of the application with the mvn clean install command, the library will generate metadata in target/META-INF/spring-configuration-metadata.jsonwhich will be used by VS Code to analyze the properties inside of application.properties.

Let's create the com.taskagile.config.ApplicationProperties configuration class as follows:

@Configuration
@ConfigurationProperties(prefix="app")
@Validated
public class ApplicationProperties {
/**
* Default `from` value of emails sent out by the system
*/
@Email
@NotBlank
private String mailFrom;

// getter and setter
}

As you can see, we use the @ConfigurationProperties annotation here to let Spring Boot know that this configuration defines the properties that have the prefix app. With the @Validated, @Email, and @NotBlank annotations, Spring Boot will perform validation for the properties during the starting phase of the application. For example, if the value of the app.mail-from property is not a valid email address, the application will fail to start because of the validation error. This is the actual benefit of defining this ApplicationProperties class. It helps us to validate the value of our custom properties. The JavaDoc we add to the mailFrom field will show up as a tooltip in VS Code, as the following figure:

Figure 10.19: Tooltip in application.properties

As usually, let's commit the code after a successful build with mvn clean install. Here is the commit record:

Figure 10.20: Implementing sending emails using JavaMail commit
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.93.132