spring学习笔记1-spring Ioc容器-Spring Bean的生命周期和定义继承
Bean的生命周期
理解 Spring bean 的生命周期很容易。当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态。同样,当 bean 不再需要,并且从容器中移除时,可能需要做一些清除工作。
为了定义安装和拆卸一个 bean,我们只要声明带有 init-method 和/或 destroy-method 参数的 。init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法。同样,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。
Bean的生命周期可以表达为:Bean的定义——Bean的初始化——Bean的使用——Bean的销毁
如果你在非 web 应用程序环境中使用 Spring 的 IoC 容器;例如在丰富的客户端桌面环境中;那么在 JVM 中你要注册关闭 hook。这样做可以确保正常关闭,为了让所有的资源都被释放,可以在单个 beans 上调用 destroy 方法。
实例:
- 创建一个名为IocTest的Java工程
- 在工程中导入spring库文件。
- 在src下创建名为org.TestIoc05的包,在包下创建两个class文件,分别是TestIoc05.java和Tstudent.java
- 在src下创建名为Beans.xml的Bean配置文件
下面是文件TestIoc05.java的内容
package org.TestIoc05;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIoc05 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Tstudent student = (Tstudent)context.getBean("Tstudent05");
student.helloStu();
((AbstractApplicationContext) context).registerShutdownHook();
}
}
下面是文件Tstudent.java的内容
package org.TestIoc05;
public class Tstudent {
private String name;
public void setName(String name) {
this.name=name;
}
public void helloStu() {
System.out.println("hello student "+name+" .");
}
public void init() {
System.out.println("this is a init method! ");
}
public void destory() {
System.out.println("this is a destory method! ");
}
}
下面是文件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">
<bean id="Tstudent01" class="org.TestIoc01.Tstudent">
<property name="iocInfo" value="BeanFactory"></property>
</bean>
<bean id="Tstudent02" class="org.TestIoc02.Tstudent">
<property name="iocInfo" value="ApplicationContext"></property>
</bean>
<bean id="Tstudent03" class="org.TestIoc03.Tstudent" scope="singleton">
<property name="name" value="zs"></property>
</bean>
<bean id="Tstudent04" class="org.TestIoc04.Tstudent" scope="prototype">
<property name="name" value="zs"></property>
</bean>
<bean id="Tstudent05" class="org.TestIoc05.Tstudent" init-method="init" destroy-method="destory">
<property name="name" value="zs"></property>
</bean>
</beans>
输出结果如下:
Bean的定义继承
bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等。
子 bean 的定义继承父定义的配置数据。子定义可以根据需要重写一些值,或者添加其他值。
Spring Bean 定义的继承与 Java 类的继承无关,但是继承的概念是一样的。你可以定义一个父 bean 的定义作为模板和其他子 bean 就可以从父 bean 中继承所需的配置。
当你使用基于 XML 的配置元数据时,通过使用父属性,指定父 bean 作为该属性的值来表明子 bean 的定义。
实例:
- 创建一个名为IocTest的Java工程
- 在工程中导入spring库文件。
- 在src下创建名为org.TestIoc06的包,在包下创建两个class文件,分别是TestIoc06.java和Tstudent.java
- 在src下创建名为Beans.xml的Bean配置文件
下面是文件TestIoc06.java的内容
package org.TestIoc06;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIoc06 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Tstudent1 student1 = (Tstudent1)context.getBean("Tstudent06");
student1.getName();
student1.getAge();
System.out.println("----------------------");
Tstudent2 student2 = (Tstudent2)context.getBean("Tstudent07");
student2.getName();
student2.getId();
student2.getAge();
}
}
下面是文件Tstudent1.java的内容
package org.TestIoc06;
public class Tstudent1 {
private String name;
private String age;
public void setName(String name) {
this.name=name;
}
public void setAge(String age) {
this.age=age;
}
public void getName() {
System.out.println("my name is "+name);
}
public void getAge() {
System.out.println("my age is "+age);
}
}
下面是文件Tstudent2.java的内容
package org.TestIoc06;
public class Tstudent2 {
private String name;
private String id;
private String age;
public void setName(String name) {
this.name=name;
}
public void setId(String id) {
this.id=id;
}
public void setAge(String age) {
this.age = age;
}
public void getAge() {
System.out.println("my age is "+age);
}
public void getName() {
System.out.println("my name is "+name);
}
public void getId() {
System.out.println("my id is "+id);
}
}
下面是文件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">
<bean id="Tstudent01" class="org.TestIoc01.Tstudent">
<property name="iocInfo" value="BeanFactory"></property>
</bean>
<bean id="Tstudent02" class="org.TestIoc02.Tstudent">
<property name="iocInfo" value="ApplicationContext"></property>
</bean>
<bean id="Tstudent03" class="org.TestIoc03.Tstudent" scope="singleton">
<property name="name" value="zs"></property>
</bean>
<bean id="Tstudent04" class="org.TestIoc04.Tstudent" scope="prototype">
<property name="name" value="zs"></property>
</bean>
<!-- init-method="init" destroy-method="destory" -->
<bean id="Tstudent05" class="org.TestIoc05.Tstudent">
<property name="name" value="zs"></property>
</bean>
<bean id="Tstudent06" class="org.TestIoc06.Tstudent1">
<property name="name" value="zs"></property>
<property name="age" value="18"></property>
</bean>
<bean id="Tstudent07" class="org.TestIoc06.Tstudent2" parent="Tstudent06">
<property name="name" value="ls"></property>
<property name="id" value="111000"></property>
</bean>
</beans>
输出结果如下: