org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ controllery”的bean时出错

时间:2018-11-19 14:04:43

标签: java spring spring-mvc spring-boot

我想要创建Rest Web Services并且它起作用了。我更改了代码,  现在我有这个错误:

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   在文件中定义名称为“ controllery”的bean创建时出错   [C:\ Users \ amir \ workspace \ gu \ target \ classes \ gu \ webservice \ controller \ Controllery.class]:   通过构造函数参数0表示的不满足的依赖关系;   嵌套异常为   org.springframework.beans.factory.UnsatisfiedDependencyException:   在文件中定义名称为“ interfaceImpl”的bean创建时出错   [C:\ Users \ amir \ workspace \ gu \ target \ classes \ gu \ webservice \ impl \ InterfaceImpl.class]:   通过构造函数参数0表示的不满足的依赖关系;   嵌套异常为   org.springframework.beans.factory.BeanCreationException:错误   创建名称为“ testRepository”的bean:初始化方法的调用   失败嵌套异常是java.lang.IllegalArgumentException:不是   托管类型:类别gu.webservice.domain.Test位于   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:733)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:198)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1266)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1123)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:317)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)   〜[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548)   〜[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)   〜[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]在   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)   [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]位于   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)   [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]位于   org.springframework.boot.SpringApplication.run(SpringApplication.java:307)   [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]位于   org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)   [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]位于   org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)   [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]位于   gu.webservice.GuApplication.main(GuApplication.java:11)​​[classes /:na]   造成原因:   org.springframework.beans.factory.UnsatisfiedDependencyException:   在文件中定义名称为“ interfaceImpl”的bean创建时出错   [C:\ Users \ amir \ workspace \ gu \ target \ classes \ gu \ webservice \ impl \ InterfaceImpl.class]:   通过构造函数参数0表示的不满足的依赖关系;   嵌套异常为   org.springframework.beans.factory.BeanCreationException:错误   创建名称为“ testRepository”的bean:初始化方法的调用   失败嵌套异常是java.lang.IllegalArgumentException:不是   托管类型:类别gu.webservice.domain.Test位于   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:733)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:198)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1266)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1123)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:317)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:819)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:725)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE] ... 19常见   省略帧的原因:   org.springframework.beans.factory.BeanCreationException:错误   创建名称为“ testRepository”的bean:初始化方法的调用   失败嵌套异常是java.lang.IllegalArgumentException:不是   托管类型:类别gu.webservice.domain.Test位于   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1694)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:317)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:819)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:725)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE] ... 33常见   省略的帧原因:java.lang.IllegalArgumentException:不是   托管类型:类别gu.webservice.domain.Test位于   org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:473)   〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]在   org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation。(JpaMetamodelEntityInformation.java:74)   〜[spring-data-jpa-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66)   〜[spring-data-jpa-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:180)   〜[spring-data-jpa-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:118)   〜[spring-data-jpa-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:101)   〜[spring-data-jpa-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:304)   〜[spring-data-commons-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda $ afterPropertiesSet $ 4(RepositoryFactoryBeanSupport.java:290)   〜[spring-data-commons-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.util.Lazy.getNullable(Lazy.java:141)   〜[spring-data-commons-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.util.Lazy.get(Lazy.java:63)   〜[spring-data-commons-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:293)   〜[spring-data-commons-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:102)   〜[spring-data-jpa-2.0.11.RELEASE.jar:2.0.11.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1753)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1690)   〜[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE] ... 44常见   省略了帧

听是我的控制器类:

@Controller
@RequestMapping("/start")
public class Controllery {
    //private static final org.jboss.logging.Logger logger = LoggerFactory.logger(Controllery.class);
    @Autowired
    Services test;
    public Controllery(Services test) {
        //super();
        this.test = test;
    }

    @PostMapping
    public List<Test> customercontroll(){
        return test.findAllTest();
    }
     @PostMapping("/employees")
        public Test createEmployee(@Valid @RequestBody Test employee) {
            return test.SaveTest(employee);
        }

我的存储库界面:

@Service
public interface TestRepository extends  JpaRepository<Test ,Long> {

    Test findByLastname();
}

我的服务等级:

package gu.webservice.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import gu.webservice.domain.Test;
@Service
public interface Services{
    Test findTestById(Long id);
    List<Test>findAllTest();
    Test SaveTest(Test test);
    //Test findTestByLastName();
}
And Service impl:
package gu.webservice.impl;

import java.util.List;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import gu.webservice.domain.Test;
import gu.webservice.repository.TestRepository;
@Configuration
@EnableWebMvc //THIS
@ComponentScan(basePackages = "guru.services")

public  class InterfaceImpl implements Services{
    TestRepository test;
    Test entity;
    public InterfaceImpl(TestRepository test) {
    //  super();
        this.test = test;
    }

    /*@Override
    public Test findTestById(Long id) {
        // TODO Auto-generated method stub
        return test.getOne(id);
    }
*/
    @Override
    public List<Test> findAllTest() {
        // TODO Auto-generated method stub
        return (List<Test>) test.findAll();
    }

    public Test SaveTest(Test test) {
        // TODO Auto-generated method stub
        return this.test.save(test);
    }
/*
    @Override
    public Test findTestByLastName() {
        // TODO Auto-generated method stub
        return test.findByLastname();
    }
*/
    @Override
    public Test findTestById(Long id) {
        // TODO Auto-generated method stub
        return null;
    }
}

引导程序类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import gu.webservice.domain.Test;
import gu.webservice.repository.TestRepository;
public class TestBootstrap implements CommandLineRunner {
    @Autowired
    TestRepository test;
    public TestBootstrap(TestRepository test){
        this.test=test;
    }
    @Override
    public void run(String... args) throws Exception {
        // TODO Auto-generated method stub
        Test t=new Test();
        t.setId(1);
        t.setName("ali");
        t.setLastname("rezaii");
        test.save(t);

    }

}

有人可以帮助我解决问题吗?

1 个答案:

答案 0 :(得分:0)

已编辑:在存储库中,将@Service更改为@Repository

在您的InterfaceImpl.java中,您需要向服务中注入@Autowired

必须是这样的:

@Configuration
@EnableWebMvc //THIS
@ComponentScan(basePackages = "guru.services")

public  class InterfaceImpl implements Services{

@Autowired
TestRepository test;
Test entity;

// ADD GETTERS AND SETTERS OF test

public InterfaceImpl() {
super();
}
// ALL METHODS HERE
}