xml方式创建bean

public abstract class Animal {
    abstract String getName();
}
public class AnimalFactory {
    public static Animal getAnimal(String type){
        if ("dog".equals(type)){
            return new Dog();
        }else {
            return new Cat();
        }
    }
}
public class AnimalFactory2 {
    public Animal getAnimal(String type){
        if ("dog".equals(type)){
            return new Dog();
        }else {
            return new Cat();
        }
    }
}
public class Cat extends Animal {
    @Override
    String getName() {
        return "cat";
    }
}
public class IocService {
    private Animal animal;

    public Animal getAnimal() {
        return animal;
    }

    public void setAnimal(Animal animal) {
        this.animal = animal;
    }

    public String getName(){
        return animal.getName();
    }
}

public class Student {
    private String name;
    private int age;
    private List<String> list;

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    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 "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", list=" + list +
                '}';
    }
}

public class Student2 {
    private String name;
    private int age;

    public Student2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private List<String> list;

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    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 "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", list=" + list +
                '}';
    }
}

ioc/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="student" class="cn.fly.springbootdemo.ioc.Student">
        <property name="name" value="老王"/>
        <property name="age" value="30"/>
        <property name="list">
            <list>
                <value>tom</value>
                <value>sam</value>
            </list>
        </property>
    </bean>
    <!--有参构造-->
    <bean id="student2" class="cn.fly.springbootdemo.ioc.Student2">
        <constructor-arg index="0" value="老王2"/>
        <constructor-arg index="1" value="32"/>
        <property name="list">
            <list>
                <value>tom</value>
                <value>sam</value>
            </list>
        </property>
    </bean>
    <!--静态工厂构造-->
    <bean id="iocService" class="cn.fly.springbootdemo.ioc.IocService">
<!--        <property name="animal" ref="cat"/>-->
        <property name="animal" ref="dog2"/>
    </bean>
    <bean id="cat" class="cn.fly.springbootdemo.ioc.AnimalFactory"
        factory-method="getAnimal">
        <constructor-arg index="0" value="cat"/>
    </bean>
    <bean id="dog" class="cn.fly.springbootdemo.ioc.AnimalFactory"
          factory-method="getAnimal">
        <constructor-arg value="dog"/>
    </bean>
    <!--实例工厂构造-->
    <bean id="animalFactory" class="cn.fly.springbootdemo.ioc.AnimalFactory2"/>
    <bean id="cat2" factory-bean="animalFactory"  factory-method="getAnimal">
        <constructor-arg value="cat"/>
    </bean>
    <bean id="dog2" factory-bean="animalFactory"  factory-method="getAnimal">
        <constructor-arg value="dog"/>
    </bean>
</beans>

<!--
优点:
    低耦合
    对象关系清晰
    集中管理
缺点:
    配置繁琐
    开发效率稍低
    文件解析耗时
-->

测试

@SpringBootTest
@ContextConfiguration("classpath:ioc/beans.xml")
public class BeansTest {
    @Autowired
    private Student student;

    @Autowired
    private Student2 student2;

    @Autowired
    private IocService iocService;

    @Test
    public void test(){
        System.out.println(student.toString());
    }
    @Test
    public void test2(){
        System.out.println(student2.toString());
    }
    @Test
    public void test3(){
        System.out.println(iocService.getName());
    }
}

猜你喜欢

转载自www.cnblogs.com/fly-book/p/12691702.html