In this class, we will have these three configuration classes autowired since they are spring managed beans. To test our properties, let’s create a configuration class that implements CommandLineRunner so that we can log all the properties configured in its run method. DevConfigProps.java package import .properties.ConfigurationProperties import .Configuration ( "dev" ) public class DevConfigProps These prefixes will be used to configure these classes by using the for dev properties, for qa properties and for prod properties. If you consider the application.properties file above, you will notice that each of these properties is either prefix with dev, qa or prod. Let’s create a package called config and create three classes DevConfigProps.java, QaConfigProps.java and ProdConfigProps.java in this package and annotate each class with to tell spring that these classes should be scanned, managed and configured as spring beans during application bootup. Our aim is to load these properties into our application by creating beans and using annotation.
Just bear with me with this approach to show how to use the annotation. In reality, we cannot have different environment properties like this, but rather create a specific application properties file for each environment, set up a profile and choose the active profile or profiles to use.
This is not a good practice to have all these specific environment properties on a single file. Each property is prefixed with the type of environment they belong to. The file contains three properties values for three different environment - dev, qa and prod. Prod.name = Production Application prod.port = 8091 prod.dbtype = MySQL prod.version = 1.0.1 prod.dburl = jdbc:mysql://localhost:3308/ prod.dbname = studentDB prod.dbuser = admin-prod prod.dbpassword = admin-prod Qa.name = QA Test Application qa.port = 8092 qa.dbtype = Mongo DB qa.version = 1.2.beta qa.dburl = mongodb://:27017/ qa.dbname = studentDB qa.dbuser = admin qa.dbpassword = admin #PROD environment properties Open the application.properties file and write the properties values below:ĭev.name = Development Application dev.port = 8091 dev.dbtype = Maria DB dev.version = 1.0.alpha dev.dburl = jdbc:mariadb://localhost:3306/ dev.dbname = studentDB dev.dbuser = root dev.dbpassword = root #QA environment properties
Unzip the file and open it with your prefered IDE as a Maven project to download all the required dependencies and at the end of all these, your project structure should look like the image below:
Select Spring Web as the only dependency for our project.Ĭlick ‘Generate’ button to download the bootstrapped project as a zip file. Let go to to generate and bootstrap our project.Ĭhoose Maven Project, Java as the language, give your project the group name and artefact Id. In this write-up, I will demonstrate how to use annotation to inject properties values from the application.properties or application.yaml files. Also is the using of Environment object to lookup values, properties or profiles in the classpath. One of them is the annotation discussed in the Spring annotation tricks write up.Īnother one is the using annotation on a configuration bean to inject properties values to a bean and use it in the class. Spring provides several ways of injecting/looking up values from the application configuration properties file.