我是Spring框架的初学者,并且正在努力解决以下问题:
使用Qualifier批注标记后,Spring似乎无法识别该组件。组件扫描无法找到的软件包似乎有问题。我正在右键单击主类并运行该程序。
到目前为止,我已经尝试了很多事情,但是没有任何效果。但最后我得到以下错误:
启动ApplicationContext时出错。要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。 2018-06-27 13:37:10.177错误8476 --- [main] o.s.b.d.LoggingFailureAnalysisReporter:
申请无法开始
说明:
com.tony.practicas.controllers.PersonaController中的setEducationLevelServiceAcc方法的参数0需要一个类型为'com.tony.practicas.services.EducationLevelService'的bean。
操作:
考虑在您的配置中定义类型为“ com.tony.practicas.services.EducationLevelService”的bean。
以退出代码1完成的过程
这是项目的结构:
这是我班级的代码:
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());
}
}
主要
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");
}
}
服务
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;
}
}
复制属性
spring.profiles.active=accountant
答案 0 :(得分:2)
您使用注释group by
,这意味着仅在Spring概要文件@Profile("accountant")
处于活动状态时才会创建此bean。
具有配置文件特定的配置后,您需要在环境中设置活动配置文件。
有多种方法
•在VM参数中使用-Dspring.profiles.active = prod
•在application.properties中使用spring.profiles.active = prod