栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

Spring(bean的生命周期)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring(bean的生命周期)

目录

1.原理

2.单例多例理论

 3.单例多例的代码


1.原理

案例1:

package com.xbb.baenLife;

public class Demo1 {
	public static void main(String[] args) {
		person p = new person();
	System.out.println(p.getSex());
		
	}

}

class person{
	private String name;
	private int age;
	private String sex;
	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 getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public person(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	public person() {
		this.init();
		this.name = "bb";
		this.age = 18;
		this.sex = "哈哈";
		
		
	}
	private void init() {
		
		
	}
	
}

 效果:

 

 小总结:

1.通过三种方式将(配置文件,注解,配置类)bean标签转成beandifinition对象

2.BeanFactoryPostProcessor旅游在初始化之前修改属性值

3.BeanFactory进行bean实例化,就是生产javabean

4.Aware感知接口,能够拿到spring上下文内部的资源对象

5.BeanPostProcessor后置处理器,相对于环绕通知

2.单例多例理论

创建了四个不同的对象

 四个相同的对象:

package com.xbb.baenLife;

public class Demo1 {
	public static void main(String[] args) {
		person p1 = person.newInstance();
		person p2 = person.newInstance();
		person p3 = person.newInstance();
		person p4 = person.newInstance();
	System.out.println(p1);
	System.out.println(p2);
	System.out.println(p3);
	System.out.println(p4);
	}
}
class person{
	private person(){
		
	}
	private final static person p = new person();
	public static person newInstance() {
		return p;
	}
	
	
	
}

效果:

 spring单例模式的优点:

 3.单例多例的代码

单例与多例的区别:

 

 

scope="prototype"

总结: spring默认采用单例模式

区别:

单例模式下JAVAbean的生命周期

        容器生对象生,容器死对象死

多例模式下JAVAbean的生命周期

       使用时对象生,死亡跟jvm垃圾回收机制走

bean的初始化时间点,除了与bean管理模式(单例和多例)有关,还跟BeanFactory的子类有关

全部代码:

Demo2:

package com.xbb.baenLife;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;


public class Demo2 {
	// 体现单例与多例的区别
	@Test
	public void test1() {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
//		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
//		ParamAction p1 = (ParamAction) applicationContext.getBean("paramAction");
//		ParamAction p2 = (ParamAction) applicationContext.getBean("paramAction");
		
		
		InstanceFactory p1 = (InstanceFactory) applicationContext.getBean("InstanceFactory");
		InstanceFactory p2 = (InstanceFactory) applicationContext.getBean("InstanceFactory");
		
		
		// System.out.println(p1==p2);
//		p1.execute();
//		p2.execute();
		
//		单例时,容器销毁instanceFactory对象也销毁;多例时,容器销毁对象不一定销毁;
		applicationContext.close();
	}

	// 体现单例与多例的初始化的时间点 instanceFactory
	@Test
	public void test2() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
	}

	// BeanFactory会初始化bean对象,但会根据不同的实现子类采取不同的初始化方式
	// 默认情况下bean的初始化,单例模式立马会执行,但是此时XmlBeanFactory作为子类,单例模式下容器创建,bean依赖没有初始化,只有要获取使用bean对象才进行初始化
	@Test
	public void test3() {
		// ClassPathXmlApplicationContext applicationContext = new
		// ClassPathXmlApplicationContext("/spring-context.xml");

		Resource resource = new ClassPathResource("/spring-context.xml");
		BeanFactory beanFactory = new XmlBeanFactory(resource);
//		InstanceFactory i1 = (InstanceFactory) beanFactory.getBean("instanceFactory");
		
	}

}

InstanceFactory:

package com.xbb.baenLife;

public class InstanceFactory {
	public void init() {
		System.out.println("初始化方法");
	}

	public void destroy() {
		System.out.println("销毁方法");
	}

	public void service() {
		System.out.println("业务方法");
	}
}

ParamAction:

package com.xbb.baenLife;

import java.util.List;


public class ParamAction {
	private int age;
	private String name;
	private List hobby;
	private int num = 1;
	// private UserBiz userBiz = new UserBizImpl1();

	public ParamAction() {
		super();
	}

	public ParamAction(int age, String name, List hobby) {
		super();
		this.age = age;
		this.name = name;
		this.hobby = hobby;
	}

	public void execute() {
		// userBiz.upload();
		// userBiz = new UserBizImpl2();
		System.out.println("this.num=" + this.num++);
		System.out.println(this.name);
		System.out.println(this.age);
		System.out.println(this.hobby);
	}
}

配置:



	
	
	
	
	
	
	
	
	
		
		
		
		
		
			篮球
			boy
			rap
		
		
	
	
	
	
		
		
		
		
		
		
			篮球
			boy
			rap
		
		
		
	
	
	
	
		
		
		
			
				抽烟
				烫头
				大保健
			
		
	

	
	
	
	
	


转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1039748.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号