Create Project: springboot_autowired_profile (add Spring Boot Starters from the table)
Edit File: application.properties (spring.profiles.active = Profile1)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create Package: services (inside main package)
– Create Interface: MyServiceInterface.java (inside package controllers)
– Create Class: MyServiceImplementation1.java (inside package controllers)
– Create Class: MyServiceImplementation2.java (inside package controllers)
application.properties
spring.profiles.active = Profile1
MyServiceInterface.java
package com.ivoronline.springboot_autowired_profile.services;
public interface MyServiceInterface {
public String sayHello();
}
MyServiceImplementation1.java
package com.ivoronline.springboot_autowired_profile.services;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
@Service
@Profile("Profile1")
public class MyServiceImplementation1 implements MyServiceInterface {
public String sayHello() {
return "Hello";
}
}
MyServiceImplementation2.java
package com.ivoronline.springboot_autowired_profile.services;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
@Service
@Profile({"Profile2","Profile3"})
public class MyServiceImplementation2 implements MyServiceInterface {
public String sayHello() {
return "Hello World";
}
}