Java SpringBoot中的依赖注入
我有一个控制器Java SpringBoot中的依赖注入,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个控制器 @Controller public class AppController{ @Autowired private IDemoApplicationService service; //more code } 服务 @Service("service") public class DemoApplicationServiceImpl implements IDemoApplicationService{ @Override public void
@Controller
public class AppController{
@Autowired
private IDemoApplicationService service;
//more code
}
服务
@Service("service")
public class DemoApplicationServiceImpl implements IDemoApplicationService{
@Override
public void createEmployee(Employee employee){
try {
dao.insertEmployee(employee);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public String updateEmployee(Employee employee, int id) {
dao.updateEmployee(employee, id);
return "redirect:/employees";
}
@Override
public String deleteEmployee(int id) {
dao.deleteEmployee(id);
return "redirect:/employees";
}
@Override
public String getEmployees(Model model) {
model.addAttribute("employees", dao.getEmployees());
return "employees";
}
@Override
public Employee findById(int id) {
return dao.findById(id);
}
}
服务接口
public interface IDemoApplicationService {
}
我想在我的控制器中使用@Autowired来使用服务,但是当我运行应用程序时,我得到了以下错误
org.springframework.beans.factory.BeanCreationException:错误
创建名为“demoApplication”的bean:自动连线的注入
依赖关系失败;嵌套异常是
org.springframework.beans.factory.BeanCreationException:无法
autowire字段:专用服务。IDemoApplicationService
controllers.DemoApplication.service;嵌套异常是
org.springframework.beans.factory.noSuchBean定义异常:否
为找到类型为[services.IDemoApplicationService]的限定bean
依赖项:至少需要1个符合autowire条件的bean
此依赖项的候选项。依赖项批注:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
有人能告诉我该怎么做才能让它工作吗
感谢如果您查看
@SpringBootApplication
内部,它包含许多其他Spring的注释
其中一些:
1)@Configuration
2)@ComponentScan
在@SpringBootApplication中,您可以路径一个参数:
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
public String[] scanBasePackages() default {};
正如您所看到的,@ComponentScan
的别名是知道哪些包要扫描带有@Component或子注释的类,如@Service@Repository
,如果您不提供此参数,Spring将扫描带有SpringBootApplication注释的类所在的主包
您的服务很可能位于不同的包中您的
演示应用程序
是您的应用程序启动类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
然后,您需要有一个单独的控制器文件,如下所示:
@Controller
public class AppController {
@Autowired
private IDemoApplicationService service;
//more code
}
@Service("service")
public class DemoApplicationServiceImpl implements IDemoApplicationService{
}
为什么需要进行上述更改:
@SpringBootApplication
相当于使用三个单独的注释,即@EnableAutoConfiguration
、@ComponentScan
和@Configuration
。您可以在上阅读更多关于此的信息
在
@springbootplication
之前提供@Controller
时,@Configuration
部分没有机会在上下文中注册bean,因此您得到了所述错误。为什么您的控制器要用@springbootplication
注释?它是一个springboot应用程序,还没有回答我的问题。为什么您的控制器要用它来注释?这是我第一个遵循本教程的spring boot应用程序,在控制器中,这里用@SpringBootApplicationCheck注释了完整的示例。您需要定义main方法来启动springboot应用程序,然后配置控制器和服务类。创建名为的bean时出错appcontroller@Wearybands您得到的确切错误是什么?由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名为“appController”的bean时出错:注入自动连线依赖失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动连接字段:services.DemoApplicationServiceImpl controllers.AppController.service;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到符合依赖项要求的[services.DemoApplicationServiceImpl]类型的bean:至少需要1个符合此依赖项autowire候选项要求的bean。@Wearybands对我的代码进行了更改。您需要使用服务接口自动连线,并在@service
注释中提供引用名称。原因:org.springframework.beans.factory.BeanCreationException:创建名为“appController”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动连接字段:private services.IDemoApplicationService controllers.AppController.service;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[services.IDemoApplicationService]的符合条件的bean:应至少有1个bean符合此依赖项的autowire候选项的条件。