Often listen to the teacher say the container, what the container is. How it's reflected in spring. Have been puzzled, these two days to see the Spring Management Bean demo, the container in spring has a simple understanding.
We know that a container is a space concept that is generally understood as a place where objects can be filled. In spring containers are generally understood as beanfactory or ApplicationContext. We know that spring's IOC container can help us create objects, and we don't have to manually go to the new object after the object is given to spring management.
What is the difference between beanfactory and ApplicationContext?
Beanfactory employs a factory design pattern that reads bean configuration documents, manages bean loading, instantiates, maintains dependencies between beans, and is responsible for the bean's declaration cycle. In addition to providing the functionality provided by the above Beanfactory, ApplicationContext provides a more complete framework function: Internationalization support, AOP, transactions, etc. At the same time Beanfactory does not initialize the object when parsing the configuration file, the object is initialized only with Object Getbean (), and ApplicationContext initializes all objects in the configuration file when the configuration file is parsed, Getbean ( ) method is just the process of getting an object.
So we usually use applicationcontext when we use it.
ApplicationContext is how to manage beans. The following demo is a simple imitation of this principle:
1. Create a class Personservicebean and configure it in the XML file.
[Java] view plain copy public class Personservicebean implements Personservice {public void Save () { System.out.println ("I Am the Save () method"); }} [HTML] view plain copy <bean id= "Personservice" class= "Cn.itcast.service.impl.PersonServiceBean" &G T;</bean>
2. Create a class beandefinition that provides a constructor that acts as a public transformation class for each bean.
[Java] View Plain copy public class beandefinition { private String id; private String className; public beandefinition (string id, string classname) { this.id = id; this.className = className; } public string getid () { return id; } public void setid (string id) { this.id = id; } Public string getclassname () { return classname; } Public void setclassname (string classname) { this.className = className; } }