Spring Nosuchbeanexception

pkmbmrz7  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(147)

我创建了一个简单的Spring核心应用程序。
在尝试执行它时,我得到了NoSuch bean defined exception,即使我在config中定义了特定的bean。
以下是我的档案:

  1. #config.xml
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd">
  7. <bean id="reportService" class="com.nagesh.sample.ReportServie"/>
  8. </beans>

字符串
对象类

repoService.java

  1. `package com.nagesh.sample;
  2. public class ReportService {
  3. public void display() {
  4. System.out.println("Hi, Welcome to Report Generation application");
  5. }
  6. }


主类档桉

mainclass

  1. package com.nagesh.spring.Sample;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.nagesh.sample.ReportService;
  5. public class App
  6. {
  7. public static void main( String[] args )
  8. {
  9. System.out.println( "Hello World!" );
  10. ApplicationContext context =
  11. new ClassPathXmlApplicationContext("classpath*:config.xml");
  12. ReportService reportService =
  13. (ReportService) context.getBean("reportService");
  14. reportService.display();
  15. }
  16. }`


下面是pom.xml文件

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.nagesh.spring</groupId>
  5. <artifactId>Sample</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>Sample</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  14. <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-core</artifactId>
  18. <version>5.1.5.RELEASE</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-context</artifactId>
  23. <version>5.1.5.RELEASE</version>
  24. </dependency>
  25. </dependencies>
  26. </project>`


我该如何解决这个问题?

7vhp5slm

7vhp5slm1#

一个错误:

  1. <bean id="reportService" class="com.nagesh.sample.ReportServie"/>

字符串
改成

  1. <bean id="reportService" class="com.nagesh.sample.ReportService"/>


Job Done:)

相关问题