I found a simple problem and ask for help. There are the exception message: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [factory] for bean with name 'us' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundException: factory
here is interface: package net.runze.spr1.factory;
public interface Person {
public void testBeing();
}
here is class cn
package net.runze.spr1.factory;
public class CN implements Person {
@Override
public void testBeing() {
System.out.println("china");
}
}
here is class us:
package net.runze.spr1.factory;
public class US implements Person {
@Override
public void testBeing() {
System.out.println("lalalala US");
}
}
here is the factory:
package net.runze.spr1.factory;
public class PersonFactory {
public Person getPerson(String arg) {
if ("cn".equalsIgnoreCase(arg)) {
return new CN();
} else {
return new US();
}
}
}
here is my xml
<bean id="factory" class="net.runze.spr1.factory.PersonFactory"/>
<bean id="cn"
factory-bean="factory"
factory-method="getPerson">
<constructor-arg value="cn"></constructor-arg>
</bean>
<bean id="us"
class="factory"
factory-method="getPerson">
<constructor-arg value="us"></constructor-arg>
</bean>
here is my test class:
@Test
public void instanceFactorytest1() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person b1 = context.getBean("cn", US.class);
Person b2 = context.getBean("us", US.class);
b1.testBeing();
b2.testBeing();
}
Thank you very much
now i know. really silly~~ i configed it wrong in the xml file
<bean id="us"...> this should be "factory-bean" not class.