Get the two methods of the Bean 1. Gets 2 from the ApplicationContext application context container. Get the Bean's difference from the Bean factory use ApplicationContext to get the bean example We define a student class and let spring call it Student.javapackage com.getbean; /*** Created by admin on 2017/12/9.*/public class Student {private String name;private int age;private i NT Id; public String getName () {return name;} public void SetName (String name) {this.name = name; System.out.print (name);} public int Getage () {return age;} public void setage (int age) {this.age = age;} public int getId () {return ID;} public void setId (int id) {this.id = id;}} //add student class to spring config file, <bean id= "student" class= "com.getBean.Student" ><property name= "name" > <value> fat yo </value></property><property name= "age" ><value>1</value></ Property><property name= "id" ><value>1</value></property></bean> // Call Beanpackage Com.getbean;import via ApplicationContext org.springframework.beans.factory.xml. Xmlbeanfactory;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.beans.factory.beanfactory;import org.springframework.core.io.classpathresource; /*** Created by admin on 2017/12/9.* describes two ways to get beans differently than they do * 1. Gets bean* 2 from the ApplicationContext container context. Get Bean*/public class Rungetbean {public static void main (string[] args) {///1, get ApplicationContext via bean//from Beanfactory When we call new Classpathxmlapplicationcontext the Bean in the spring configuration file is instantiated the bean's scope is the ApplicationContext app for the singleton single instance = New Classpathxmlapplicationcontext ("Com/getbean/beans.xml"), App.getbean ("student"); }} this way to get the bean is created All bean 2 in the configuration file are automatically instantiated when the instance is ApplicationContext. Use the Bean factory to get bean package com.getbean;import Org.springframework.beans.factory.xml.xmlbeanfactory;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import ORG.SPRIngframework.beans.factory.beanfactory;import org.springframework.core.io.classpathresource; /*** Created by Admin on 2017/12/9.* describes two ways to get beans differently than they do * 1. Gets bean* 2 from the ApplicationContext container context. Get Bean*/public class Rungetbean {public static void main (string[] args) {///1, get ApplicationContext via bean//from Beanfactory When we call new Classpathxmlapplicationcontext the Bean in the spring configuration file is instantiated the bean's scope is a single instance of//ApplicationContext app = new Classpathxmlapplicationcontext ("Com/getbean/beans.xml");//App.getbean ("student");//through the Bean factory to get beanbeanfactory Factory = new Xmlbeanfactory (New Classpathresource ("Com/getbean/beans.xml"); Factory.getbean ("Student");}} When you use the Bean factory only if you call the Getbean method, the bean instance is created in most of the project development, the bean is obtained using the ApplicationContext method, because it can be loaded in advance, Faster and in mobile devices most use the Bean factory because it saves memory summary: If the bean configured with ApplicationContext is singleton regardless of whether you use it, it will be instantiated, the advantage is that it can be preloaded, The downside is a waste of memory. If you use Beanfactory to configure the bean will not be instantiated immediately, when you use the time will be instantiated, the advantage is to save memory generally if there is no special requirements, you should use applicationcontext
Bean Factory and ApplicationContext