org.springframework.beans.factory.CannotLoadBeanClassException
org.springframework.beans.factory.CannotLoadBeanClassException
我发现了一个简单的问题并寻求帮助。
有异常信息:
上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.CannotLoadBeanClassException:找不到 class [factory] 用于 class 路径资源 [beans.xml 中定义的名称为 'us' 的 bean ];嵌套异常是 java.lang.ClassNotFoundException: factory
这是界面:
包裹 net.runze.spr1.factory;
public interface Person {
public void testBeing();
}
这里是classcn
package net.runze.spr1.factory;
public class CN implements Person {
@Override
public void testBeing() {
System.out.println("china");
}
}
这里是class我们:
package net.runze.spr1.factory;
public class US implements Person {
@Override
public void testBeing() {
System.out.println("lalalala US");
}
}
这里是工厂:
package net.runze.spr1.factory;
public class PersonFactory {
public Person getPerson(String arg) {
if ("cn".equalsIgnoreCase(arg)) {
return new CN();
} else {
return new US();
}
}
}
这是我的 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>
这是我的测试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();
}
非常感谢
现在我知道了。真傻~~
我在 xml 文件
中配置错误
<bean id="us"...> this should be "factory-bean" not class.
我发现了一个简单的问题并寻求帮助。 有异常信息: 上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.CannotLoadBeanClassException:找不到 class [factory] 用于 class 路径资源 [beans.xml 中定义的名称为 'us' 的 bean ];嵌套异常是 java.lang.ClassNotFoundException: factory
这是界面: 包裹 net.runze.spr1.factory;
public interface Person {
public void testBeing();
}
这里是classcn
package net.runze.spr1.factory;
public class CN implements Person {
@Override
public void testBeing() {
System.out.println("china");
}
}
这里是class我们:
package net.runze.spr1.factory;
public class US implements Person {
@Override
public void testBeing() {
System.out.println("lalalala US");
}
}
这里是工厂:
package net.runze.spr1.factory;
public class PersonFactory {
public Person getPerson(String arg) {
if ("cn".equalsIgnoreCase(arg)) {
return new CN();
} else {
return new US();
}
}
}
这是我的 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>
这是我的测试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();
}
非常感谢
现在我知道了。真傻~~ 我在 xml 文件
中配置错误<bean id="us"...> this should be "factory-bean" not class.