Reputation: 800
I am trying a very basic example. I tried to see other stack overflow answers, but couldn't solve this problem. I am new to this space, let me know if I left out anything.
Here is my error log :
Dec 11, 2017 8:40:20 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [BeanDefinition.xml] Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'MySpringBeanWithDepenency' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973) at model.main.Main.main(Main.java:18)
which clearly says it cannot find the MySpringBeanWithDepenency
Here is my 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" />
this is my main class :
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();
}
}
this is my 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);
}
}
this is my folder structure :
Upvotes: 1
Views: 27328
Reputation: 1
The default bean name is mySpringBeanWithDepenency with "m" you should use
MySpringBeanWithDepenency test = (MySpringBeanWithDepenency)factory.getBean("mySpringBeanWithDepenency");
Upvotes: 0
Reputation: 41271
Try to debug adding:
System.out.println("Bean names: " + Arrays.toString(context.getBeanNamesForType(MySpringBeanWithDependency.class)));
If this prints an empty list, then you have an issue with your classpath scanning. If it prints a name, then that's a name you can use to get the bean.
Alternatively, instead of factory.getBean(String name)
use factory.getBean(MySpringBeanWithDependency.class)
, so that you don't need to know what name Spring has given your bean.
Upvotes: 4
Reputation: 175
Try MySpringBeanWithDepenency test = (MySpringBeanWithDepenency) factory.getBean(MySpringBeanWithDepenency.class);
EDITED
So I've recreated code using given project structure. It works with exception that I've used following code for IWriter
package model.writer;
public interface IWriter {
void writer(String s);
}
and for implementation
package model.writer;
import org.springframework.stereotype.Component;
@Component
public class NiceWriter implements IWriter {
public void writer(String s) {
}
}
EDITED 2
If you want to have two implementation for IWriter
interface and both supposed to be spring managed beans you need to help spring container to decide which one you want it to autowire. Use @Qualifier
for this. See tutorial for details
Upvotes: 1