Spring Food memory Configuration Bean

Source: Internet
Author: User

"Bean Configuration"

To configure a bean in an XML file through a bean node

1 <!-- 2 Configuration Bean3 the full class name of the Class:bean, which creates the bean in the IOC container in a reflective manner, requires that the bean must have an argument-free constructor. 4 ID: Identifies the bean,id unique in the container. 5  -6    <BeanID= "HelloWorld"class= "Com.hk.beans.HelloWorld">7        < Propertyname= "Name"value= "Spring"></ Property>8    </Bean>

The name of the Id:bean.

-Must be unique within the IOC container.

-If the ID is not specified, spring automatically assigns the corresponding class name to the Bean.

--id can specify more than one name, separated by commas, semicolons, or spaces.

"Note":

When there is no parameterless constructor in the bean and there is a parameter constructor, the following error occurs when you run the program:

"Spring Container"

--applicationcontext represents a container, but it is actually an interface.

--Before the Spring IOC container reads the bean configuration to create the bean instance, it must be instantiated. You can get the bean instance from the IOC container and use it only after the container is instantiated.

--spring provides two types of IOC container implementations.

(1) The basic realization of the BEANFACTORY:IOC container.

(2)ApplicationContext: Provides more advanced features. is the sub-interface of the beanfactory.

Beanfactory is the infrastructure of the spring framework, facing spring itself;

ApplicationContext for developers using the spring framework, almost all applications use ApplicationContext rather than the underlying beanfactory.

The configuration files are the same regardless of the way you use them.

"ApplicationContext"

The main implementation class for 1.ApplicationContext:

--Classpathxmlapplicationcontext: Loads the configuration file from the classpath.

--filesystemxmlapplicationcontext: Loads the configuration file from the file system.

2.ConfigurableApplicationContext (sub-interface) extended to ApplicationContext, adding two new main methods: Refresh () and Close (), Lets ApplicationContext have the ability to start, refresh, and close the context.

3.ApplicationContext instantiates all singleton beans when the context is initialized.

4.WebApplicationContext is intended for Web applications and allows initialization to be done from a path relative to the Web root directory.

"Get beans from IOC container"

1. Call the Getbean () method of ApplicationContext. The method is defined in the parent interface beanfactory of ApplicationContext.

It is known from the figure that the bean can also be obtained by type.

1 New Classpathxmlapplicationcontext ("Applicationcontext.xml"); 2 HelloWorld HelloWorld = Ctx.getbean (HelloWorld.  Class

Disadvantages!! : If this type is not unique in an IOC container (there are two or more beans of the same type), an error occurs.

---summary:

1 // 2. Get the bean instance from the IOC container 2// mode one: Use ID to locate the bean in the IOC container3//  HelloWorld HelloWorld = (HelloWorld) ctx.getbean ("HelloWorld"); 4 // Mode Two: Returns the Bean in the IOC container using the type, but requires that only one bean of that type must be in the IOC container 5 HelloWorld HelloWorld = Ctx.getbean (HelloWorld.  Class

"The way of Dependency injection"

Spring supports 3 types of dependency injection:

-- attribute input

-- constructor injection

--Factory mode (rarely used, not recommended)

1. Attribute Injection

(1) Overview: Attribute injection is the object that is injected into the bean's property value or since the setter method .

(2) attribute injection using the <property> element, use the Name property to specify the Bean's property name, the Value property, or the <value> child node to specify the property value.

(3) attribute injection is the most commonly used injection method in practical application .

1    <  ID= "HelloWorld"  class= "Com.hk.beans.HelloWorld">2         <name= "name"  value= "Spring" ></  Property > 3    </ Bean >

2. Construction Method Injection

(1) by constructing a method to inject the Bean's property value or dependent object, it guarantees that the bean instance can be used after instantiation.

(2) Constructor injection declares the attribute in the <constructor-arg> element,There is no Name attribute in <constructor-arg> .

Car.java:

1  PackageCom.hk.beans;2 3  Public classCar {4     PrivateString brand;5     PrivateString Corp;6     Private DoublePrice ;7     Private intmaxspeed;8     9      PublicCar (string brand, String Corp,DoublePrice ) {Ten         Super(); One          This. Brand =brand; A          This. Corp =Corp; -          This. Price =Price ; -     } the  - @Override -      PublicString toString () { -         return"Car [brand=" + Brand + ", corp=" + corp + ", price=" + Price ++ ", maxspeed=" + Maxspeed + "]"; -     } +  A      PublicCar (string brand, String Corp,intmaxspeed) { at         Super(); -          This. Brand =brand; -          This. Corp =Corp; -          This. Maxspeed =maxspeed; -     } -}

Configuration file Code:

1 <!--to configure a Bean's properties by constructing a method -2    <BeanID= "Car"class= "Com.hk.beans.Car">3       <Constructor-argvalue= "BWM"Index= "0"></Constructor-arg>4       <Constructor-argvalue= "Shanghai"Index= "1"></Constructor-arg>5       <Constructor-argvalue= "100000"type= "Double"></Constructor-arg>6    </Bean>7    8    <!--use the constructor to inject property values to specify the position of the parameter and the type of the parameter! To differentiate the overloaded constructors!  -9    <BeanID= "Car2"class= "Com.hk.beans.Car">Ten       <Constructor-argvalue= "Bieke"type= "Java.lang.String"></Constructor-arg> One       <Constructor-argvalue= "Beijing"type= "Java.lang.String"></Constructor-arg> A       <Constructor-argvalue= "Max"type= "int"></Constructor-arg> -    </Bean>

(test) Main.java:

1         New Classpathxmlapplicationcontext ("Applicationcontext.xml"); 2         3         Car car = (car) ctx.getbean ("Car"); 4         System.out.println (car); 5         6         Car = (car) ctx.getbean ("Car2"); 7         SYSTEM.OUT.PRINTLN (car);

Operation result;

Spring Food memory Configuration Bean

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.