我已经通过在pom.xml中添加插件和Spring Boot依赖项将现有的spring应用程序转换为Spring Boot应用程序。这是我的pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- tag::actuator[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- end::actuator[] -->
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<!-- Spring Security -->
<!-- odbc -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.2.0</version>
</dependency>
<!-- odbc -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
以下是我的控制器类:
@Controller
@ComponentScan("com.eg.example.controller")
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@Autowired
private DatabaseDAO dao;
/*some code */
}
以下是DatabaseDAO.class
public class DatabaseDAO {
/*dao sepcific code*/
}
现在运行maven安装后,jar会在目标文件夹中构建,当我从命令提示符运行jar时,我收到以下错误:
错误SpringApplication - 应用程序启动失败org.springframework.beans.factory.BeanCreationException:使用名称&#39; homeController创建bean时出错&#39 ;:自动连接依赖项的注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.eg.example.dao.DatabaseDAO com.eg.example.controller.HomeController.dao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[com.eg.example.dao.DatabaseDAO]的限定bean依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)~ [spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)〜[spring-context-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)〜[spring-context-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)〜[spring-boot-1.1.9.RELEASE.jar!/:1.1.9.RELEASE]
在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)[spring-boot-1.1.9.RELEASE.jar!/:1.1.9.RELEASE]
在org.springframework.boot.SpringApplication.run(SpringApplication.java:320)[spring-boot-1.1.9.RELEASE.jar!/:1.1.9.RELEASE]
在org.springframework.boot.SpringApplication.run(SpringApplication.java:952)[spring-boot-1.1.9.RELEASE.jar!/:1.1.9.RELEASE]
在org.springframework.boot.SpringApplication.run(SpringApplication.java:941)[spring-boot-1.1.9.RELEASE.jar!/:1.1.9.RELEASE]
在com.ge.ideate.Application.main(Application.java:17)[gs-spring-boot-0.1.0.jar!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.7.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)〜[na:1.7.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)〜[na:1.7.0_51]
在java.lang.reflect.Method.invoke(未知来源)〜[na:1.7.0_51]
在org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)[gs-spring-boot-0.1.0.jar!/:na]
在java.lang.Thread.run(未知来源)[na:1.7.0_51]
引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.eg.example.dao.DatabaseDAO com.eg.example.controller.HomeController.dao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[com.eg.example.dao.DatabaseDAO]的限定bean依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:522)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
...省略了22个共同框架 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[com.ge.ideate.dao.IdeateDAO]的限定bean依赖:预期至少有1个bean有资格作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)~ [spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)〜[spring-beans-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
...省略了24个共同框架 java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
在java.lang.reflect.Method.invoke(未知来源)
在org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
在java.lang.Thread.run(未知来源)
引起:org.springframework.beans.factory.BeanCreationException:创建名称为&#39; homeController&#39;的注册自动连接依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.ge.ideate.dao.IdeateDAO com.ge.ideate.controller.HomeController.dao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[com.ge.ideate.dao.IdeateDAO]的限定bean:期望至少有一个bean符合此依赖关系的autowire候选资格。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
在org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
在org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
在org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
在org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
在com.ge.ideate.Application.main(Application.java:17)
......还有6个
我无法弄清楚错误的位置,请帮我解决这个问题。这是我的servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.eg.example.*" />
</beans:beans>
答案 0 :(得分:1)
您应该在@Repository
级以上注释DatabaseDAO
。 Spring上下文导致的这个错误无法找到bean名称DatabaseDao将其注入HomeController
组件。