装配Bean基于XML

装配Bean 基于XML

1.实例化方式

3种bean的实例化方式:默认构造、静态工厂、实例工厂。

1).默认构造

<bean id="" class="">  必须提供默认构造

2).静态工厂
常用与spring整合其他框架(工具)
静态工厂:用于生成实例对象,所有的方法必须是static

<bean id=""  class="工厂全限定类名"  factory-method="静态方法">

下面我们举例说明:
要被创建的类:

package com.fly.spring.factory;

public class Teacher {

    private String score;

    public void getScore() {
        System.out.println("*****Hello score*****" + score);
    }

    public void setScore(String score) {
        this.score = score;
    }

}

静态工厂类:

package com.fly.spring.factory;

/**
 * 静态工厂创建实例
 * @author Administrator
 *
 */
public class TestStaticBeanFactory {

    /**
     * 创建对象出来
     */
    public static Teacher createTeacher(){
        return new Teacher();
    }
}

配置文件:applicationContext.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">

       <!-- 将静态工厂创建的实例交予spring 
        class 确定静态工厂全限定类名
        factory-method 确定静态方法名
    -->
       <!-- 创建TestStaticBeanFactory实例 -->
       <bean id="testStaticBeanFactory" 
             class="com.fly.spring.factory.TestStaticBeanFactory" 
             factory-method="createTeacher">
       </bean>


</beans>

测试的类:

package com.fly.spring.factory;

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

public class TestDemo {

    @Test  
    public void testDemo1(){
        String xmlPath = "com/fly/spring/factory/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        Teacher bean = applicationContext.getBean("testStaticBeanFactory", Teacher.class);
        bean.getScore();
    }

}

打印结果
这里写图片描述

3).实例工厂
实例工厂:必须先有工厂实例对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。

需要被实例的类

package com.fly.spring.factory_instance;

public class Cat {

    public void getString(){
        System.out.println("*************实例工厂**************");
    }
}

实例工厂:

package com.fly.spring.factory_instance;

/**
 * 实例工厂   所有方法非静态
 * @author Administrator
 *
 */
public class TestBeanFactory {

    /**
     * 创建对象出来
     */
    public Cat createCat(){
        return new Cat();
    }
}

配置文件applicationContext.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">

       <!-- 将静态工厂创建的实例交予spring 
        class 确定静态工厂全限定类名
        factory-method 确定静态方法名-->
       <!-- 创建TestStaticBeanFactory实例 -->
       <bean id="testStaticBeanFactory" 
             class="com.fly.spring.factory_instance.TestBeanFactory" >
       </bean>

       <bean id="cat" factory-bean="testStaticBeanFactory" factory-method="createCat"></bean>


</beans>

测试代码:

package com.fly.spring.factory_instance;

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

public class TestDemo {

    @Test  
    public void testDemo1(){
        String xmlPath = "com/fly/spring/factory_instance/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        Cat bean = applicationContext.getBean("cat", Cat.class);
        bean.getString();
    }

}

补充一个错误的答案:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值