文章目录
自定义初始化 销毁方法
意义
当我们需要一些特定操作的时候,比如某dao类,需要在构造以后去获取数据库的链接资源,在销毁时释放这些资源,所以我们就需要自定义初始化和销毁方法
编辑实体类
public class Cat {
public Cat() {
super();
System.out.println("cat 对象开始实例化");
}
public void run() {
System.out.println("喵喵喵!!!");
}
public void init() {
System.out.println("cat类自定义的init方法开始执行");
}
public void destory() {
System.out.println("cat类自定义的destory方法开始执行");
}
}
编辑xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!--
bean的定义
形式如下
<bean id = "定义的id值" class = "类的路径"></bean>
-->
<!--
自定义一个bean对象的初始化与销毁方法:
-->
<bean id = "zjjCat" class = "cn.springLearn.day1.Cat" init-method="init" destroy-method="destory"></bean>
</beans>
init-method:属性的值就是该bean类被初始化时调用的方法名
destroy-method:属性的值就是该bean类被销毁时调用的方法名
编辑测试类
public class UserDemoTest {
@Test
public void test() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Cat cat = (Cat)ac.getBean("zjjCat");
cat.run();
ac.destroy();
}
}
运行结果
单例模式
单例模式定义
单例模式,是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中,应用该模式的一个类只有一个实例。即一个类只有一个对象实例。
在spring中我们按照这种模式在xml中配置的bean,在使用时默认采用了单例模式
编写测试类
public class UserDemoTest {
@Test
public void test() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
User user = (User)ac.getBean("zjjUser");
User user2 = (User)ac.getBean("zjjUser");
System.out.println(user == user2);
}
}
运行结果
多例模式
多例模式定义
多例模式就时我们平时使用的new对象的用法,一个类会有多个对象
编写实体类
public class Dog {
public Dog() {
super();
System.out.println("dog 对象开始实例化");
}
public void run() {
System.out.println("汪汪汪!!!");
}
public void init() {
System.out.println("dog类自定义的init方法开始执行");
}
public void destory() {
System.out.println("dog类自定义的destory方法开始执行");
}
}
编辑xml配置文件
在xml中我们在编写bean的时候需要加上一个属性 scope
scope:有两种值
singleton(默认):单例模式,整个生命周期只会有一个对象存在
prototype:多例模式,整个生命周期只会有多个对象存在
<bean id = "zjjDog" class = "cn.springLearn.day1.Dog" scope="prototype"></bean>
编写测试类
public class UserDemoTest {
@Test
public void test() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Dog dog = (Dog)ac.getBean("zjjDog");
Dog dog2 = (Dog)ac.getBean("zjjDog");
System.out.println(dog);
System.out.println(dog == dog2);
}
}
运行结果
懒加载
懒加载定义
当容器创建时就将对象也一起创建,就是非懒加载
当我们需要使用该对象才加载的时候,就是懒加载
懒加载优点
节省资源
懒加载缺点
bug不容易暴露,需要到使用的时候才会发现要使用的类有问题
ps:单例模式默认非懒加载
多例模式为懒加载
编辑xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean id = "zjjUser" class = "cn.springLearn.day1.User"></bean>
<!--
懒加载(lazy-init):
取值有三个 default ,true ,false
定义:当容器创建时就将对象也一起创建,就是非懒加载
当我们需要使用改对象才加载的时候,就是懒加载
优点:节省资源
缺点:bug不容易暴露,需要到使用的时候才会发现要使用的类有问题
ps:单例模式默认非懒加载
多例模式为懒加载
-->
<bean id = "zjjChicken" class = "cn.springLearn.day1.Chicken" lazy-init="true" init-method="init" destroy-method="destory"></bean>
</beans>
编辑测试方法
public class UserDemoTest {
@Test
public void test() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//Chicken ck = (Chicken) ac.getBean("zjjChicken");
}
}
测试结果
当我们将测试类的注释取消掉以后
证明了,我们定义的uer对象因为是单例模式,所以在容器初始化阶段就已经被实例话出来了,但是我们的chicken对象只有当我们去使用它的时候它才被实例化出来,这就是懒加载