如题, 我在一个普通的java项目中新建了一个bean.xml, 想通过spring的ioc去获取对象 ,谁知道出现这种错误,尝试过几种解决办法:
classpath*:bean.xml
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:bean.xml");
UserImpl userImpl =(UserImpl)applicationContext.getBean("userImpl");
这种方式在applicationContext.getBean("userImpl");会出现Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userImpl' available
根据项目结构目录一级一级的往下找(解决问题)
![](https://img.haomeiwen.com/i3055930/7becfc3a58028134.png)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("cn/paul/bean.xml");
UserImpl userImpl =(UserImpl)applicationContext.getBean("userImpl");
String name= userImpl.getName("paul");
System.out.println(name);
网友评论