当前位置: 首页 > 知识库问答 >
问题:

获取错误:创建名为“*”的bean时出错:通过字段“repo”表示的不满足的依赖关系;n

宋烨烁
2023-03-14

错误:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“Issue TemplateServiceImpl”的bean时出错:通过字段“repo”表示的不满足的依赖关系;嵌套异常为org.springframework.beans.factory.noSuchBeanDefinitionException:没有“com.conseco.repository.issueTemplateRepo”类型的合格bean可用:至少需要一个合格的自动候选bean。依赖项批注:{@org.springframework.beans.factor.annotation.AutoWired(required=true)}(位于org.springframework.beans.factor.annotation.AutoWiredanNotationBeanPostProcessor.596)(位于org.springframework.beans.factor.annotation.InjectionMetadata.Injection(InjectionMetadata.90)(位于ETBean$0(AbstractBeanFactory.java:320)在org.springframework.beans.factory.support.defaultSingletOnBeanRegistry.getSingleton(DefaultSingletOnBeanRegistry.java:222)在org.springframework.beans.factory.support.abstractBeanFactory.dogetBean(AbstractBeanFactory.java:318)在.ContextLoaderListener.ContextInitialized(ContextLoaderListener.java:103)在org.apache.catalina.core.StandardContext.java:4840)在org.apache.catalina.core.StandardContext.Startinternal(StandardContext.java:5303)在org.apache.catalina.util.LifeCycleBase.Start(LifeCycleBase.147)在org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.1397依赖关系注释:{@org.springframework.beans.factory.annotation.AutoWired(required=true)}(位于org.springframework.beans.factory.support.defaultlistablebeanfactory.raiseNoMatchingbeanfound(位于org.springframework.beans.factory.support.defaultlistablebeanfactory.java:1654)(位于

我的代码:

IssueTemplateController:

package com.conseco.controller;




import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.conseco.dao.IssueTemplateInfo;
import com.conseco.service.IssueTemplateService;


@Controller
public class IssueTemplateController {

    @Autowired
  IssueTemplateService service;



    @RequestMapping("/new")
    public ModelAndView create()
    {

        System.out.println("service: "+service);
        ModelAndView mav=new ModelAndView("index");
        System.out.println("1..................");
        IssueTemplateInfo issue=new IssueTemplateInfo();
        issue.setSDT("123456");
        issue.setAnalysis("test");
        issue.setApplicaiton("AWD");
        issue.setBatch("no");
        issue.setBusinessImpact("N/A");
        issue.setDescription("will describe later");
        issue.setEMER("N/A");
        issue.setImpDate("25th");
        issue.setPermanentFix("TBT");
        issue.setQCDefect("234");
        issue.setRootCause("unknown");
        issue.setSeverity(3);
        issue.setStatus("Inprogress");
        issue.setWorkAround("a lot to work on");
        service.save(issue);

        System.out.println("4..............");


        return mav;
    }

}


MyFrontController:



package com.conseco.controller;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.conseo.config.WebMvcConfig;

public class MyFrontController extends  AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[] {WebMvcConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] {"/"};
    }

}




IssueTemplateRepo:
package com.conseco.repository;

import org.springframework.data.repository.CrudRepository;


import com.conseco.dao.IssueTemplateInfo;


public interface IssueTemplateRepo extends CrudRepository<IssueTemplateInfo, String> {

}



IssueTemplateService:


package com.conseco.service;

import org.springframework.stereotype.Service;

import com.conseco.dao.IssueTemplateInfo;


public interface IssueTemplateService {


    IssueTemplateInfo save(IssueTemplateInfo issue);
}



IssueTemplateServiceImpl:
package com.conseco.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.conseco.dao.IssueTemplateInfo;
import com.conseco.repository.IssueTemplateRepo;

@Service
public class IssueTemplateServiceImpl implements IssueTemplateService {

    @Autowired
    private IssueTemplateRepo repo;

    public IssueTemplateRepo getRepo() {
        return repo;
    }

    public void setRepo(IssueTemplateRepo repo) {
        this.repo = repo;
    }

    @Override
    public IssueTemplateInfo save(IssueTemplateInfo issue) {
        // TODO Auto-generated method stub
        return repo.save(issue);
    }

}





WebMvcConfigurer:
package com.conseo.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



@Configuration
@EnableWebMvc
@ComponentScan({"com.conseco.service","com.conseco.controller","com.conseco.repository","com.conseco.dao"})
public class WebMvcConfig implements WebMvcConfigurer {

}

共有1个答案

韦衡
2023-03-14

在接口上添加注释@component:

@Component
public interface IssueTemplateRepo extends CrudRepository<IssueTemplateInfo, String> {

    }

添加@component后,注释将成为一个spring bean,并且可以由spring IoC容器管理。

 类似资料: