Spring框架-02-01-Spring的Bean的生命周期,组装方式,作用域


在这里插入图片描述


Spring IoC容器概述

  • Spring容器即体现了IoC原理
  • Spring容器通过读取配置元数据负责对Beans实例化、配置和装配
  • 配置元数据可以用XML、Java注解或Java代码来描述

在这里插入图片描述

Spring容器会读取配置文件,放置对象到容器里面,自动帮你生成对象。

在这里插入图片描述

BeanFactory

在这里插入图片描述
帮你把用的对象实例化,用的是反射技术。
BeanFactory实际上也是工厂模式。

课上例子……
在这里插入图片描述

ApplicationContext

在这里插入图片描述

是BeanFactory的子接口,比BeanFactory功能更多。
是它的超级。

有四个实现类。(ApplicationContext的实现)

  • ClassPathXmlApplicationContext
    基于XML文件的配置,放置在文件根目录下
  • FileSystemXmlApplicationContext
    XML文件放操作系统的路径下(C:/ D:什么的)
  • XmlWebApplicationContext
    基于WEB程序的配置
  • AnnotationConfigApplicationContext
    完全注解方式

在这里插入图片描述
超集=提供更多的东西


Bean概述

在这里插入图片描述

构造方法实例化

在这里插入图片描述

项目结构

在这里插入图片描述

Desk.java

package com;

public class Desk {
    
    
	private static Desk desk;
	
	public Desk() {
    
    
		System.out.println("Desk构造方法");
	}
	
	public static Desk createInstance() {
    
    
		System.out.println("createInstance");
		if(desk==null)
			desk = new Desk();
		return desk;
	}
}

Test.java

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    

	public static void main(String[] args) {
    
    
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		Desk desk = (Desk)ctx.getBean("Desk");
		
		
	}
}

运行结果

在这里插入图片描述

静态工厂方式实例化

在这里插入图片描述

在构造方法实例化的基础上做修改即可

修改Bean.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    增加Factory-method属性即可
     <bean id="Desk" class="com.Desk" factory-method="createInstance"/>
</beans>


实例工厂实例化

在这里插入图片描述

项目结构

在这里插入图片描述

Chair.java

package com;

public class Chair {
    
    
	public Chair() {
    
    
		System.out.println("Chair无参数构造");
	}
}

ChairFactory.java

package com;

public class ChairFactory {
    
    
	public Chair create() {
    
    
		return new Chair();
	}
}

Desk.java

package com;

public class Desk {
    
    
	private static Desk desk;
	
	public Desk() {
    
    
		System.out.println("Desk构造方法");
	}
	
	public static Desk createInstance() {
    
    
		System.out.println("createInstance");
		if(desk==null)
			desk = new Desk();
		return desk;
	}
}

Test.java

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    

	public static void main(String[] args) {
    
    
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");		
		Chair chair=(Chair)ctx.getBean("Chair");
		
	}
}

Beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
     <bean id="Desk" class="com.Desk" factory-method="createInstance"/>
     
     <bean id="ChairFactory" class="com.ChairFactory"></bean>
     	<!-- factory-bean的属性值是上面的id值,不是类名 -->
     	<!-- factory-methid的属性值是工厂类的方法名 -->
     <bean id="Chair" factory-bean="ChairFactory" factory-method="create"></bean>          
     
</beans>


运行结果

在这里插入图片描述




Bean的生命周期

在这里插入图片描述

在这里插入图片描述

实现Aware接口 与 使用自定义方法配置

就是系统的方法与 用户自定义的方法
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

项目结构

在这里插入图片描述

BeanLife.java

package com;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


//实现接口
public class BeanLife implements ApplicationContextAware, InitializingBean, DisposableBean{
    
    
	private ApplicationContext ctx;
	private String name;
	public void setName(String name) {
    
    
		this.name = name;
		System.out.println("setName属性的赋值");
	}
	public BeanLife() {
    
    
		System.out.println("BeanLife的无参构造方法");
	}
	
	public void show() {
    
    
		System.out.println(name);
	}
	
	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
    
    
		ctx = arg0;//让自己的类可以用传来的ApplicationContext参数
		System.out.println("applicationAware");
	}
	@Override
	public void destroy() throws Exception {
    
    
		System.out.println("destroy");
		
	}
	@Override
	public void afterPropertiesSet() throws Exception {
    
    
		System.out.println("afterPropertiesSet");
		
	}
	

	public void init1() {
    
    
		System.out.println("init1");
	}
	

	public void destroy1() {
    
    
		System.out.println("destroy1");
	}

}


Test.java

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    

	public static void main(String[] args) {
    
    
//		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//		BeanLife bl = (BeanLife)ctx.getBean("BeanLife");
//		bl.show();
		
		
		
		
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		BeanLife bl = (BeanLife)ctx.getBean("BeanLife");
		bl.show();
		ctx.close();//关闭的方法在实现类里面,不再容器里面
		
	}
}

Beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
       
     <bean id="BeanLife" class="com.BeanLife" init-method="init1" destroy-method="destroy1">
        <property name="name" value="ZhangSan"></property>
    </bean>     
</beans>

运行结果

在这里插入图片描述

使用注解方式配置

和上面相比,只是Bean.xml文件里面bean属性删掉,换成了注解形式,并且在BeanFactory里面加上了部分注解

项目结构

在这里插入图片描述

BeanLife

package com;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


//实现接口
@Component("BeanLife")
public class BeanLife implements ApplicationContextAware, InitializingBean, DisposableBean{
    
    
	private ApplicationContext ctx;
	private String name;
	public void setName(String name) {
    
    
		this.name = name;
		System.out.println("setName属性的赋值");
	}
	public BeanLife() {
    
    
		System.out.println("BeanLife的无参构造方法");
	}
	
	public void show() {
    
    
		System.out.println(name);
	}
	
	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
    
    
		ctx = arg0;//让自己的类可以用传来的ApplicationContext参数
		System.out.println("applicationAware");
	}
	@Override
	public void destroy() throws Exception {
    
    
		System.out.println("destroy");
		
	}
	@Override
	public void afterPropertiesSet() throws Exception {
    
    
		System.out.println("afterPropertiesSet");
		
	}
	
	@PostConstruct
	public void init1() {
    
    
		System.out.println("init1");
	}
	
	@PreDestroy
	public void destroy1() {
    
    
		System.out.println("destroy1");
	}

}


Beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
       
    <context:annotation-config />
    <context:component-scan base-package="com">
    </context:component-scan>  
</beans>

Bean的作用域

在这里插入图片描述
在这里插入图片描述

配置singleton属性

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44627608/article/details/114826848