繁体   English   中英

Spring @Autowired不适用于基于注释的配置

[英]Spring @Autowired not working with Annotation based configuration

我试图基于注释创建一个Spring MVC项目。 它可以正确编译,但在部署时会引发错误

11:00:24,788 WARN  [org.jboss.as.ee] (MSC service thread 1-5) JBAS011006: Not installing optional component org.springframework.http.server.ServletServerHttpAsyncRequestControl due to an exception (enable DEBUG log level to see the cause)
11:00:24,788 WARN  [org.jboss.as.ee] (MSC service thread 1-5) JBAS011006: Not installing optional component org.springframework.web.context.request.async.StandardServletAsyncWebRequest due to an exception (enable DEBUG log level to see the cause)
11:00:24,788 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
11:00:24,803 WARN  [org.jboss.weld.deployer] (MSC service thread 1-5) JBAS016012: Deployment deployment "SpringRepository.war" contains CDI annotations but no bean archive was found (no beans.xml or class with bean defining annotations).
11:00:24,803 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) JBAS010417: Started Driver service with driver-name = SpringRepository.war_com.mysql.jdbc.Driver_5_1
11:00:25,194 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./SpringRepository: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./SpringRepository: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_25]
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_25]
Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restControllerImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.spring.service.EmployeeService com.spring.controller.RestControllerImpl.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:222)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    ... 3 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restControllerImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.spring.service.EmployeeService com.spring.controller.RestControllerImpl.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.EmployeeService] found for dependency: 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.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:193)
    ... 7 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.spring.service.EmployeeService com.spring.controller.RestControllerImpl.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.EmployeeService] found for dependency: 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:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.EmployeeService] found for dependency: 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 24 more

11:00:25,210 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "SpringRepository.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.undertow.deployment.default-server.default-host./SpringRepository" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./SpringRepository: Failed to start service
    Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restControllerImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.spring.service.EmployeeService com.spring.controller.RestControllerImpl.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restControllerImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.spring.service.EmployeeService com.spring.controller.RestControllerImpl.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.spring.service.EmployeeService com.spring.controller.RestControllerImpl.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}"}}
11:00:25,241 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "SpringRepository.war" (runtime-name : "SpringRepository.war")
11:00:25,241 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 1) JBAS014774: Service status report
JBAS014777:   Services which failed to start:      service jboss.undertow.deployment.default-server.default-host./SpringRepository: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./SpringRepository: Failed to start service

这是我的AppConfig类,其中定义了2个Bean

@Configuration
@ComponentScan("com.spring")
public class Appconfig {

    @Bean
    public EmployeeServiceImpl EmployeeServiceImpl() {
        System.out.println("Service");
        return new EmployeeServiceImpl();

    }

    @Bean
    public RestControllerImpl restControllerImpl() {
        System.out.println("RestController");
        return new RestControllerImpl();
    }



}

这是我的RestController,其中有AutoWired EmployeeService

@Controller
@ComponentScan("com.spring.service")
@RequestMapping("/employee")
public class RestControllerImpl {

    @Autowired
    EmployeeService employeeService;


    @RequestMapping(value = "/getEmployee", method = RequestMethod.GET)
    @ResponseBody
    public List<Employee> getEmployee(){

        return employeeService.findAll();

    };

这是我的EmployeeService

@Service
public interface EmployeeService {

    public List<Employee> findAll();

}

及其实现

public class EmployeeServiceImpl implements EmployeeService{

    @Resource
    private EmployeeRepository employeeRepository;


    @Override
    @Transactional
    public List<Employee> findAll() {
        return employeeRepository.findAll();
    }

}

我认为问题在于EmployeeService无法用作@Bean

您可以在以下位置找到完整的代码

https://github.com/ashishkumar9211/SpringRestHibernateRepository

更改:

@Bean
public EmployeeServiceImpl EmployeeServiceImpl() {
    System.out.println("Service");
    return new EmployeeServiceImpl();

}

@Bean
public EmployeeService EmployeeServiceImpl() {
    System.out.println("Service");
    return new EmployeeServiceImpl();

}

因此,您可以使用该界面进行自动布线。

并将@Service批注添加到接口的实现中,并将其从接口中删除。

接口:

public interface EmployeeService {

    public List<Employee> findAll();

}

实现方式:

@Service
public class EmployeeServiceImpl implements EmployeeService{

    @Resource
    private EmployeeRepository employeeRepository;


    @Override
    @Transactional
    public List<Employee> findAll() {
        return employeeRepository.findAll();
    }

}

由于您的AppConfig类被标注为@ComponentScan(“ com.spring”),因此您可以在RestController中删除@ComponentScan

@Controller
@RequestMapping("/employee")
public class RestControllerImpl {
  /* your code  */
}

并从界面中删除@Service批注,并将其添加到serviceimpl类。

根据日志,容器无法注入“ EmployeeService”

org.springframework.beans.factory.BeanCreationException:创建名称为'restControllerImpl'的bean时出错:自动连接依赖项的注入失败; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:com.spring.service.EmployeeService com.spring.controller.RestControllerImpl.employeeService; 嵌套异常为

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM