How it works...

In this recipe, we did a number of things; so let's break them down one change at a time.

The first change, ignoring the need to create the keystore, was the creation of the tomcat.https.properties and TomcatSslConnectorProperties objects to bind them to. Previously, we already dealt with making changes to the various settings in application.properties when configuring our datasource. At that time, though, we did not have to create any binding objects because Spring Boot already had them defined.

As we learned earlier, Spring Boot already exposes many properties to configure the application settings, including a whole set of settings for the server section. These values get bound to an internal Spring Boot class: ServerProperties

A complete list of the common application properties can be found in the Spring Boot reference documentation at http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html.

What we did with our addition was simply mimic Spring Boot and create our own configuration group with a binding object behind it. The reason that we didn't use the already existing server.tomcat. prefix, and instead opt for custom.tomcat, was mostly governed by the need to separate our config values from the default ones. Since we are adding a second connector, we want to have a clean separation between the default configuration properties and our custom ones.

The @ConfigurationProperties(prefix = "custom.tomcat.https") method is an important annotation for our TomcatSslConnectorProperties object. It tells Spring Boot to automatically bind the properties with the custom.tomcat.https prefix to fields that are declared in TomcatSslConnectorProperties. In order for the binding to take place—in addition to defining the fields in the class—it is very important to define the getters and setters as well. It is also worth mentioning that during the binding process, Spring will automatically try to convert the property values to their appropriate data types. For example, the value of custom.tomcat.https.keystore gets automatically bound to a private file keystore field object.

The converters, which we learned about earlier, will also be used during the process of converting to custom-defined data types.

The next step is to tell Spring Boot to include the properties that are defined in tomcat.https.properties in the list of properties. This is achieved by adding @PropertySource("classpath:/tomcat.https.properties") next to @Configuration in the WebConfiguration class.

After the values are imported, we will need to tell Spring Boot to automatically create an instance of TomcatSslConnectorProperties for us to use. This is done by adding the following annotation next to @Configuration:

@EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)

This will instruct Spring Boot to automatically create a bean of type TomcatSslConnectorProperties and bind it with the values from the specified classpath:/tomcat.https.properties file. This bean can later be used for autowiring into different places, such as when we create a ServletWebServerFactory bean.

After all the property support is set and done, we will proceed with the actual code to create a second connector. The creation of the ServletWebServerFactory bean provides Spring Boot with a factory to use in order to create  WebServer. The convenient configureConnector(Connector connector) method, which we added to TomcatSslConnectorProperties, gives us a good place to encapsulate and consolidate all the settings that are needed to configure the newly created Connector instance.

..................Content has been hidden....................

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