“线程中的异常”;“主要”;org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为';customerBo';定义

“线程中的异常”;“主要”;org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为';customerBo';定义,spring,aop,Spring,Aop,我在春季aop上做运动 我已经定义了我的spring-bean.xml文件,该文件包含所有bean的定义 它也在项目类路径中 但是我得到了运行时异常NosUCHBeanDefinitionException 问题是什么?没有得到 这是我在App.java中的代码 public class App { public static void main( String[] args ) { ApplicationContextappContext=new Class

我在春季aop上做运动 我已经定义了我的spring-bean.xml文件,该文件包含所有bean的定义 它也在项目类路径中 但是我得到了运行时异常NosUCHBeanDefinitionException 问题是什么?没有得到 这是我在App.java中的代码

public class App 
{
     public static void main( String[] args )
     {
       ApplicationContextappContext=new ClassPathXmlApplicationContext("classpath*:resources/Spring-Customer.xml");
       System.out.println("ApplicationContext:"+appContext);
       CustomerBo customer=(CustomerBo)appContext.getBean("customerBo");
       customer.addCustomer();
     }
}
下面是我的Spring-Customre.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" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:aspectj-autoproxy />

    <bean id="customerBo" class="com.kishor.customer.bo.Impl.CustomerBoImpl" />

    <!-- Aspect -->
    <bean id="logAspect" class="com.kishor.aspect.LoggingAspect" />

</beans>

您可能没有完全理解maven项目的项目结构,或者犯了一些错误:

  • src/main/java
    包含需要编译和修改的源代码
  • src/main/resources
    包含不能编译的资源
编译后的源代码和资源都放在同一个目录的末尾(当然子目录将保留)

因此,位于
src/main/resources
的文件将位于类路径根目录中!这意味着您必须从规范中删除
资源
文件夹名称,从中加载xml

ApplicationContextappContext=
      new ClassPathXmlApplicationContext("classpath*:Spring-Customer.xml");

我建议检查XML配置文件并添加以下行:


只需将应用程序上下文初始化更改为

  ApplicationContext applicationContext = new 
ClassPathXmlApplicationContext("Spring-Customer.xml");

你能用它的名字发布bean定义xml文件的内容吗?每当你遇到异常,我都会发布xml文件,这就是问题所在,发布堆栈跟踪。有很多有用的信息可以帮助您/我们调试问题。注意:如果您使用的是maven,则路径中不需要
资源。其中的所有内容都放在类路径的根目录中我使用maven时仍然遇到了这个错误这是我的堆栈跟踪
  ApplicationContext applicationContext = new 
ClassPathXmlApplicationContext("Spring-Customer.xml");