1. Ioc&di Overview
IOC (Inversion of Control): The idea is to reverse the direction of resource acquisition. The traditional resource lookup direction requires the component to initiate a request to the container to find the resource, in response to the container's timely return of the resource.
When the IOC is applied, it is the container that actively pushes the resource to the component it manages, and the component only chooses an appropriate way to accept the resource. Also called passive form of the lookup.
Another way to express DI (Dependency injection)--IOC is that the component accepts resource injections from the container in some pre-defined form (for example, setter methods).
2, the bean configuration
Beans are configured in two ways, through setter, and by constructing methods.
(1) by means of setter
<!--class     : Full category name    ID: Unique setting for identity bean the    name value is spring, and the SetName method    must be added to the HelloWorld class class= "Com.study.wjy.HelloWorld" >    <property name= "name" value= "Spring" ></property> </bean>
View Code(2) By way of construction method
<!-- using constructors to create a bean instance     index represents the subscript position of the constructor, starting at 0 The    type represents the types of the construction arguments    index and type can use only one class = "Com.study.wjy01.Car" >    <constructor-arg value= "Baoma" index= "0" ></constructor-arg>    <constructor-arg value= "Shanghai" index= "1" ></constructor-arg>    <constructor-arg value= " 300000 "type=" double "></constructor-arg></bean>
View Code3. Automatic Bean Assembly
Automatic Bean assembly Method: ByName, Bytype, default, constructor, No.
ByName: Assemble the bean ID according to the Object name field
Bytype: Automatic assembly According to object type, throws an exception when there are two types
 <!-- person class properties:  PR                    Ivate   String name;                     private   Address addr;         private   car car; When using ByName, automatic assembly ID  =addr and id=car beans with Bytype, automatically assemble class as address and car bean --> <bean id= "addr" class  = "com.study.demo.Address" p:city= "Beijing" P: street= "Changanjie" ></bean> <bean id= "car" class  = " Com.study.demo.Car "P:name=" Audi "p:price=" 200000 "></bean> <bean id=" person "class  =" Com.study.demo.Person "autowire=" ByName "></BEAN> 
View Code
Note: There can be only one type of object set for automatic assembly, byname and Bytype cannot be set at the same time
4. Relationship between beans (1) Bean inheritance
<!--Bean is not instantiated when it is defined as an abstract bean by abstraction (so it is not possible to define the class attribute), only acting as a child bean with parent inheritance configuration--    <bean id= "Address"  Abstract= "true" ></bean>    class= "Com.study.demo.Address"  p:city= "Beijing" p:street= "Hankou" parent= "Address" ></bean>
View Code(2) Bean dependency
    The <!-- Bean's dependency        p tag is similar     to the shorthand function--    class= "Com.study.demo.Car" p:name= "Audi" p:price= " 300000 "></bean>    class=" Com.study.demo.Person "p:name=" Dawei "    p:addr-ref=" Address2 "depends-on=" Car "></bean>
View Code5, the scope of the bean (scope)
The scope types of beans are: prototype, request, session, Singleton
Singleton: Default value. The container creates a bean instantiation at initialization, creating only one bean throughout the life cycle, so it is a singleton
Prototype: the prototype. The container does not create a bean instantiation at initialization time, and is instantiated every time the bean is fetched.
<bean id= "Car" class= "Com.study.demo.Car" scope= "singleton" ></bean>public class Main {public static void Main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontext ("Bean-scope.xml"); Car car = (car) ctx.getbean ("Car"); Car car2 = (car) ctx.getbean ("Car"); System.out.println (car = = CAR2);                   
6. Bean Reference External File example
db.property file contents: User=Root Password=12390Driverclass=Com.mysql.jdbc.Driver Jdbcurl=MYSQL:JDBC:///testBean-properties.xml file Contents:<context:property-placeholder location= "Classpath:db.property"/> <bean id= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "User" value= "${user}" ></property> <property name= "Password" value= "${password}" ></property> <property name= "Driverclass" value= "${d Riverclass} "></property> <property name=" Jdbcurl "value=" ${jdbcurl} "></property> </bean& GtMain method Output: Public classMain { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Bean-properties.xml"); DataSource DataSource= (DataSource) ctx.getbean ("DataSource");        System.out.println (DataSource); }    }View Code7. Application of Spel expression of bean
<bean id= "Address"class= "Com.study.demo04.Address" > <!--!--<property name= "City" value= "Beijing" by direct definition and direct write consistency--><  ></property> <property name= "City" value= "#{' Beijing '}" ></property> <property Name= "Street" value= "Wudaokou" ></property> </bean> <bean id= "Car"class= "Com.study.demo04.Car" > <property name= "name" value= "Audi" ></property> <property name= "PR Ice "value=" #{3333} "></property> <!--to refer to the static value of the class by T (), with the +-*/etc operation--<property name=" Tyrpe Rimiter "value=" #{t (Java.lang.Math). PI * Ten} "></property> </bean> <bean id=" Person "class= "Com.study.demo04.Person" > <property name= "name" value= "Tom" ></property> <!--by judging the value of the assignment--&G        T <property name= "Info" value= "#{car.price > 30000? ' Golden collar ': ' White Collar ' ></property> <!--reference Bean object by ID--<property name= "addr" value= "#{address}" > </property> <property name= "car" value= "#{car}" ></property> </bean>View Code
  
  
One, Spring's first lesson