Spring构造注入的一点小问题,找到了解决办法,没找到为啥这么解决。

最近学习Spring,在使用构造注入时遇到一点问题,先上代码块:

VariableInjectionService类:

public class VariableInjectionService {
    private int ID;
	private int age;
	private String name;
    //准备构造方法注入
	public VariableInjectionService(int id,int age,String name) {
		this.ID = id;
		this.age = age;
		this.name = name;
	}
    //准备方法注入
    //geter/seter
    ...
}

对应配置文件:

<bean id="ConstructorInjectFirst" class="...(类路径没有问题).VariableInjectionService">
	<constructor-arg name="ID" value="1"></constructor-arg>
	<constructor-arg name="age" value="24" type="int"></constructor-arg>
	<constructor-arg name="name" value="张三" type="String"></constructor-arg>
</bean>

跑起来:

method(){
    ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    VariableInjectionService vIServer01 = (VariableInjectionService) appContext.getBean("ConstructorInjectFirst");
        ......
}	

一碗毒鸡汤:

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ConstructorInjectFirst' defined in class path resource [ApplicationContext.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [int] - did you specify the correct bean references as arguments?
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ConstructorInjectFirst' defined in class path resource [ApplicationContext.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [int] - did you specify the correct bean references as arguments?
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:719)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:197)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1267)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1124)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
	at com.itCodeSchool.MethodPackage.InjectTest.ConstructorTest01(InjectTest.java:12)
	at com.itCodeSchool.Tests.UserServiceTest.VariableInjection(UserServiceTest.java:19)
	at com.itCodeSchool.Tests.DebugTest.main(DebugTest.java:7)

毒鸡汤变鲜鸡汤:

<bean id="ConstructorInjectFirst" class="com.itCodeSchool.Servers.VariableInjectionService">
	<constructor-arg name="id" value="1"></constructor-arg>
	<constructor-arg name="age" value="24" type="int"></constructor-arg>
	<constructor-arg name="name" value="张三" type="String"></constructor-arg>
</bean>

问题来了:为什么改成了“id”就把毒鸡汤给变成了鲜鸡汤了呢?

注:这一个解决方法纯粹是我误打误撞整出来的,至于为啥这样整就能行,目前我还没有找到。

各位走过路过的大佬们,能帮忙解释一下吗?

猜你喜欢

转载自blog.csdn.net/txd2016_5_11/article/details/81388390