@Controller
@Controller
public class ControllerClass {
@Autowired
ServiceClass serviceClass;
@RequestMapping("/")
public String getAll(Model model) {
System.out.println(serviceClass.getAll());
model.addAttribute("contents", serviceClass.getAll());
return "NewFile";
}
}
存储库
@Repository
public interface RepositiryClass {
List<Domain> get();
}
RepoImpl
public class RepoImpl implements RepositiryClass {
@Override
public List<Domain> get() {
List<Domain> dList = new ArrayList<>();
Domain one = new Domain("testOne", "testTwo");
Domain two = new Domain("testTwo", "TestOne");
Domain three = new Domain("three", "three");
dList.add(one);
dList.add(two);
dList.add(three);
return dList;
}
@服务
@Service
public interface ServiceClass {
List<Domain> getAll();
}
ServiceClassImpl
public class ServiceClassImpl implements ServiceClass {
@Autowired
RepositiryClass repositriyClass;
@Override
public List<Domain> getAll() {
return repositriyClass.get();
}
}
异常
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controllerClass': Unsatisfied dependency expressed through field 'serviceClass': No qualifying bean of type [com.controller.domain.service.ServiceClass] found for dependency [com.controller.domain.service.ServiceClass]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.controller.domain.service.ServiceClass] found for dependency [com.controller.domain.service.ServiceClass]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
根本原因
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'serviceClassImpl': Unsatisfied dependency expressed through field 'repositriyClass': No qualifying bean of type [com.controller.domain.repo.RepositiryClass] found for dependency [com.controller.domain.repo.RepositiryClass]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.controller.domain.repo.RepositiryClass] found for dependency [com.controller.domain.repo.RepositiryClass]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
的web.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
的DispatcherServlet-servlet.xml中
<mvc:annotation-driven />
<context:component-scan base-package="com.*" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
包裹声明
搜索各种表格后,我仍然无法解决这个问题 为什么我得到这个错误,因为我已经实现了所有,但仍然不确定为什么我会收到此错误以及如何解决这个问题。
由于
答案 0 :(得分:0)
将@Service
注释添加到ServiceClassImpl
。不是界面。
@Service
public class ServiceClassImpl implements ServiceClass {
@Autowired
RepositiryClass repositriyClass;
@Override
public List<Domain> getAll() {
return repositriyClass.get();
}
}
接口:
public interface ServiceClass {
List<Domain> getAll();
}
还将@Repository
注释添加到存储库实现:
@Repository
public class RepoImpl implements RepositiryClass {
@Override
public List<Domain> get() {
List<Domain> dList = new ArrayList<>();
Domain one = new Domain("testOne", "testTwo");
Domain two = new Domain("testTwo", "TestOne");
Domain three = new Domain("three", "three");
dList.add(one);
dList.add(two);
dList.add(three);
return dList;
}
}
不是界面:
public interface RepositiryClass {
List<Domain> get();
}
请记住:Annotation始终属于Interfaces的实现。不是界面本身