简体   繁体   English

org.springframework.beans.factory.CannotLoadBeanClassException

[英]org.springframework.beans.factory.CannotLoadBeanClassException

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]; 有异常消息:在上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.CannotLoadBeanClassException:找不到在类路径资源[beans.xml]中定义的名称为“ us”的bean的类[factory]。 nested exception is java.lang.ClassNotFoundException: factory 嵌套的异常是java.lang.ClassNotFoundException:factory

here is interface: package net.runze.spr1.factory; 这是接口:包net.runze.spr1.factory;

public interface Person {
    public void testBeing();
}

here is class cn 这是班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 这是我的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 真傻~~我在xml文件中配置错误

<bean id="us"...> this should be "factory-bean" not class.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.beans.factory.CannotLoadBeanClassException:找不到类 - org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class Tomcat提供了org.springframework.beans.factory.CannotLoadBeanClassException: - Tomcat gives org.springframework.beans.factory.CannotLoadBeanClassException: Spring异常处理:org.springframework.beans.factory.CannotLoadBeanClassException - Spring Exception Handling : org.springframework.beans.factory.CannotLoadBeanClassException org.springframework.beans.factory.CannotLoadBeanClassException:找不到类 - org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class 异常org.springframework.beans.factory.CannotLoadBeanClassException:找不到类 - Exception org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class Spring MVC项目中的org.springframework.beans.factory.CannotLoadBeanClassException - org.springframework.beans.factory.CannotLoadBeanClassException in Spring mvc project org.springframework.beans.factory.CannotLoadBeanClassException:找不到类[org.springframework.orm.hibernate5.HibernateTrasactionManager] - org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate5.HibernateTrasactionManager] “ org.springframework.beans.factory.CannotLoadBeanClassException:找不到类[org.springframework.jdbc.datasource - " org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource 集成jsf和spring异常:org.springframework.beans.factory.CannotLoadBeanClassException:找不到类 - integration jsf and spring Exception : org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class 部署到glassfish服务器时出现“ org.springframework.beans.factory.CannotLoadBeanClassException:找不到类…” - “org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class…” when deploying to glassfish server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM