spring4学习IOC之注入方式(三)

依赖注入注入方式

这里写图片描述

1.编写People类,PeopleFactory类和静态PeopleFactory2类

package com.newbeedaly.entity;

public class People {

    private int id;
    private String name;
    private int age;



    public People() {
        super();
        // TODO Auto-generated constructor stub
    }




    public People(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }




    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
    }



}
package com.newbeedaly.factory;

import com.newbeedaly.entity.People;

public class PeopleFactory {

    public People createPeople(){
        People p=new People();
        p.setId(5);
        p.setName("小七");
        p.setAge(77);
        return p;
    }
}
package com.newbeedaly.factory;

import com.newbeedaly.entity.People;

public class PeopleFactory2 {

    public static People createPeople(){
        People p=new People();
        p.setId(8);
        p.setName("小八");
        p.setAge(88);
        return p;
    }
}

2.编写配置文件beans.xml

<?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">

    <bean id="people" class="com.newbeedaly.entity.People"></bean>

    <bean id="people2" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
    </bean>

    <bean id="people3" class="com.newbeedaly.entity.People">
        <constructor-arg type="int" value="2"></constructor-arg>
        <constructor-arg type="String" value="李四"></constructor-arg>
        <constructor-arg type="int" value="22"></constructor-arg>
    </bean>

    <bean id="people4" class="com.newbeedaly.entity.People">
        <constructor-arg index="0" value="3"></constructor-arg>
        <constructor-arg index="1" value="王五"></constructor-arg>
        <constructor-arg index="2" value="55"></constructor-arg>
    </bean>

    <bean id="people5" class="com.newbeedaly.entity.People">
        <constructor-arg index="0" type="int" value="4"></constructor-arg>
        <constructor-arg index="1" type="String" value="招六"></constructor-arg>
        <constructor-arg index="2" type="int" value="66"></constructor-arg>
    </bean>

    <bean id="peopleFactory" class="com.newbeedaly.factory.PeopleFactory"></bean>

    <bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>

    <bean id="people8" class="com.newbeedaly.factory.PeopleFactory2" factory-method="createPeople"></bean>
</beans>
  1. 编写Test测试类
package com.newbeedaly.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.newbeedaly.entity.People;


public class Test {

    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        People people=(People)ac.getBean("people");
        System.out.println(people);

        // 属性注入
        People people2=(People)ac.getBean("people2");
        System.out.println(people2);

        // 构造方法注入
        People people3=(People)ac.getBean("people3");
        System.out.println(people3);

        People people4=(People)ac.getBean("people4");
        System.out.println(people4);

        People people5=(People)ac.getBean("people5");
        System.out.println(people5);

        // 工厂方法注入
        People people7=(People)ac.getBean("people7");
        System.out.println(people7);

        People people8=(People)ac.getBean("people8");
        System.out.println(people8);
    }
}

猜你喜欢

转载自blog.csdn.net/willdic/article/details/80536092