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 :
import
s in your quoted code, but notpackage
. Does your class file begin withpackage model.testbean;
? – slimdepenency
instead ofdependency
, it jolts me out of being able to concentrate on the code. – slim