Spring IoC
概念
inversion of control,控制反转,所谓的反转是将主动权交给了spring容器,由spring容器来动态决定创建对象
使用
GirlFriend.java
package com.qfedu.entity;
public class GirlFriend {
private String look;
private int age;
private String height;
private double money;
private boolean sex;
private Addr home;
private Addr now;
public GirlFriend() {
super();
System.out.println("this is constructor without param.");
}
public GirlFriend(String look, int age, String height, double money) {
super();
this.look = look;
this.age = age;
this.height = height;
this.money = money;
System.out.println("this is constructor with 4 params.");
}
public GirlFriend(String look, int age, String height, double money, boolean sex) {
super();
this.look = look;
this.age = age;
this.height = height;
this.money = money;
this.sex = sex;
System.out.println("this is constructor with 5 params.");
}
public GirlFriend(String look, int age, String height, double money, boolean sex, Addr home,Addr now) {
super();
this.look = look;
this.age = age;
this.height = height;
this.money = money;
this.sex = sex;
this.home = home;
this.now = now;
System.out.println("this is constructor with 7 params.");
}
@Override
public String toString() {
return "GirlFriend [look=" + look + ", age=" + age + ", height=" + height + ", money=" + money+ ", sex=" + sex
+ ", home=" + home + ", now=" + now + "]";
}
public String getLook() {
return look;
}
public void setLook(String look) {
this.look = look;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public Addr getHome() {
return home;
}
public void setHome(Addr home) {
this.home = home;
}
public Addr getNow() {
return now;
}
public void setNow(Addr now) {
this.now = now;
}
}
resource/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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="gf1" class="com.qfedu.entity.GirlFriend" scope="singleton">
<property name="age" value="20" />
<property name="height" value="160" />
<property name="look" value="quanzhixian" />
<property name="money" value="8000" />
<property name="sex" value="false"></property>
</bean>
<bean id="gf2" class="com.qfedu.entity.GirlFriend">
<property name="age" value="20" />
<property name="height" value="160" />
<property name="look" value="quanzhixian" />
<property name="money" value="8000" />
<property name="sex" value="false"></property>
</bean>
<bean id="gf3" class="com.qfedu.entity.GirlFriend">
<property name="sex" value="true"></property>
<constructor-arg name="look" value="wuyifan" />
<constructor-arg name="height" value="180" />
<constructor-arg name="money" value="500000" />
<constructor-arg name="age" value="22" />
</bean>
<bean id="gf4" class="com.qfedu.entity.GirlFriend">
<constructor-arg name="look" value="wuyifan" />
<constructor-arg name="money" value="500000" />
<constructor-arg name="age" value="29" />
<constructor-arg name="height" value="180" />
<constructor-arg name="sex" value="true" />
</bean>
<bean id="gf5" class="com.qfedu.entity.GirlFriend">
<constructor-arg value="wuyifan" />
<constructor-arg value="29" />
<constructor-arg value="180" />
<constructor-arg value="500000" />
<constructor-arg value="true" />
</bean>
<bean id="gf6" class="com.qfedu.entity.GirlFriend">
<constructor-arg index="0" value="wuyifan" />
<constructor-arg index="1" value="29" />
<constructor-arg index="2" value="180" />
<constructor-arg index="3" value="500000" />
<constructor-arg index="4" value="true" />
</bean> -->
<bean id="gf7" class="com.qfedu.entity.GirlFriend">
<constructor-arg type="java.lang.String" value="180" />
<constructor-arg type="double" value="500000" />
<constructor-arg type="boolean" value="true" />
<constructor-arg type="java.lang.String" value="wuyifan" />
<constructor-arg type="int" value="29" />
</bean>
<bean id="now" class="com.qfedu.entity.Addr">
<property name="province" value="beijing" />
<property name="city" value="beijing" />
<property name="area" value="chaoyang" />
</bean>
<bean id="home" class="com.qfedu.entity.Addr">
<property name="province" value="shaanxi" />
<property name="city" value="xian" />
<property name="area" value="gaoxinqu" />
</bean>
<bean id="gf8" class="com.qfedu.entity.GirlFriend">
<property name="age" value="20" />
<property name="height" value="160" />
<property name="look" value="quanzhixian" />
<property name="money" value="8000" />
<property name="sex" value="false"/>
<property name="home" ref="home" />
<property name="now" ref="now" />
</bean>
<bean id="gf9" class="com.qfedu.entity.GirlFriend">
<constructor-arg name="look" value="wuyifan" />
<constructor-arg name="money" value="500000" />
<constructor-arg name="age" value="29" />
<constructor-arg name="height" value="180" />
<constructor-arg name="sex" value="true" />
<constructor-arg name="home" ref="home" />
<constructor-arg name="now" ref="now" />
</bean>
</beans>
GirlFriendDemoSpring.java
package com.qfedu.entity;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class GirlFriendDemoSpring {
@Test
public void testGirlFriend() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
//GirlFriend gf1 = (GirlFriend)ac.getBean("gf1");
//System.out.println(gf1);
//
//GirlFriend gf2 = (GirlFriend)ac.getBean("gf1");
//System.out.println(gf2);
//
//System.out.println(gf1 == gf2);
//
//GirlFriend gf3 = (GirlFriend)ac.getBean("gf2");
//System.out.println(gf3);
//
//System.out.println(gf1 == gf3);
//
//
//GirlFriend gf4 = (GirlFriend)ac.getBean("gf3");
//System.out.println(gf4);
//
//GirlFriend gf5 = (GirlFriend)ac.getBean("gf4");
//System.out.println(gf5);
//
//GirlFriend gf6 = (GirlFriend)ac.getBean("gf5");
//System.out.println(gf6);
//
//GirlFriend gf7 = (GirlFriend)ac.getBean("gf6");
//System.out.println(gf7);
//
//GirlFriend gf8 = (GirlFriend)ac.getBean("gf7");
//System.out.println(gf8);
//
//GirlFriend gf9 = (GirlFriend)ac.getBean("gf8");
//System.out.println(gf9);
GirlFriend gf10 = (GirlFriend)ac.getBean("gf9");
System.out.println(gf10);
ac.close();
}
}
单纯的bean节点,要求我们bean对象必须包含无参构造器
每个bean对象对于spring容器而言,默认都是单例的,通过scope来修改它的创建方式,总有四个值singleton, prototype(java), request, session
property属性要求我们的bean对象必须要有标准的setter
<property name="age" value="20" />
<property name="**home" ref="home" />
对于beans.xml文件而言,里面定义的都是bean对象,对于spring容器而言,所有对象都可以被认为是bean对象
说明
对于带有参数构造器的对象的创建方式,使用bean和constructor-arg来调用带参构造器完成对象的创建,其创建方式有四种
name + value/ref(可读性最高,顺序随意换)
value(代码最简单,但是可读性最差,顺序不能换)
index + value/ref(简单,但是可读性一般, index值从0开始)
Type + value/ref(相同类型的值按照顺序赋值,不同类型数据可以自由换顺序)
对象赋值
可以使用ref
property赋值
构造器赋值
<property name="home" ref="home" />
<property name="now" ref="now" /><constructor-arg name="home" ref="home" />
<constructor-arg name="now" ref="now" /><?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:ppp="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="home" class="com.qfedu.entity.Addr" ppp:province="shaanxi" ppp:city="xian"ppp:area="gaoxin" />
<bean id="now" class="com.qfedu.entity.Addr" ppp:province="beijing" ppp:city="beijing"ppp:area="chaoyang" />
<bean id="gf1" class="com.qfedu.entity.GirlFriend" ppp:look="quanzhixian" ppp:height="160"ppp:home-ref="home" ppp:now-ref="now" />
</beans>
要在头文件部分添加p的namespace的声明,p的namespace的值可以修改 2. 一旦修改了namespace的值,后期所有的属性都应该以该修改后的名字来进行赋值操作 3. namespace对应的链接 不能修改 4. 可以简化代码
说明:
p 的namespace赋值
说明:
要在头文件部分添加p的namespace的声明,p的namespace的值可以修改
一旦修改了namespace的值,后期所有的属性都应该以该修改后的名字来进行赋值操作
namespace对应的链接 不能修改
目的是可以简化代码
<?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:ppp="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="home" class="com.qfedu.entity.Addr" ppp:province="shaanxi" ppp:city="xian"ppp:area="gaoxin" />
<bean id="now" class="com.qfedu.entity.Addr" ppp:province="beijing" ppp:city="beijing"ppp:area="chaoyang" />
<bean id="gf1" class="com.qfedu.entity.GirlFriend" ppp:look="quanzhixian" ppp:height="160" ppp:home-ref="home" ppp:now-ref="now" />
</beans>