beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 使用无参构造函数创建People对象-->
<bean id="people" class="com.qian.test1.People">
<property name="name" value="ZhouShuyi"/>
<property name="age" value="27"/>
<property name="address" value="上海"/>
</bean>
<!-- 使用含参构造函数创建People1对象-->
<bean id="people1" class="com.qian.test1.People">
<constructor-arg index="0" value="Zhangsan"/>
<constructor-arg name="age" value="21"/>
<constructor-arg type="java.lang.String" value="杭州"/>
</bean>
</beans>
People类:
package com.qian.test1;
public class People {
private String name;
private int age;
private String address;
public People(){
System.out.println("无参构造函数");
}
public People(String name,int age,String address){
System.out.println("含参数构造函数");
this.name=name;
this.age=age;
this.address=address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void showMessage(){
System.out.println("name is "+name+" age is "+age+" address is "+address);
}
}
测试类:
import com.qian.test1.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People p=(People)context.getBean("people");
p.showMessage();
}
}
运行结果:
无参构造函数
含参数构造函数
name is ZhouShuyi age is 27 address is 上海
如图,我分别用无参构造函数和有参构造函数实例化了两个People类bean对象
使用无参构造函数创建bean对象可以用
<property name="属性名" value="属性值"/>
的方法初始化属性值,可以不初始化,则属性为默认值
使用有参构造函数创建bean对象可以用
<constructor-arg index="下标" value="属性值"/> 或
<constructor-arg name="属性名" value="属性值"/> 或
<constructor-arg type="类型" value="属性值"/>
三种方法作为构造函数参数,不可以省略,若使用第三种时多个属性类型相同,则按顺序初始化值
可以看到我并没有使用getBean("people1"),控制台却打印了 "有参构造函数" ,所以在我使用前bean中的对象就已经被初始化了,而我做的操作只是拿出来使用。
C、P命名空间
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 使用p命名空间注入,创建People对象-->
<bean id="people" class="com.qian.test1.People" p:name="zhou1" p:age="27" p:address="上海"/>
<!-- 使用c命名空间注入,创建People1对象-->
<bean id="people1" class="com.qian.test1.People" c:name="zhou2" c:age="27" c:address="上海"/>
</beans>
c同constructor-arg,p同property,其实质是构造器注入,这样只是用起来会方便一点,只需要导入约束即可。