eufamia
2021-04-07 16:10
采纳率: 100%
浏览 153
已采纳

通过@Service注解无法创建Service的实例对象,有伙伴们遇到过这个问题吗

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;
    @Override
    public void insert(Admin admin) {
        adminDao.insert(admin);

    }
@Service/*service的实现类使用了注解*/
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;
    @Override
    public void insert(Admin admin) {
        adminDao.insert(admin);

    }


@Controller
public class AdminController {
/*这里会报错,提示adminService为空*/
    @Autowired
    private AdminService adminService;
    @RequestMapping("/adminAdd.do")
    public  void adminAdd(){
    }
}

在spring的配置文件中有配置扫描的包,但是还是为空

<context:component-scan base-package="com.zjw.survey.service"/>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • 张小帅和刘美美 2021-04-07 16:39
    已采纳
    @Resource(type = AdminServiceImpl.class)
    private AdminService adminService;
    
    这样注入
    
    // @Autowired 这样注入的时候 AdminService 找不到实现类
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 应该是数据库配置问题吧,把错误信息贴出来看看。

    评论
    解决 无用
    打赏 举报
  • 张小帅和刘美美 2021-04-07 16:13

    启动报错还是idea提示错误?
     

    评论
    解决 无用
    打赏 举报
  • eufamia 2021-04-07 16:34

    错误提示 idea也会有提示

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminController': Unsatisfied dependency expressed through field 'adminService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zjw.survey.service.AdminService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
            at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1415)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:608)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531)
            at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
            at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588)
            at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702)
            at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668)
            at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716)
            at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591)
            at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530)
            at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170)
            at javax.servlet.GenericServlet.init(GenericServlet.java:158)
            at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1134)
            at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1089)
            at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:761)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
            at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
            at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
            at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
            at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
            at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
            at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
            at java.lang.Thread.run(Thread.java:748)
        Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zjw.survey.service.AdminService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1777)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1333)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1287)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
            ... 39 more

    评论
    解决 无用
    打赏 举报
  • storm_huang 2021-04-07 17:22
    AdminServiceImpl和AdminService是同一个目录下面的?
    看日志,这样的报错基本都是扫描包设置不对,造成无法扫描到对应的Service
    评论
    解决 无用
    打赏 举报
  • 有问必答小助手 2021-04-07 17:35

    您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,欢迎您加入CSDN!

    目前问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    评论
    解决 无用
    打赏 举报
  • 码眼 2021-04-07 21:55

    保证controller service都在扫描的包下

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题