Spring XML Configuration Way Notes

Source: Internet
Author: User

1. What is Spring

The spring framework is an open-source, lightweight enterprise application development framework designed to reduce intrusion, and the spring Framework provides a holistic solution where developers can choose to use the solution provided by spring or to integrate with a third-party framework.

2. The essence of Spring is: managing objects in software, how objects are created, and maintaining relationships between objects.

3. Core of Spring: AOP and IOC (inversion of control). That is, tangent-oriented programming and control inversion. Can reduce the coupling between components, that is, decoupling, easy to maintain the system later.

IOC: The principle of: Refers to the object in the program to get the method of inversion, previously generated objects through new, now by the third-party framework to create (or inject) objects, the third-party framework generally by injecting a configuration file to specify which implementation to inject, thereby reducing the coupling between the objects, The spring framework implements IOC control using DI (Dependency injection), which relies on injection. The IOC is a kind of thought, and Di is the main way to realize this idea.

DI consists of two injection methods,setter injection and constructor injection

4. instantiation of the spring container (in two ways)

4.1: Loading system files for instantiation

String confi= "C:\application.xml"; // resource path under System path ApplicationContext ac=New filesystemxmlapplicationcontext (CONFI); // loading a configuration file to get the spring context object

4.2: Instantiate the configuration file under load Classpath

String confi= "Applicationcontext.xml"; // files under the classpath ApplicationContext ac=New classpathxmlapplicationcontext (CONFI);

5. Spring instantiates a bean in three ways

For example, there is a JavaBean object person, under Com.fonghuaxi

 import   Com.fonghuaxi;  public  class   person{ private   String username;  private   String password;  private  int   age;  public  static   person getinstance () { return  new   person ();}  //   ...}  

5.1: Constructor instantiation

<id= "Person"  class= "Com.fonghuaxi.Person"></ the bean>//spring container invokes the person's default constructor to instantiate and generate the object. 

5.2: Instantiation of a static factory

<id= "Person1"  class= "Com.fonghuaxi.Person " Factory-method= "getinstance"></bean>

5.3: Instantiation of instance methods

 <  bean  id  = "Person1"   class  = "Com.fonghuaxi.Person"  ></ bean  >  <  bea  id  = "Person2"   Factory-bean  = "Person1"   Factory-method  = "getinstance"  ></ bean  >   

6. Scope of the bean

Scope of the Bean

Singleton In each of the spring IOC containers, a bean definition corresponds to an object instance, and the default
Prototype A bean definition corresponds to multiple instances
Request In an HTTP request, a bean definition corresponds to an instance, only in a Web environment
Session In a httpsession, a bean definition corresponds to an instance, only in a Web environment
Globalsession Interested can understand their own, using the same as the session

7. The life cycle callback method of the bean

7.1 Specifying the init-method= "Init" initialization callback on the bean

7.2 The callback is specified on the bean definition destroy-method= "destory" destroys the bean, only works under Singleton

7.3 Specify initialization callback default-init-method= "Init" (Specify initialization for all beans) on top beans, specify default-destory-method= "Destory" (Specify destroy callback for all beans).

8.method of deferred instantiation of beans

Specify lazy-init= "True" on the bean to delay the instantiation of the bean, specifying default-lzay-init= under the top beans "true to specify deferred initialization for all beans

9. the spring IOC setter injection

For example, the person class is under Com.fongzhongxi

class person{private  String username; Private String Password;setter and Getter Methods // The setter method is provided for the container to automatically inject the corresponding property value ...  Public Person (String username,password) {    this. username=username;      this. password=password;          }}

Setter injection:

<BeanID= "Person1"class= "Com.fongzhongxi.Person">< Propertyname= "username"value= "Zhang San" />//Note that the property of name username is username consistent with the attribute specified in person, otherwise it cannot be injected< Propertyname= "Password"value= "123"/></Bean>

Constructor injection:

<id= "Person2"  class= "Com.fongzhongxi.Person"> <  index= "0"  ref= "Person1"/>// Note the ref reference Setter's ID is injected into the Person1 property </bean>

10. Automatic assembly of Spring IOC

Spring XML Configuration Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.