Tony Morantes
Tony Morantes

Reputation: 43

Spring framework does not recognize component after being marked with qualifier annotation

I'm a Spring framework beginner and I'm struggling with the following:

It looks like Spring is not recognizing the Component once it is marked with Qualifier annotation. It seems to be a problem with the packages which component scan cannot find. I'm running the program using right click run on the main class.

I've tried many thing so far but nothing is working. But in the end I get the following error:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-06-27 13:37:10.177 ERROR 8476 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Parameter 0 of method setEducationLevelServiceAcc in com.tony.practicas.controllers.PersonaController required a bean of type 'com.tony.practicas.services.EducationLevelService' that could not be found.

Action:

Consider defining a bean of type 'com.tony.practicas.services.EducationLevelService' in your configuration.

Process finished with exit code 1


Here it is the structure of the project:

Structure

Here are the code of my classes:

PersonaController

package com.tony.practicas.controllers;

import com.tony.practicas.services.EducationLevelService;
import com.tony.practicas.services.PersonaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

@Controller
public class PersonaController {

private PersonaService personaService;
private EducationLevelService educationLevelService;
private EducationLevelService educationLevelServiceAcc;

@Autowired
public void setPersonaService(PersonaService personaService) {
    this.personaService = personaService;
}

@Autowired
public void setEducationLevelService(EducationLevelService educationLevelService) {
    this.educationLevelService = educationLevelService;
}

@Autowired
@Qualifier("educationLevelAcc")
public void setEducationLevelServiceAcc(EducationLevelService educationLevelServiceAcc) {
    this.educationLevelServiceAcc = educationLevelServiceAcc;
}


public void setPersonaName (String name){
    personaService.setName(name);
    System.out.println(personaService.getName() + educationLevelService.getEducationLevel());
    System.out.println(educationLevelServiceAcc.getEducationLevel());
}
}

Main

package main;

import com.tony.practicas.controllers.PersonaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.tony.practicas")
public class PracticasApplication {

public static void main(String[] args) {

    ApplicationContext context = SpringApplication.run(PracticasApplication.class, args);

    PersonaController tony = (PersonaController) context.getBean("personaController");
    tony.setPersonaName("Tony");
}
}

Services

package com.tony.practicas.services;

public interface EducationLevelService {
    String getEducationLevel();
}

package com.tony.practicas.services;

import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component("educationLevelAcc")
@Profile("accountant")
@Primary
public class EducationLevelServiceAccountantImpl implements                 
EducationLevelService {

@Override
public String getEducationLevel() {
    return " Accountant";
    }
}

package com.tony.practicas.services;

import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component("educationLevelIng")
@Profile("engineer")
@Primary
public class EducationLevelServiceEngineerImpl implements         
EducationLevelService {

@Override
public String getEducationLevel() {
        return " Engineer";
    }
}

package com.tony.practicas.services;

public interface PersonaService {

    public abstract void setName(String name);
    public abstract String getName();

}

package com.tony.practicas.services;

import org.springframework.stereotype.Component;

@Component
public class PersonaServiceImpl implements PersonaService{

    private String name;

    @Override
    public void setName(String name) {
        this.name = name + " Miguel Morantes Polanco";
    }

    @Override
    public String getName() {
        return name;
    }
}

Aplication Properties

spring.profiles.active=accountant

Upvotes: 2

Views: 756

Answers (1)

Ivan
Ivan

Reputation: 8758

You use annotation @Profile("accountant") which means that this bean will be created only if Spring profile accountant is active.

Once you have profile specific configuration, you would need to set the active profile in an environment.

There are multiple ways of doing this

•Using -Dspring.profiles.active=prod in VM Arguments

•Use spring.profiles.active=prod in application.properties

http://www.springboottutorial.com/spring-boot-profiles

Upvotes: 2

Related Questions