spring theme --- the first part of IOC (c)

On one we summarize some of the characteristics of the spring IOC, including spring bean scope, spring inheritance, dependency, and how to read external spring resources, p namespace of spring. If there is doubt friends can leave a message below or with my private letter Oh, I see will be 11 reply, our common exchanges and common progress.
In this section we summarize spring IOC factory methods to create objects and automatically load spring IOC
Section: spring IOC factory method
IOC is typical of the factory model, here we come to learn how to use the factory pattern to create the bean, the bean factory pattern to create IOC there are two ways:
static factory method
instance factory method
As always, we are still learning through the factory method with code examples, let's learn a static factory method .
(1) Create a Car class entity

public class Car {
    private int num;
    private String name;
    public Car(int num,String name){
        super();
        this.num=num;
        this.name=name;
    }
    //在此我们省略了toString方法

(2) create static factory class, static factory method

public class CarFactory {
    private static Map<Integer,Car> cars;
    static{
        cars=new HashMap<Integer,Car>();
        cars.put(1,new Car(1,"奔驰"));
        cars.put(2,new Car(2,"奥迪"));
    }
    public static Car getCar(int id){
        return cars.get(id);
    }
}

(3) disposed in a static factory spring.xml

   <bean id="car1" class="entity.CarFactory" factory-method="getCar">
        <constructor-arg value="1"></constructor-arg>
    </bean>
    <!--factory-method指向的是静态方法;constructor-arg的value属性为调用静态方法所传的参数-->

(4) obtaining a test class object car1

public class ApplicationContext {
    public static void main(String[] args){
        AbstractApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        Car car=(Car)applicationContext.getBean("car1");
        System.out.println(car);
    }
}

Run Results:
Here Insert Picture Description
instance factory method
(1) creates an instance of the class factory, factory method

public class InstanceCarFactory {
    private Map<Integer,Car> cars;
    public InstanceCarFactory(){
        cars=new HashMap<Integer, Car>();
        cars.put(1,new Car(1,"奔驰"));
        cars.put(2,new Car(2,"奥迪"));
    }
    public Car getCar(int id){
        return cars.get(id);
    }
}

(2) spring.xml bean configuration

   <bean id="carFactory" class="entity.InstanceCarFactory"></bean>
    <bean id="car2" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="2"></constructor-arg>
    </bean>

(3) obtaining a test class object car2

public class ApplicationContext {
    public static void main(String[] args){
        AbstractApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        Car car=(Car)applicationContext.getBean("car2");
        System.out.println(car);
    }
}

The result:
Here Insert Picture Description
an object created by the way the top two kinds of factories, we can see that the way to create a car an object of static factory method, without instantiating the factory object, spring.xml we can see, we only configured Car bean , no factory bean. Because static methods static factory does not need to create an object can be called.
The factory instance created Car object, we must first instantiating the factory object, because non-static method call, you must call the object through the factory and can not be invoked through the class. So spring.xml configured first in the factory bean, then configure Car bean.
Briefly, the static factory method is a factory class for static methods, examples of the way the plant is a non-static method in the factory class.
Section II: spring IOC auto-loading
automatic loading and in fact somewhat similar dependency injection, we learned front dependency injection, containing the class A category B, then we can ref property is injected into the class A category B. What is it automatic loading? A class is simply included in Classes B, we can use in the bean autowire automatically loaded into the B class A category.
There are two important attributes which autowire values are byName and byType, here we come through some simple examples explain the use of these two property values:
byName:

        <!--spring.xml-->>
        <!--设置了bean标签autowire属性,属性值设为byName,在这里IOC容器能通通过id值将bean进行匹配,然后创建对象由API调用-->>
        <bean id="stu" class="entity.Student" autowire="byName">
            <property name="id" value="1"></property>
            <property name="name" value="jacob"></property>
            <property name="age" value="18"></property>
        </bean>
        <bean id="classes" class="entity.Classes">
            <property name="class_name" value="jacob_classes"></property>
            <property name="class_id" value="1001"></property>
        </bean>
//Student.class
public class Student {
    private int id;
    private String name;
    private int age;
    private Classes classes;
//Classes.class
public class Classes {
    private int class_id;
    private String class_name;
//测试类
public class ApplicationContext {
    public static void main(String[] args){
        AbstractApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        Student student=(Student)applicationContext.getBean("stu");
        System.out.println(student);
    }
}

The result:
Here Insert Picture Description
We can see that we add in the id of the bean in the stu autowire, and its value is set to byName, the resulting object student in Classes property value what we value assigned to the id attribute classes, indicating stu id of the bean is matched to the classes and implementation of automatic loading by byName (id value of bean).
Then we look at autowire another value byType:
we can use it and get the object API way to put together memory, I do not know little friends still remember application is to obtain the bean through what ways, one is the id a run-time class, in fact, automatic loading and it is very similar.

        <!--spring.xml-->>
        <!--bean的autowire值设置为byType,IOC通过Student中属性Classes(entity.Classes)和bean中其他class匹配-->>
        <bean id="stu" class="entity.Student" autowire="byType">
            <property name="id" value="1"></property>
            <property name="name" value="jacob"></property>
            <property name="age" value="18"></property>
        </bean>
        <bean id="classes" class="entity.Classes">
            <property name="class_name" value="jacob_classes"></property>
            <property name="class_id" value="1001"></property>
        </bean>

The result:
Here Insert Picture Description
If I set two bean in spring.xml, they are like the bean Classes, we will look at the results:
Here Insert Picture Description
we have seen, the program error, the reason is in the xml configuration, we configure two identical class of the bean, it also proves that the property is to be matched by byType class.
Summary:
In this section we summarize the spring IOC created by the factory schema objects, including static factory methods and instance factory mode, in which objects are created static factory way, we just need to create the configuration object bean in spring.xml because static factory class are static methods, can be directly used to call class, and examples of ways to create an object factory need to configure the factory bean class and you need to create an object of class bean. Then we explain another IOC characteristics, auto-loading, it dependency injection is very similar, except that it requires disposed bean tag header, there are two attribute values, respectively, and the byType byName, matching the id former, the latter category by matching operation, it should be noted that, when using byType, spring.xml arranged only in one kind of bean runtime class, a plurality of program error.
The above is my understanding of this part, if there is an error or you have any other questions can be given a message, I will respond one by one, I hope for your help, if badly written but also ask you to bear with me. Welcome to add comments below yo.
SSM framework for interested children's shoes, be the venue here , where you can quickly build a good framework for SSM.
If you write when the project encountered some unusual problems, can focus on my blog park , will be released on top of some of the problems I encountered in writing projects and solutions.

Released seven original articles · won praise 4 · Views 256

Guess you like

Origin blog.csdn.net/llllxxxxyyyy/article/details/104073338