- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试一个与依赖注入(inject)和 Autowiring 相关的简单 spring 框架示例。我面临一个难以解决的错误。使用了以下 jar 文件:
*commons-logging.jar
*org.springframework.asm-3.0.1.RELEASE.jar
*org.springframework.beans-3.0.1.RELEASE.jar
*org.springframework.context-3.0.1.RELEASE.jar
*org.springframework.core-3.0.1.RELEASE.jar
*org.springframework.expression-3.0.1.RELEASE.jar
以下是代码片段:
Engine.java
package beans;
public class Engine {
private String modelyear;
public Engine() {
}
public Engine(String modelyear) {
this.modelyear = modelyear;
}
public String getModelyear() {
return modelyear;
}
public void setModelyear(String modelyear) {
this.modelyear = modelyear;
}
}
Car.java
package beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Car {
@Qualifier(value="e1")
@Autowired
private Engine engine;
public void printData(){
System.out.println("Engine Model Year ="+engine.getModelyear());
}
}
spring.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- Activate autowire annotation -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean id="c" class="beans.Car" />
<bean id="e1" class="bean.Engine">
<property name="modelyear" value="2015" />
</bean>
<bean id="e2" class="bean.Engine">
<property name="modelyear" value="2016" />
</bean>
</beans>
Client.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Car;
public class Client {
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("resources/spring.xml");
Car c = (Car)ap.getBean("c");
c.printData();
}
}
执行上面的代码后,出现如下错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'c': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: beans.Engine beans.Car.engine; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [bean.Engine] for bean with name 'e1' defined in class path resource [resources/spring.xml]; nested exception is java.lang.ClassNotFoundException: bean.Engine
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.Client.main(Client.java:11)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: beans.Engine beans.Car.engine; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [bean.Engine] for bean with name 'e1' defined in class path resource [resources/spring.xml]; nested exception is java.lang.ClassNotFoundException: bean.Engine
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
... 13 more
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [bean.Engine] for bean with name 'e1' defined in class path resource [resources/spring.xml]; nested exception is java.lang.ClassNotFoundException: bean.Engine
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1208)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:570)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1277)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:303)
at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:185)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:810)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:767)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 15 more
Caused by: java.lang.ClassNotFoundException: bean.Engine
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1229)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1200)
... 23 more
最佳答案
您错误地定义了 <bean id="e1" class="bean.Engine">
& <bean id="e2" class="bean.Engine">
在 XML 中。类(class)应该是 beans.Engine
根据 Engine 类中定义的包名称
关于java - Autowiring 字段上的 org.springframework.beans.factory.BeanCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47088891/
我正在使用 grails cxf-Plugin,它在本地运行良好,但是当我将它部署在 tomcat 服务器上时出现错误: ERROR context.GrailsContextLoaderListen
这是 org.xml.sax.SAXParseException;cvc-complex-type.2.4.c: The matching wildcard is strict, but no dec
有人可以告诉我如何克服 BeanCreationException 吗? 将两个变量添加到 Owner.java 后,我收到 BeanCreationException如下: @OneToMany(c
我正在尝试使用 Spring 开发 RESTful Web 服务,然后将其部署在 Google App Engine 上。该服务的客户端将是一个 Android 应用程序。我是第一次使用 spring
2011-06-13 11:45:53,128 ERROR [org.springframework.web.context.ContextLoader] Context initialization
我正在尝试从 session 工厂获取 session 以用于我的 CRUD 方法。我试图在构造函数中设置 session ,但此时 bean 尚未初始化。创建 bean 后如何初始化它? 堆栈跟踪
我正在尝试在 glassfish 上部署 netbeans java ee 项目(maven + hibernate + JSF) 我在下面收到错误。我的数据库配置没问题(从多个应用程序仔细检查),并
我正在开始使用一个小型 Spring MVC 应用程序,当我运行服务器时,出现此错误,我不知道我做错了什么,我试图理解为什么 @Autowiring 不起作用: org.springframework
我正在使用 SpringBoot 开发 Spring 4 应用程序。 在com.test.tm包中, 应用类: @SpringBootApplication @EnableJpaRepositor
我正在设置一个 Spring Boot maven 多模块项目,其中包含域层、持久层和 Web 层。 我一直在查找类似的问题,但似乎没有一个是我的情况,我怀疑这是因为该项目被划分为单独的maven模块
我有一个在本地运行的项目,但是当我尝试将其部署到 tomcat 7 服务器时,出现以下异常: ERROR: [Feb-05 16:39:30,240] web.context.ContextLoade
我尝试在 Spring 应用程序中使用 Hibernate,但项目部署时出现错误 Exception while loading the app : java.lang.IllegalStateExc
我尝试使用 spring4 和 jackrabbit 开发一些服务器程序。当我使用 JUnit 测试 Controller 类时,发生 BeanCreationExceptrion 错误。我认为这个错
我正在研究STS数据库连接。 我尝试连接我的本地数据库。 我的HomeController.java: @Controller public class HomeController { @Autow
我的 bean 定义, 异常, Caused by: org.springframework.beans.factory.BeanCreationException: Error creating
在启动过程中,我的应用程序创建了一个 bean,它在任务执行器中调度一些任务,然后在创建另一个 bean 后失败。这使我的应用程序处于不死状态,应用程序看起来正在运行但不提供功能。我想知道如何全局处理
我收到以下错误。谁能告诉我错误堆栈中的哪一行定义了我的错误? 2019-02-11 19:26:02.681 [main] ERROR o.s.web.context.ContextLoader -
我已经开始将 Spring MVC/Spring Web Tomcat 应用程序迁移到 Spring Boot。目前我正在将 xml 配置文件迁移到 java 配置。 当我尝试通过 mvn sprin
我在 Tomcat 上使用 Spring MVC。当我尝试启动 tomcat 时出现此异常: WARNING: Exception encountered during context initial
试图整合hibernate和spring,我遇到了这个错误 SEVERE: Context initialization failed org.springframework.beans.factor
我是一名优秀的程序员,十分优秀!