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

作甚 2014-01-17 04:06:40

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有问题啊
...全文
100 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
作甚 2014-01-17
  • 打赏
  • 举报
回复
引用 1 楼 yys79 的回复:
你没有2个参数的构造方法,配置2个参数当然不行
谢谢,新手,错误比较低级
loveunittesting 2014-01-17
  • 打赏
  • 举报
回复
你没有2个参数的构造方法,配置2个参数当然不行

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧