Spring Basics and Bean configuration

Source: Internet
Author: User

IOC and DI:

IOC (Inversion of control): The idea is to reverse the direction of resource acquisition. The traditional resource lookup method requires that the component initiate a request lookup resource to the container. In response, the container returns resources in a timely manner.
When the IOC is applied, it is the container that proactively pushes the resources to the components it manages, and what the component does is to choose an appropriate way to receive resources. This approach is also known as the passive form of lookups.
DI (Dependency injection): Another form of IOC representation that a component accepts a resource injection from a container in a pre-defined way, such as a setter method. This statement is more straightforward than the IOC.

Spring Container:
It must be instantiated before the spring IOC container reads the bean configuration to create the bean instance. The bean instance can be obtained from the IOC container and used only after the container is instantiated.
Spring provides two types of IOC container implementations.
Basic implementation of-BEANFACTORY:IOC container
-applicationcontext: Provides more advanced features. It's Beanfactory's sub-interface.
-beanfactory is the spring framework infrastructure for spring itself; ApplicationContext for developers using the spring framework, In almost all applications, the applicationcontext is used directly rather than the underlying beanfactory

-The configuration file is the same regardless of the way you use it.

ApplicationContext Inheritance structure diagram


ApplicationContext's main implementation class:
-classpathxmlapplicationcontext: Loading a configuration file from a Classpath
-filesystemxmlapplicationcontext: Loading configuration files from the file system
Configurableapplicationcontext is extended to ApplicationContext, adding two main methods: Refresh () and close (), allowing ApplicationContext to start, The ability to refresh and close the context.
ApplicationContext instantiates all singleton beans when initializing the context
Webapplicationcontext is intended for Web applications and allows initialization to be done from a path relative to the Web root directory.

Methods of injecting attributes:
Spring supports 3 kinds of dependency injection methods
-Attribute Injection
-Constructor Injection
-factory method injection (rarely used, not recommended)

Property injection is a setter method that injects the bean's property values or dependent objects
Attribute injection uses the <property> element, using the Name property to specify the Bean's property name, the Value property, or the <value> node to specify the property value
Attribute injection is the most commonly used injection method in practical applications.

1 <!--Configuration Bean2 the full class name of the Class:bean, which creates the bean in the IOC container in a reflective manner, so a constructor with no arguments is required in the bean3 ID: Identifies the bean,id unique in the container. 4  -5 <BeanID= "Hellospring"class= "Com.yl.HelloSpring">6 < Propertyname= "Name"value= "Spring"></ Property>7 </Bean>

Construct Method Injection
By constructing a method to inject the Bean's property value or dependent object, it ensures that the bean instance is instantiated and can be used
Constructor injection declares attributes in the <constructor-arg> element

1     <!--use the constructor to inject property values to specify the position of the parameter and the type of the parameter to differentiate the overloaded constructor -2     <BeanID= "Car"class= "Com.yl.Car">3         <Constructor-argvalue= "Audi"Index= "0"></Constructor-arg>4         <Constructor-argvalue= "Shanghai"Index= "1"></Constructor-arg>5         <Constructor-argvalue= "300000"type= "Double"></Constructor-arg>6     </Bean>7     8     <BeanID= "Car2"class= "Com.yl.Car">9         <Constructor-argvalue= "BMW"type= "Java.lang.String"></Constructor-arg>Ten         <Constructor-argvalue= "Shanghai"type= "Java.lang.String"></Constructor-arg> One         <Constructor-argvalue= "$"type= "int"></Constructor-arg> A     </Bean>

The code for the car class used in the configuration file:

1 Package Com.yl;2 3 Public class Car {4     5 private String brand;6 private String origin;7 private double price;8 private int speed;9 Public Car (string brand, String origin, double price) {Ten super (); One This.brand = brand; A This.origin = origin; - this.price = Price; -     } the Public Car (string brand, string origin, int speed) { - super (); - This.brand = brand; - This.origin = origin; + this.speed = speed; -     } + @Override A Public String toString () { at return "Car [brand=" + Brand + ", origin=" + Origin + ", price=" - + price + ", speed=" + Speed + "]"; -     } -      -      -}

Test code:

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);

Output:

Car [Brand=audi, Origin=shanghai, price=300000.0, speed=0]car [Brand=bmw, Origin=shanghai, price=0.0, speed =200]

Spring Basics and Bean configuration

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.