Use of Spring IOC configuration (i)

Source: Internet
Author: User

This article mainly accounts for the rapid understanding and use of spring-related configuration, so it is necessary to understand the principles of spring and to learn or to supplement them if they are simply described or not explained in terms of frequently used configuration items.
The spring version used in this example is spring-4.0.0.m2, and the preparation environment operates as follows:
First, create a Java Project project and import the required spring jar. This example is used to include:
/springlearn/lib/spring-aop-4.0.0.m2.jar
/springlearn/lib/spring-beans-4.0.0.m2.jar
/springlearn/lib/spring-context-4.0.0.m2.jar
/springlearn/lib/spring-core-4.0.0.m2.jar
/springlearn/lib/spring-expression-4.0.0.m2.jar
/springlearn/lib/spring-instrument-4.0.0.m2.jar
Also included are the external jar files referenced by spring.
/springlearn/lib/commons-logging-1.1.3.jar
Second, create the main configuration file beans.xml under SRC, the content is as follows:

<?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/ Beanshttp://www.springframework.org/schema/beans/spring-beans.xsd "><!--learn bean named  Import the relevant spring configuration file--><import resource= "Com/mahaochen/spring/learn01/beanlearn01.xml"/></beans>

"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "

Basic configuration of Spring beans

The basic structure of the XML configuration is as follows:

<?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/ Beanshttp://www.springframework.org/schema/beans/spring-beans.xsd "><bean id=" Spearker01 "class=" Com.mahaochen.spring.learn01.Speaker "scope=" Singleton "/></beans>

ID: called "identifier", which indicates that the corresponding Bean,id value must be unique;
Class: Used to indicate that the corresponding class object needs to be instantiated;
Scope: Used to indicate the scope of the instance.


Naming of Spring Beans

Operation Steps:
1. Create a Speaker object.

public class Speaker {/** * speech */public Void speech () {System.out.println ("The Speaker starts to speech!");}}


2. Create a spring configuration file, Beanlearn01.xml.

<?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/ Beanshttp://www.springframework.org/schema/beans/spring-beans.xsd "><!--learn bean named--><!--scope: Singleton a bean definition corresponds to an object instance in each spring IOC container. Prototype a bean definition corresponds to multiple object instances. Request in an HTTP request, a bean definition corresponds to an instance, that is, each HTTP request will have its own bean instance, which is created from a bean definition. This scope is valid only in the case of a web-based spring ApplicationContext. Session in an HTTP session, a bean definition corresponds to an instance. This scope is valid only in the case of a web-based spring ApplicationContext. --><bean id= "Spearker01" class= "Com.mahaochen.spring.learn01.Speaker" scope= "Singleton"/></beans>

3. Introduce the spring configuration file beanlearn01.xml into the master profile Beans.xml.

<?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/ Beanshttp://www.springframework.org/schema/beans/spring-beans.xsd "><!--learn bean named  -->< Import resource= "Com/mahaochen/spring/learn01/beanlearn01.xml"/></beans>

4, write test class Testspring01.java.

public class TestSpring01 {public static void main (string[] args) {beanfactory beanfactory = new Classpathxmlapplicationco ntext ("beans.xml"); get Bean "way One" by id Speaker Speaker01 = (Speaker) beanfactory.getbean ("Spearker01"); Speaker01.speech ();//Get Bean by id "way Two" Speaker Speaker02 = Beanfactory.getbean ("Spearker01", Speaker.class); Speaker02.speech (); System.out.println ("Bean's scope is configured to Scope=\" singleton\ ": Two speaker is the same object reference"); System.out.println ("\" Speaker Speaker01 = Speaker speaker02?\ "  " + (Speaker01==speaker02));}}

Instantiating a bean using a constructor


Operation Steps:
1. Create a Speaker object.

public class Speaker {private String name;p rivate string topic;private int timehour;/** * Empty constructor */public Speaker () {}/** * with parameter constructor * @param name * @param topic * @param timehour */public Speaker (string name, string topic, int timehour) {THIS.N Ame = Name;this.topic = Topic;this.timehour = Timehour;} /** * Lecture */public void speech () {System.out.println (toString ());} @Overridepublic String toString () {return "Speaker [name=" + name + ", topic=" + topic + ", timehour=" + Timehour + "]";}}


2. Create a spring configuration file, Beanlearn02.xml.

<?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/ Beanshttp://www.springframework.org/schema/beans/spring-beans.xsd "><!--learn 02 using the constructor to instantiate the bean--><!-- Construct parameters with parameters--<bean id= "Speaker02" class= "Com.mahaochen.spring.learn02.Speaker" ><!--specifying constructor parameters  -- <constructor-arg index= "0" value= "make"/><constructor-arg index= "1" value= "Enjoy your lift!" /><constructor-arg index= "2" value= "1" ></constructor-arg></bean></beans>

3. Introduce the spring configuration file beanlearn02.xml into the master profile Beans.xml.

<?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/ Beanshttp://www.springframework.org/schema/beans/spring-beans.xsd "><!--learn  using the constructor to instantiate the bean-- <import resource= "Com/mahaochen/spring/learn02/beanlearn02.xml"/></beans>

4, write test class Testspring02.java.

public class TestSpring02 {public static void main (string[] args) {beanfactory beanfactory = new Classpathxmlapplicationco ntext ("beans.xml");//Use the constructor to instantiate Beanspeaker Speaker = Beanfactory.getbean ("Speaker02", Speaker.class); Speaker.speech ( ); System.out.println ("success!");}}




"Reproduced use, please specify the Source: Http://blog.csdn.net/mahoking"

Use of Spring IOC configuration (i)

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.