Spring foundation: the difference and connection between BeanFactory and ApplicationContext and spring management objects

contact:

The ApplicationContext interface inherits from BeanFactory and has more functions;

the difference:

BeanFactory defaults to lazy loading (the object is created when it is acquired) The
ApplicationContext default is urgently loaded (the object is created when the container is created), but the urgent loading is changed to lazy loading,
method one: configure a single bean to lazy load in In the spring configuration file, add lazy-init = "true" to the bean properties.

<bean id="tomato" class="cn.it.show.Tomato" lazy-init="true"></bean>

Method 2: Configure all beans to be lazy loaded, add default-lazy-init = "true" after the declaration and constraints of the spring configuration file;

<?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" default-lazy-init="true">

Spring management objects

  • Prerequisites: There must be no parameter structure.
  • Manage external beans: manage our own classes
	<!-- 管理外部类 -->
	<bean id="tomato" class="cn.it.show.Tomato" lazy-init="true"></bean>
  • Manage internal Beans: manage classes written by others, API comes with class
    For example: get the current system time-"former way: new Date ()
    Now Spring management:
<!-- 管理内部类 -->
	<bean id="data" class="java.util.Date"></bean>

You can get the current time directly in the test method

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Date bean = ac.getBean("data",java.util.Date.class);
		System.out.println(bean);//Thu Apr 16 15:25:41 CST 2020
Published 23 original articles · received 1 · views 165

Guess you like

Origin blog.csdn.net/weixin_45528650/article/details/105557669