[FIXED] Wrong profile is active

Issue

@SpringBootApplication
public class SfgDiApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(SfgDiApplication.class, args);

        PetController petController = ctx.getBean("petController", PetController.class);
        System.out.println("--- The Best Pet is ---");
        System.out.println(petController.whichPetIsTheBest());
}


@Controller
@ResponseBody
public class PetController {

    public PetController(PetService petService) {
        this.petService = petService;
    }

    private final PetService petService;

    @GetMapping("pet-type")
    public String whichPetIsTheBest(){
        return petService.getPetType();
    }
}



public interface PetService {

    String getPetType();
}




@Service("cat")
public class CatPetService implements PetService {
    @Override
    public String getPetType() {
        return "Cats Are the Best!";
    }
}



@Profile({"dog", "default"})
public class DogPetService implements PetService {
    public String getPetType(){
        return "Dogs are the best!";
    }
}

application.properties

spring.profiles.active=dog 

Result

--- The Best Pet is ---
Cats Are the Best!

I don’t understand why cats are here. I can even comment the properties out, but cats are still here. I would like to see dogs.

What can I try next?

Solution

It looks like the DogService is not a bean. So in the end you only have a single bean (CatService) and this one will be picked all the time.

Answered By – Loading

Answer Checked By – Jay B. (FixeMe Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *