首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

Spring构造方法注入时,注入一种方法时正常注入多种时报错

2014-01-19 
Spring构造方法注入时,注入一种方法时正常注入多种时报错,求助package testimport org.apache.commons.la

Spring构造方法注入时,注入一种方法时正常注入多种时报错,求助


package test;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
//import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
//import org.springframework.beans.factory.xml.XmlBeanFactory;
//import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//import org.springframework.core.io.ClassPathResource;

public class MockBusinessObject {
private String dependency1;
private int dependency;
public MockBusinessObject(String dependency)
{
this.dependency1 = dependency; 
}
public MockBusinessObject(int dependency)
{
this.dependency = dependency;
}
public int getMockBusinessObject(int dependency)
{
return dependency;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("dependency1", dependency1).append("dependency", dependency).toString();
}
public static void main(String[] args)
{
DefaultListableBeanFactory beanRegistry = new DefaultListableBeanFactory();
BeanFactory container = (BeanFactory)bindViaXMLFile(beanRegistry);
MockBusinessObject mockBusinessObject = (MockBusinessObject)container.getBean("MockBusinessObject");
System.out.println(mockBusinessObject.toString());
}
public static BeanFactory bindViaXMLFile(BeanDefinitionRegistry registry) 
{
//XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);
//reader.loadBeanDefinitions("classpath:/test/NewFile.xml");
//return (BeanFactory)registry;
//return new XmlBeanFactory(new ClassPathResource("/test/NewFile.xml"));
return new ClassPathXmlApplicationContext("classpath:/test/NewFile.xml"); 
}
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd 
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd 
http://www.springframework.org/schema/lang 
http://www.springframework.org/schema/lang/spring-lang-2.0.xsd 
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="MockBusinessObject" class="test.MockBusinessObject">
    <constructor-arg index="0" value="111111"/>
    <constructor-arg index="1" value="222222"/>
</bean>
</beans>

如果只注入value="111111",下一行注释掉就正常,加上第二行就报错如下
一月 17, 2014 3:56:08 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15b011c: startup date [Fri Jan 17 15:56:08 CST 2014]; root of context hierarchy
一月 17, 2014 3:56:09 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [test/NewFile.xml]
一月 17, 2014 3:56:10 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@699df5: defining beans [MockBusinessObject]; root of factory hierarchy
一月 17, 2014 3:56:10 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@699df5: defining beans [MockBusinessObject]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MockBusinessObject' defined in class path resource [test/NewFile.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:250)


at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.MockBusinessObject.bindViaXMLFile(MockBusinessObject.java:45)
at test.MockBusinessObject.main(MockBusinessObject.java:35)
是不是我的加载BeanFactoy有问题啊
[解决办法]
你没有2个参数的构造方法,配置2个参数当然不行

热点排行