Tuesday, 17 July 2018

Spring boot: Importing Additional Configuration Classes

In this post, you are going to learn different ways to import additional configuration classes.
a.   By defining the main application class in top level package
b.   By using @Import annotation

By defining the main application class in top level package
If you define the spring boot main application class in top level package, it automatically scans all the sub packages and automatically pick up all the spring components including @Configuration classes.

Address.java
package com.sample.myApp.model;

public class Address {
 private String city;
 private String country;
 private String phoneNum;
 private String email;

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 public String getPhoneNum() {
  return phoneNum;
 }

 public void setPhoneNum(String phoneNum) {
  this.phoneNum = phoneNum;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 @Override
 public String toString() {
  return "Address [city=" + city + ", country=" + country + ", phoneNum=" + phoneNum + ", email=" + email + "]";
 }

}

Employee.java
package com.sample.myApp.model;

import org.springframework.stereotype.Component;

@Component
public class Employee {

 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
 }

}

AddressConfiguration.java
package com.sample.myApp.Configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sample.myApp.model.Address;

@Configuration
public class AddressConfiguration {

 @Bean
 public Address getDefaultAddress() {
  Address address = new Address();

  address.setCity("NOT_INITIALIZED");
  address.setCountry("NOT_INITIALIZED");
  address.setEmail("NOT_INITIALIZED");
  address.setPhoneNum("NOT_INITIALIZED");

  return address;

 }

}

EmployeeConfiguration.java
package com.sample.myApp.Configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sample.myApp.model.Employee;

@Configuration
public class EmployeeConfiguration {
 
 @Bean
 public Employee getDefaultEmployee() {
  Employee emp = new Employee();
  emp.setFirstName("NO_NAME");
  emp.setId(-1);
  emp.setLastName("NO_NAME");
  return emp;
 }
}

Application.java
package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.model.Address;
import com.sample.myApp.model.Employee;

@SpringBootApplication
public class Application {

 public static void main(String args[]) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  Employee employee = (Employee) applicationContext.getBean("getDefaultEmployee");
  Address address = (Address) applicationContext.getBean("getDefaultAddress");

  System.out.println(employee);
  System.out.println(address);

  applicationContext.close();
 }
}


Output
Employee [id=-1, firstName=NO_NAME, lastName=NO_NAME]
Address [city=NOT_INITIALIZED, country=NOT_INITIALIZED, phoneNum=NOT_INITIALIZED, email=NOT_INITIALIZED]

Project structure looks like below.



By using @Import annotation
By using @Import annotation, you can aggregate all the configuration classes. If the spring boot application class is not at the root level, you can specify all the configuration classes using @Import annotation.

@SpringBootApplication
@Import({ AddressConfiguration.class, EmployeeConfiguration.class })
public class Application {

}

Application.java
package com.sample.myApp.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;

import com.sample.myApp.Configuration.AddressConfiguration;
import com.sample.myApp.Configuration.EmployeeConfiguration;
import com.sample.myApp.model.Address;
import com.sample.myApp.model.Employee;

@SpringBootApplication
@Import({ AddressConfiguration.class, EmployeeConfiguration.class })
public class Application {

 public static void main(String args[]) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  Employee employee = (Employee) applicationContext.getBean("getDefaultEmployee");
  Address address = (Address) applicationContext.getBean("getDefaultAddress");

  System.out.println(employee);
  System.out.println(address);

  applicationContext.close();
 }
}


Output
Employee [id=-1, firstName=NO_NAME, lastName=NO_NAME]
Address [city=NOT_INITIALIZED, country=NOT_INITIALIZED, phoneNum=NOT_INITIALIZED, email=NOT_INITIALIZED]

Updated project structure looks like below.




Previous                                                 Next                                                 Home

No comments:

Post a Comment