Spring Learning Journey (iv) a further exploration of spring's working principle

Source: Internet
Author: User

The last blog post on the working principle of spring made a general introduction, want to see the students please go left. Say a few details today.

(i) Spring IOC container and its instantiation and use

The Spring IOC container is responsible for the instantiation, configuration, and assembly of Beans with two interfaces: Beanfactory and ApplicationContext. ApplicationContext, which inherits from Beanfactory, provides more support for enterprise-level application development. This interface is used in real-world applications.

1) Instantiate the spring container (there are four main types)

1.classpathxmlapplicationcontext: Look for the configuration XML file instantiation container under the classpath.

New Classpathxmlapplicationcontext ("Hellobean.xml");

2.filesystemxmlapplicationcontext: Look for the configuration file under File system path to instantiate the container.

ApplicationContext act=New filesystemxmlapplicationcontext ("D:/beans.xml");

3.Xmlwebapplicationcontext: Instantiate the container from the XML configuration file in the Web App directory Web-inf. (Small series failed to achieve success, please achieve successful students advice)

New Xmlwebapplicationcontext ();

4. In the Web. XML configuration file, instantiate the container by configuring the listener. (Assuming that the spring configuration file is already configured)

The spring configuration file can specify multiple, separated by commas.

// get the Web server container in a Web page by using the request object or by other means  ServletContext sc=request.getservletcontext (); // use the static methods provided by the spring framework to get the spring container  from the Web server Webapplicationcontext Wact=webapplicationcontextutils.getrequiredwebapplicationcontext (SC);

2) generating a bean instance

The spring container obtains the managed object from the container through the Getbean () method.

For example:

Hellobeans student= (Hellobeans) Wctx.getbean ("stu1");

(ii) Bean configuration based on XML file mode

The formation of beans in a Java container is called Assembly.

There are two types of bean assembly: XML file-based and annotation-based.

The XML file-based approach is to configure the bean information with an XML file. There are two main parts: the namespace, the bean, and the configuration of the information.

4 Ways to configure beans:

Example: Defining a two entity class

 Public classAddress {PrivateString City; PrivateString School; //no parameter constructor     PublicAddress () { This. city= "Taian";  This. school= "Nongda"; }    //a parametric constructor     PublicAddress (String city,string school) { This. city=City ;  This. school=School; }    //omitted the Setter/getter method}
 Public classStudent {PrivateString name; Private intAge ;    Address address; //default constructor     PublicStudent () {}//a parametric constructor     PublicAddress (String name,intage,address Address) {         This. name=name;  This. age=Age ;  This. address=address; }    //omitted the Setter/getter method}

The 1th configuration method is injected with a constructor with parameters:

<Beanname= "A1"class= "Com.edu.bean.Address"><Constructor-argIndex= "0"type= "Java.lang.String"value= "Beijing"/><Constructor-argIndex= "1"type= "Java.lang.String"value= "Tsinghua"/></Bean>

The 2nd configuration method uses the parameterless constructor to inject:

<name= "A2"  class= "com.edu.bean.Address"/> 

The 3rd configuration method, using the property setter method to inject:

<Beanname= "A3"class= "Com.edu.bean.Address">< Propertyname= "City"value= "Beijing"></ Property>< Propertyname= "School"value= "Tsinghua"></ Property></Bean>

The 4th configuration method uses the property's setter method to inject the reference property:

<Beanname= "Addr"class= "Com.edu.bean.Address">< Propertyname= "City"value= "Beijing"></ Property>< Propertyname= "School"value= "Tsinghua"></ Property></Bean><Beanname= "ss"class= "Com.edu.bean.Student">< Propertyname= "Name"value= "Zhang San"></ Property>< Propertyname= "Age"value= " the"></ Property>< Propertyname= "Address"ref= "Addr"></ Property></Bean>

(iii) Spring Expressions--spel (Spring expression lanuage)

Use "#{...}" As delimiters. All the characters in the curly braces will be considered spel. Spel provides the convenience for dynamic assignment of bean properties.

The following example each set of two statements is an equivalent representation:

< Propertyname= "Count"value= "#{5}"></ Property>< Propertyname= "Count"value= "5"></ Property>< Propertyname= "Address"value= "#{addr}"></ Property>< Propertyname= "Address"ref= "Addr"></ Property>< Propertyname= "Address"value= "#{addr.city}"></ Property>

(iv) annotation-based bean configuration

Spring Learning Journey (iv) a further exploration of spring's working principle

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.