注入Bean属性---构造注入配置方案
在Spring配置文件中通过<constructor-arg>元素为构造方法传参
注意:
1、一个<constructor-arg>元素表示构造方法的一个参数,且使用时不区分顺序。
2、通过<constructor-arg>元素的index 属性可以指定该参数的位置索引,位置从0 开始。
3、<constructor-arg>元素还提供了type 属性用来指定参数的类型,避免字符串和基本数据类型的混淆。
注入不同数据类型
构造器注入案例:
创建两个实体类 Car 和Student 并封装,重写toString方法
//Car
package cn.happy.entity; public class Car { private String color; private String brand; //无参构造 public Car() { } //带参构造 public Car(String color, String brand) { super(); this.color = color; this.brand = brand; } @Override public String toString() { return "Car [color=" + color + ", brand=" + brand + "]"; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
//Student类 package cn.happy.entity; public class Student { private String name; private String age; private Car car; //无参构造 public Student() { //System.out.println("Student.Student()"); } //带参构造 public Student(String name, String age, Car car) { this.name = name; this.age = age; this.car = car; } @Override //重写toString方法 public String toString() { return "Student [name=" + name + ", age=" + age + ", car=" + car + "]"; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
在applicationContext.xml中配置如下
<!-- 构造注入 --> <bean id="car" class="entity.Car"> <property name="brand" value="二八大杠"></property> </bean> <bean id="student" class="entity.Student"> <constructor-arg index="0" value="成成"></constructor-arg> <constructor-arg index="1" value="28"></constructor-arg> <constructor-arg index="2" ref="car"></constructor-arg> </bean> p命名空间注入 <bean id="car" class="entity.Car" p:color="black"> <property name="brand" value="贱贱牌自行车"></property> </bean> <bean id="student" class="entity.Student" p:name="贱贱" p:age="25" p:car-ref="car"/>
然后书写单测代码:
public void addTest(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); Student student=(Student) ctx.getBean("stu"); System.out.println(student);
运行后的结果:
___________________________________________________________________________________________________________________________
设值注入
applicationContext.xml中的配置
<!--设值注入 --> <bean id="car" class="cn.happy.entity.Car"> <property name="brand" value="二手夏利"></property> </bean> <bean id="stu" class="cn.happy.entity.Student" scope="prototype"> <property name="name" value="呵呵"></property> <property name="age" value="13"></property> <property name="car" ref="car"></property> </bean>
运行结果:
________________________________________________________________________________________________________________________
P标签注入
<!-- p标签注入 --> <bean id="car" class="cn.happy.entity.Car" p:color="red"> <property name="brand" value="奥迪"></property> </bean> <bean id="stu" class="cn.happy.entity.Student" p:name="李四" p:age="12" p:car-ref="car"> </bean>
运行结果