日期:2014-05-18  浏览次数:20638 次

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="MockBus