Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@Autowired不使用基于注释的配置_Java_Spring_Spring Mvc - Fatal编程技术网

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

Java Spring@Autowired不使用基于注释的配置,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在尝试创建一个基于注释的SpringMVC项目。 它可以正确编译,但在部署时会抛出错误 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

我正在尝试创建一个基于注释的SpringMVC项目。 它可以正确编译,但在部署时会抛出错误

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,在这里我有自动连线的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();

    };
@Service
public interface EmployeeService {

    public List<Employee> findAll();

}
更改:

@Bean
public EmployeeServiceImpl 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类被注释为@ComponentScancom.spring 您可以在RestController中删除@ComponentScan

并从接口中删除@Service注释,并将其添加到serviceimpl类中

根据日志,容器未能注入EmployeeService


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

,不会产生任何影响。返回类型是实现还是接口都无关紧要。对于初学者来说,控制器上的@ComponentScan什么都不做,它只在@Configuration类上工作@服务必须位于实现上,而不是接口上。从stacktrce判断,由于@Service和@Controller,情况可能会更糟,您可能最终会遇到重复的实例,从而导致不同的异常。要么扫描组件,要么使用@Bean方法,这两种方法都不行。我按照你的建议做了更改。很好。谢谢
@Bean
public EmployeeService EmployeeServiceImpl() {
    System.out.println("Service");
    return new EmployeeServiceImpl();

}
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();
    }

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