java 继承 默认作用域,Spring(三)——Bean的 继承关系、依赖关系、作用域
1、Bean的继承关系
此处的继承并非面向对象当中的继承关系,而是 配置 上的继承关系。
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">
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// 导入IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取Person对象
Person person1 = (Person) ctx.getBean("ren1");
Person person2 = (Person) ctx.getBean("ren2");
// 输出Person
System.out.println(person1);
System.out.println(person2);
}
}
在 ren2 的 bean 中是有一个 parent 的属性的,这个属性指向 ren1 bean,这说明 ren2 继承了 ren1 的内容的。
但是如果在ren2有属性值,那么将重写该属性值,类似于面向对象的重写。
Spring允许继承 bean 的配置,被继承的 bean 成为父 bean,继承这个父 bean 的bean 成为子 bean(这并非我们所说的父类和子类)。子 bean 可以覆盖父 bean 的属性。
这个继承有什么用呢?首先可以省去重复的配置,你只需要继承一个Bean就可以不需要配置那么多的属性了。父bean作为模板,添加abstract属性为true就行了,例如
这样的话就不能创建ren1的实例,只用来被继承。
如果一个bean没有被指定class属性,那么它必须是抽象bean,即 abstract=true。
2、Bean的依赖关系
Car.java
package test;
public class Car {
private String brand;
private String carID;
public Car() {
super();
}
}
Person.java
package test;
public class Person {
private String name;
private int age;
private Car car;
public Person() {
}
}
applicationContext.xml
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">
在这里,我们模拟的是每个人有一辆车,而车又是一个JavaBean,因此我们这里需要用到引用,即在property中不再是value属性了而是ref。
这里需要注意的是,如果没有定义car的JavaBean那么就会报错的。所以我们才说Person依赖于Car。这也很容易理解,我都没写id,怎么填写ref的值呢?
** 小结 **
继承关系,在子bean中添加parent属性,属性值为父bean的id。同时父bean可以添加abstract属性作为子bean的模版,变成抽象bean。
依赖关系。首先需要定义依赖的bean(此例为Car),然后在被依赖bean(此例为Person)中的属性值用ref取代value,并指向依赖bean的ID。
3、Bean的作用域
默认情况下,IOC容器,即那个xml文件只会为bean创建一个对象,这就是所说的单例类。每次调用这个bean的,容器只会返回同一个bean对象。
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">
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// 导入IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取Person对象
Car car1 = (Car) ctx.getBean("che");
Car car2 = (Car) ctx.getBean("che");
// 输出Person
System.out.println(car1 == car2);
}
}
这里的输出结果是 true 说明这两个是同一个对象的。
修改配置文件:
运行Mian.java可以看到输出结果为false。
我们看到scope的属性,这里singleton当然就是这个属性的默认值了,将 scope="prototype" 即原型。
原型是指:每次向容器中获取bean对象,都会返回一个新的bean,即这两个bean不是同一个对象。
我们同时发现这个 scope 的取值还有 session 和 request ,这两个比较少用就不说了。
小结
使用bean的 scope 属性来配置bean的作用域。
singleton:默认值。容器初始时创建bean实例,在这整个容器的声明周期内只创建这一个bean
prototype:原型的。容器初始化不创建bean的实例。而在每次请求时都创建一个新的Bean实例,并返回。
session和request:少用。