#java #xml #spring #javabeans #applicationcontext
Вопрос:
Я новичок в Spring Framework
Я пытаюсь создать объект для класса, очень простую вещь
но при чтении я получаю много ошибок
Он показывает исключение ClassNotFound
Исходный код выглядит следующим образом:
package com.practice.spring;
//sim interface
public interface Sim {
void calling();
void data();
}
// class airtel implementing the sim interface
public class Airtel implements Sim {
@Override
public void calling() {
System.out.println("Calling from an Airtel :");
}
@Override
public void data() {
System.out.println("Data from an Airtel:");
}
}
//The driver Class
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mobile {
public static void main(String[] args) {
/*
* Sim a = new Idea(); a.calling(); a.data();
*/
ApplicationContext con = new ClassPathXmlApplicationContext("beans.xml");
Airtel s = con.getBean("air", Airtel.class);
s.calling();
s.data();
// not also working with when using with Interface reference as
Sim s = con.getBean("air",Airtel.class);
}
}
XML-файл является
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="air" class = "/SpringDemo/src/com/practice/spring/Airtel.java"></bean>
</beans>
Ошибка, которую я получаю, заключается в следующем:
Jun 03, 2021 7:24:52 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [/SpringDemo/src/com/practice/spring/Airtel.java] for bean with name 'air' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundException: /SpringDemo/src/com/practice/spring/Airtel.java
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [/SpringDemo/src/com/practice/spring/Airtel.java] for bean with name 'air' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundException: /SpringDemo/src/com/practice/spring/Airtel.java
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:681)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:648)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1614)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1090)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:877)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.practice.spring.Mobile.main(Mobile.java:14)
Caused by: java.lang.ClassNotFoundException: /SpringDemo/src/com/practice/spring/Airtel.java
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:427)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:284)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:469)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1551)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1478)
... 10 more
and finally, the JAR files that I added externally are:
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-aop-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-aop-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-aop-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-aspects-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-aspects-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-aspects-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-beans-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-beans-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-beans-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-context-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-context-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-context-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-context-indexer-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-context-indexer-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-context-indexer-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-context-support-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-context-support-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-context-support-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-core-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-core-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-core-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-expression-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-expression-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-expression-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-instrument-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-instrument-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-instrument-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-jcl-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-jcl-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-jcl-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-jdbc-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-jdbc-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-jdbc-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-jms-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-jms-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-jms-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-messaging-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-messaging-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-messaging-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-orm-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-orm-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-orm-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-oxm-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-oxm-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-oxm-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-test-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-test-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-test-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-tx-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-tx-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-tx-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-web-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-web-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-web-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-webflux-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-webflux-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-webflux-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-webmvc-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-webmvc-5.2.15.RELEASE-javadoc.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-webmvc-5.2.15.RELEASE-sources.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-websocket-5.2.15.RELEASE.jar
C:Spring Librariesspring-framework-5.2.15.RELEASElibsspring-websocket-5.2.15.RELEASE-javadoc.jar
spring-websocket-5.2.15.RELEASE-sources.jar
Путь к классу предназначен для класса Airtel, а интерфейс Sim-карты: C:Spring ПроектSpringDemosrccomпрактика
а для XML-файла это : C:Spring ПроектSpringDemosrc
IDE: Eclipse
Как я могу сделать этот исполняемый файл, поскольку я хотел знать, как контейнер IOC считывает XML-файл для создания объектов?
Ценю отзывы, которые вы можете предоставить
Спасибо
Комментарии:
1. Обновите beans.xml ссылка на класс как com.practice.spring. Эйртел . Исключение связано с тем, что путь к классу указан неверно