Spring Nosuchbeanexception

pkmbmrz7  于 4个月前  发布在  Spring
关注(0)|答案(1)|浏览(57)

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

#config.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.xsd">
    
    <bean id="reportService" class="com.nagesh.sample.ReportServie"/>
  </beans>

字符串
对象类

repoService.java

`package com.nagesh.sample;

public class ReportService {
    public void display() {
        System.out.println("Hi, Welcome to Report Generation application");
    }
}


主类档桉

mainclass

package com.nagesh.spring.Sample;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.nagesh.sample.ReportService;

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        ApplicationContext context =
                    new ClassPathXmlApplicationContext("classpath*:config.xml");
         ReportService reportService = 
                   (ReportService) context.getBean("reportService");
       reportService.display();
    }
}`


下面是pom.xml文件

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.nagesh.spring</groupId>
  <artifactId>Sample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Sample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>

  

  </dependencies>
</project>`


我该如何解决这个问题?

7vhp5slm

7vhp5slm1#

一个错误:

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

字符串
改成

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


Job Done:)

相关问题