"Spring Framework Principle"
Spring Framework Principles
"Bo Master" Gao Ruilin
"Blog Address" http://www.cnblogs.com/grl214
------Dear Readers Thank you for your support for the small part, when I am young, I am very happy to meet a group of you in the mood for love. When happiness is blooming like a flower, please allow me to remember in the flowering season always follow my you thank you for being on the road! Let's go into the world of Java with joy!
- The concept of the spring framework
- Spring in the IOC
- Spring Framework Principles
- The difference between beanfactory and Appilcationcontext
I. The concept of the spring framework |
Spring is an open source container framework that takes over the components of the Web layer, the business layer, the DAO layer, the persistence layer, and can configure various beans and maintain the relationship between beans and beans. At its core is the inversion of control (IOC), and aspect-oriented (AOP), which is simply a layered, lightweight, open-source framework.
IOC: (Full name: Inverse of control) controls inversion, the container proactively pushes the resource to the component it manages, and the component chooses a reasonable way to accept the resource.
Simple understanding: The right to create the relationship between objects and maintenance is transferred from the program to the spring container's configuration file.
DI: (Full name: Dependency injection) Dependency injection, another manifestation of the IOC, a component that accepts a container-injected resource in a pre-defined way.
Three. Spring Framework principle |
1. Example:
Build a Person class
1 public class person {2 3 private String name, 4 5 private int age ; 6 7 Private Book book; 8 9 public String GetName () {Ten return }12 public void setName (String name) {This.nam E = }16 getage () { 2 return}20 public void Setage (int. ) {this.age = }24 5 Public book GetBook () {}28- setbook (book book ) {this.book = book;31}
Build a book Class
1 public class book {2 3 private String name, 4 5 private int price ; 6 7 Private String Place; 8 9 public String GetName () {Ten return }12 public void setName (String name) {This.nam E = }16-public int GetPrice () { }20] public void Setprice (int. price ) {This.price =
}24 public
String getplace () {setplace return}28-public void this.place (String-place) 9>
Applicationcontext.xml 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 "><!-- Bean configuration file--><bean id= "person" class= "Org.jingdong.bean.life.Person" ><property name= "name" value= "GRL" ></property>
<property name= "Age" value= "one" ></property>
<property name= "book" ref= "book" ></property>
</bean>
<bean id= "book" class= "Org.jingdong.bean.life.Book" ><property name= "name" Value= "Think in Java" ></ Property>
<property name= "Place" value= "USA" ></property>
<property name= "Price" value= "></property>"
</beans>
Main.java
1 public class main {2 public static void main (string[] args) {3 //Create IOC container 4 applicationcontext AC = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); 5 //Get bean instance from container 6 person person = ( person) Ac.getbean ("person"); 7 }10}
2. Operating Principle Analysis:
(1). When ApplicationContext ac = new Classpathxmlapplicationcontext ("ApplicationContext. xml"), when executed, the Spring container object is created, At the same time, the beans in ApplicationContext. Xml are created into memory:
(2) Operation schematic diagram:
Four. The difference between beanfactory and ApplicationContext |
How to configure the Bean:
(1). Through the ApplicationContext context container: When the XML configuration file is loaded, the configured bean in the configuration file has been instantiated
(2). Beanfactory: When loading a configuration file, the bean in the configuration file is not instantiated and is created only when the bean instance is obtained through Getbean ().
Summary: The beans configured through Beanfactory are more memory-efficient than those configured through ApplicationContext.
--------------------------------------Thanks for watching------------------------------------------------
Spring Framework Principles