Design Pattern-4.1 spring getting started spring Bean Factory

Source: Internet
Author: User

We mentioned spring Bean factory when talking about the factory model. Here is a detailed description:

 

Http://wenku.baidu.com/view/69f94e80d0d233d4b14e6970.html? St = 1 download in Word format: Download in Word format:

Spring bean containers and bean factories, also known as IOC (inverseof control), are interface-oriented and abstract-oriented.

 

Getting started with spring:

The moveable interface is also available:

PackageCom. bjsxt. Spring. factory;

 

PublicInterfaceMoveable {

VoidRun ();

}

 

Of course, there are also implementations.MoveableSubclass of the interface:

CarAndTrain

 

PackageCom. bjsxt. Spring. factory;

 

 

PublicClassCarImplementsMoveable {

PublicVoidRun (){

System.Out. Println ("Running in smokeCar .......");

}

}

 

PackageCom. bjsxt. Spring. factory;

 

PublicClassTrainImplementsMoveable {

 

@ Override

PublicVoidRun (){

System.Out. Println ("Whining");

}

}

 

The key is in test (User:

PackageCom. bjsxt. Spring. factory;

 

ImportJava. Io. ioexception;

 

ImportOrg. springframework. Beans. Factory. beanfactory;

ImportOrg. springframework. Context. Support. classpathxmlapplicationcontext;

 

//We used to usePropertiesFile to read information,Obtain the name of the class. Then, we get the object of this class through reflection.

PublicClassTest {

 

/**

 *@ ParamARGs

 *@ ThrowsIoexception

 */

PublicStaticVoidMain (string [] ARGs)Throws
Exception {

//PassXMLFile to read information

//BecauseXMLTherefore, the reading method is different.

Beanfactory F =NewClasspathxmlapplicationcontext ("Applicationcontext. xml");

Object o = f. getbean ("V");

Moveable M = (moveable) O;

M. Run ();

}

 

}

 

 

Let's take a look at the information in the XML file:

 

<?XMLVersion="1.0"Encoding=UTF-8"?>

<BeansXmlns=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-3.0.xsd">

 

<BeanID="V"Class="Com. bjsxt. Spring. Factory. Train">

</Bean>

<! --// V = com. bjsxt. Spring. Factory. Car -->

 

 

</Beans>

 

It's easy.Bean, ItsIDIsVAnd then itsClassYesCom. bjsxt. Spring. Factory. Train

 

Download the Word document in this article:

Http://wenku.baidu.com/view/69f94e80d0d233d4b14e6970.html? St = 1

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.