我正在尝试一个非常基本的例子 . 我试图看到其他堆栈溢出的答案,但无法解决这个问题 . 我是这个领域的新手,如果我遗漏了任何东西,请告诉我 .
这是我的错误日志:
2017年12月11日上午8:40:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO:从类路径资源加载XML bean定义[BeanDefinition.xml]线程“main”org.springframework.beans中的异常 . factory.NoSuchBeanDefinitionException:在org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory)的org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)中没有定义名为'MySpringBeanWithDepenency'的bean . java:1159)org.springframework上的org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) .context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)在model.main.Main.main(Main.java:18)
这显然说它找不到MySpringBeanWithDepenency
这是我的BeanDefinition.xml
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="model.testbean" />
<context:component-scan base-package="model.writer" />
这是我的主要课程:
package model.main;
/**
* Created by barora on 12/10/2017.
*/
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import model.testbean.MySpringBeanWithDepenency;
public class Main {
public static void main(String args[]){
ApplicationContext context = new ClassPathXmlApplicationContext(
"BeanDefinition.xml");
BeanFactory factory = context;
MySpringBeanWithDepenency test = (MySpringBeanWithDepenency) factory.getBean("MySpringBeanWithDepenency");
test.run();
}
}
这是我的MySpringBeanWithDepenency
package model.testbean;
/**
* Created by barora on 12/10/2017.
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import model.writer.IWriter;
import org.springframework.stereotype.Service;
@Service
public class MySpringBeanWithDepenency {
private IWriter writer;
@Autowired
public void setWriter(IWriter writer){
this.writer = writer;
}
public void run(){
String s = "This is my test";
writer.writer(s);
}
}
这是我的文件夹结构:
2 回答
尝试调试添加:
如果这打印出一个空列表,那么您的类路径扫描就会出现问题 . 如果它打印一个名称,那么这是一个可以用来获取bean的名称 .
或者,使用
factory.getBean(MySpringBeanWithDependency.class)
代替factory.getBean(String name)
,这样您就不需要知道Spring为您的bean提供了什么名称 .试试
MySpringBeanWithDepenency test = (MySpringBeanWithDepenency) factory.getBean(MySpringBeanWithDepenency.class);
EDITED
所以我've recreated code using given project structure. It works with exception that I' ve使用了以下代码
IWriter
并实施
EDITED 2 如果你想为
IWriter
接口提供两个实现,并且两者都应该是spring managed beans,你需要帮助spring容器决定你想要它自动装配哪个 . 请使用@Qualifier
. 有关详细信息,请参见tutorial