我正在尝试基于Annotations创建一个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
答案 0 :(得分:0)
变化:
@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();
}
}
答案 1 :(得分:0)
由于您的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;嵌套异常是