What is Spring?
Spring is a framework;
Spring is a container frame;
Spring is used to manage beans (Java classes) and to maintain the relationships between beans (Java classes);
Container framework: The entire web belongs to spring to manage, spring can manage the web layer, the business layer, the DAO persistence layer, each layer is composed of Java class (bean), spring can manage all the beans, and maintain the relationship between the beans;
The core mechanism of spring
Bean:
Can be any object in Java, either javabean/service/servlet/dao,spring or through the management of each layer of beans, to implement the management of the entire project.
Spring can manage the instantiation of beans, the birth of objects;
Spring can manage the relationship of objects (object properties in a Class);
Ioc/di:
IOC (inverse of control): Inversion of controls
When spring is not used, all objects are new, and we decide when new, and control is in our own hands;
In spring, all objects are managed by spring, and spring determines when it is created, and control is on spring's hands;
DI (Dependency Injection): Dependency Injection
For example: the relationship between service layer and DAO layer in Web engineering:
Service relies on object properties in DAO when not using Spring;
In spring, the DAO object is injected into the service by spring, which also embodies the relationship between Spring Management Service and DAO;
AOP (Aspect onented programming): Aspect-oriented programming, programming for all objects;
Spring Starter Case
Now there are two of these classes:
To implement spring management, the steps to maintain the bean:
1, the introduction of jar package;
2, configure the core profile: Applicationcontext.xml
3. Write Test class
Attention:
The ID in the core configuration file corresponds to the object name of the created object;
Use the Getbean (ID) method in the core class to get the Bean object created by spring;
When you configure object properties, you need to use the ref attribute, which corresponds to the ID of the corresponding Bean object configuration file, such as:
Summary: If we compare the history of the program to society, it can be divided into:
Primitive Society: Self-sufficiency, need the object, on their own new object;
Industrial society: Factories, need objects, created by factories, to find factories to;
Communist Society: Spring Containers, containers contain a large number of configured objects, need not be active to find, spring will rely on injection;
Spring's execution mechanism
When the application configuration file is read, the spring frame is loaded;
When the bean's lifecycle is not considered, the bean configured in spring is created when the application configuration file is read;
When reading a configuration file, the bean is instantiated and placed in a spring container as a key-value pair
Spring's underlying Java-based reflection mechanism
Read configuration file: IO
Service = Class.forName ("Bean,userservice"). newinstance (); Reflection, establishing an instance
Byeservice = Class.forName ("Bean,byeservice"). Newintance (); Reflection, establishing an instance
Service.setsyaservice (Byeservice); Establishment of relationships between beans/injection of attributes
Hashmap.put ("service", service); Hashmap.put ("Byeservice", Byeservice); Put the object into the HashMap, as the spring container, the factory;
PS: Due to the limited ability of the author, such as wrong, please understand;
SSM Application (i)--spring Getting Started