Spring Framework-02-01-Spring's Bean life cycle, assembly method, scope


Insert picture description here


Overview of Spring IoC container

  • The Spring container embodies the principle of IoC
  • The Spring container is responsible for instantiating, configuring and assembling Beans by reading configuration metadata
  • Configuration metadata can be described in XML, Java annotations or Java code

Insert picture description here

The Spring container will read the configuration file, place the object in the container, and automatically generate the object for you.

Insert picture description here

BeanFactory

Insert picture description here
To help you instantiate the objects you use, reflection technology is used.
BeanFactory is actually a factory model.

Examples in class...
Insert picture description here

ApplicationContext

Insert picture description here

It is a sub-interface of BeanFactory and has more functions than BeanFactory.
It's super.

There are four implementation classes. (Implementation of ApplicationContext)

  • ClassPathXmlApplicationContext is
    based on the configuration of the XML file, placed in the root directory of the file
  • FileSystemXmlApplicationContext
    XML file is placed in the path of the operating system (C: / D: what)
  • XmlWebApplicationContext
    based on WEB program configuration
  • AnnotationConfigApplicationContext
    fully annotated

Insert picture description here
Superset = provide more things


Bean overview

Insert picture description here

Constructor instantiation

Insert picture description here

Project structure

Insert picture description here

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");
		
		
	}
}

operation result

Insert picture description here

Static factory method instantiation

Insert picture description here

Modifications can be made on the basis of the instantiation of the construction method

Modify the Bean.xml file

<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>


Instance factory instantiation

Insert picture description here

Project structure

Insert picture description here

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>


operation result

Insert picture description here




Bean's life cycle

Insert picture description here

Insert picture description here

Implement Aware interface and use custom method configuration

Is the systematic method and the user-defined method
Insert picture description here
Insert picture description here

Insert picture description here

Project structure

Insert picture description here

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>

operation result

Insert picture description here

Configure using annotations

Compared with the above, only the bean properties in the Bean.xml file are deleted, replaced by annotations, and some annotations are added to the BeanFactory

Project structure

Insert picture description here

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 scope

Insert picture description here
Insert picture description here

Configure singleton properties

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/114826848