天天看点

SpringBoot基础-XML方式注册beanxml方式注册Bean的实现方式XML方式注册Bean的优缺点

xml方式注册Bean的实现方式

1.无参构造函数

  • Animal 类,后续样例类的父类
public abstract class Animal {

    /**
     * @return
     */
    public abstract String get();
}
           
  • Dog 类
@Getter
@Setter
public class Dog extends Animal {

    private String name;

    private Integer age;

    private List<String> hobby;

	public Dog() {
    }
    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String get() {
        return this.toString();
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", hobby=" + hobby +
                '}';
    }
}
           
  • 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="dog" class="com.demo.silly.spring.boot.ioc.xml.Dog">
        <property name="name" value="旺财"/>
        <property name="age" value="13"/>
        <property name="hobby">
            <list>
                <value>吃饭</value>
                <value>睡觉</value>
            </list>
        </property>
    </bean>
</beans>
           
  • 测试代码

    在测试代码启动前需要先加载配置的xml

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(locations = "classpath:ioc/animal.xml")
public class IocTest {

    @Autowired
    private Animal dog;

    @Test
    public void get() {
        System.out.println(dog.get());
    }
}
           

2.有参构造

  • 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="dog" class="com.demo.silly.spring.boot.ioc.xml.Dog">
        <constructor-arg name="name" value="小黄"/>
        <constructor-arg name="age" value="12"/>
        <property name="hobby">
            <list>
                <value>吃饭</value>
                <value>睡觉</value>
            </list>
        </property>
    </bean>
</beans>
           

其他代码逻辑与1.无参构造函数 函数一样

3.静态工厂方法

  • AnimalFactory 工厂方法编写
public class AnimalFactory {

    public static Animal getAnimal(String type) {
        if ("dog".equals(type)) {
            return new Dog();
        }
        return null;
    }
}
           
  • 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="dog" class="com.demo.silly.spring.boot.ioc.xml.AnimalFactory"
          factory-method="getAnimal">
        <constructor-arg value="dog"/>
    </bean>
</beans>
           

Animal 、Dog、测试代码同上

4.实例工厂方法

  • AnimalFactory工厂方法将static去除
public class AnimalFactory {

    public  Animal getAnimal(String type) {
        if ("dog".equals(type)) {
            return new Dog();
        }
        return null;
    }
}
           
  • 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 name="animalFactory" class="com.demo.silly.spring.boot.ioc.xml.AnimalFactory"></bean>

    <bean id="dog" factory-bean="animalFactory"
          factory-method="getAnimal">
        <constructor-arg value="dog"/>
    </bean>
</beans>
           

Animal 、Dog、测试代码同上

XML方式注册Bean的优缺点

优点

  • 低耦合

    不需要在引入中去实例化对象,由容器来注入完成对应的实现

  • 对象关系清晰

    可以通过xml文件一目了然知道哪些类存在对应的依赖关系

  • 集中管理

    都配置在xml中

缺点

  • 配置繁琐
  • 开发效率稍低
  • 文件解析耗时