<?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/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "><!-- configuration bean--><bean id=" HelloWorld "class=" Com.demo.beans.HelloWorld "><property name=" name "value=" Spring "></property>< /bean></beans>
<!--config Bean
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 bean
ID: Identifies the bean,id unique in the container
-
Before the SPRINGIOC container reads the bean configuration to create the bean instance, he must be instantiated, and 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 and is a sub-interface of the Beanfactory
The former is the basic setting of the spring framework, facing spring itself;
The latter is for developers who use the spring framework, and most of the applications use the latter, rather than the former.
ApplicationContext's main implementation class:
----Classpathxmlapplicationcontext: Loading a configuration file from a class-based path
----Filesystemxmlapplicationcontext: Loading configuration files from the file system
Configurableapplicationcontext expanded to ApplicationContext, adding two main methods:
Refresh () and close (), allowing ApplicationContext to have the ability to start, refresh, and close the context
ApplicationContext All simple interest beans are instantiated when the context is initialized
Webapplicationcontext is specially prepared for Web applications, allowing initialization to be done from a path relative to the Web root directory
Dependency Injection (attribute injection, constructor injection, factory method pattern injection)
Attribute injection: is <property name= "username" value= "Jim"/>
Constructor injection:
Use the constructor to inject property values to specify the position of the parameter and the type of the parameter (which can be mixed) to differentiate the overloaded constructor.
Car.java
Package Com.demo.beans;public class Car {private string brand;private string corp;private int price;private int maxspeed;p Ublic Car (string brand, String corp, int price) {this.brand= Brand;this.corp = Corp;this.price = Price;} @Overridepublic String toString () {return this.brand+this.price;}}
<bean id= "Car" class= "Com.demo.beans.Car" ><constructor-arg value= "Audi" ></constructor-arg>< Constructor-arg value= "Shanghai" ></constructor-arg><constructor-arg value= "10000" ></ constructor-arg><!--<constructor-arg value= "10000" index= "2" ></constructor-arg>--><!-- <constructor-arg value= "10000" type= "int" ></constructor-arg>--></bean>
03-spring_ Configuration Bean