Spring注入对象类型的属性问题

2019-01-25 10:08:08

用set方法的方式可以成功注入 用构造方法注入就出错了

  1. 错误信息
  2. WARN ClassPathXmlApplicationContext:546 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employee' defined in class path resource [applicationcontext.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [spring.demo4.Car2]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?

applicationcontext.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd">
  7. <!-- setter方法 -->
  8. <bean id="car2" class="spring.demo4.Car2">
  9. <!-- <property name="name" value="奔驰"></property>
  10. <property name="price" value="800000"></property> -->
  11. <constructor-arg name="name" value="奔驰"/>
  12. <constructor-arg name="price" value="1000000"/>
  13. </bean>
  14. <!-- 注入对象类型的属性 -->
  15. <bean id="employee" class="spring.demo4.Employee">
  16. <constructor-arg name="name" value="swd"/>
  17. <constructor-arg name="Car2" ref="car2"/>
  18. <!-- <property name="name" value="swd"></property>
  19. <property name="Car2" ref="car2"></property> -->
  20. </bean>
  21. </beans>

实体类Car2

  1. package spring.demo4;
  2. public class Car2 {
  3. private String name;
  4. private String price;
  5. public Car2(String name, String price) {
  6. super();
  7. this.name = name;
  8. this.price = price;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Car2 [name=" + name + ", price=" + price + "]";
  13. }
  14. }

实体类Employee

  1. package spring.demo4;
  2. public class Employee {
  3. private String name;
  4. private Car2 car2;
  5. public Employee(String name, Car2 car2) {
  6. super();
  7. this.name = name;
  8. this.car2 = car2;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Employee [name=" + name + ", car2=" + car2 + "]";
  13. }
  14. }

测试类

  1. package spring.demo4;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class SpringDemo4 {
  6. /**
  7. * set方法注入Car2的属性
  8. */
  9. @Test
  10. public void demo2(){
  11. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");
  12. Car2 car2 = (Car2)applicationContext.getBean("car2");
  13. System.out.println(car2);
  14. }
  15. /**
  16. * 注入对象属性
  17. */
  18. @Test
  19. public void demo3(){
  20. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");
  21. Employee employee = (Employee)applicationContext.getBean("employee");
  22. System.out.println(employee);
  23. }
  24. }

0
1
0

添加评论

正在回复:
取消
2
0
1
0