spring IOC容器

application.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">

 <!--   配置类名,用于反向创建一个person对象
    同时给一个编号方便查找-->
    <bean id="date1" class="java.util.Date"/>
    <bean id="person" class="com.lp.domain.Person"/>
    <bean id="person2" class="com.lp.domain.Person">
        <property name="id" value="10"/>
        <property name="name" value="rose"/>
        <property name="age" value="20"/>
        <property name="date" ref="date1"/>
    </bean>

    <bean id="person3" class="com.lp.domain.Person">
        <constructor-arg name="id" value="10"/>
        <constructor-arg name="name" value="tony"/>
        <constructor-arg name="age" value="20"/>
        <constructor-arg name="date" ref="date1"/>
    </bean>
<!--    静态工厂 factory-method="getBean"  getBean()为静态方法 -->
    <bean id="person4" factory-method="getBean" class="com.lp.domain.PersonFactory"/>

<!--    PersonFactory2 factory2 = new PersonFactory2(); 创建工厂

        Person person3= factory2.getBean(); 调用工厂方法-->
    <bean class="com.lp.domain.PersonFactory2" id="factory2"/>
<!--    实例工厂 factory-bean="factory2" factory-method="getBean"   getBean()为成员方法-->
    <bean factory-bean="factory2" factory-method="getBean" id="person5"/>
</beans>

Test01SpringIOC

package com.lp.demo01;

import com.lp.domain.Person;
import com.lp.domain.PersonFactory2;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01SpringIoc {

    @Test
    public void test01(){
        //1:创建容器对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2:给定配置文件名称 applicationContext.xml
        //3:调用容器的getBean方法获取id对应的对象
        Person person = (Person) context.getBean("person");
        Person person2 = context.getBean("person",Person.class);
        System.out.println(person);
        System.out.println(person2);
    }
    @Test
    public void test02(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person2 = context.getBean("person2",Person.class);
        System.out.println(person2);
    }
    @Test
    public void test03(){
        //1:创建容器对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2:给定配置文件名称 applicationContext.xml
        //3:调用容器的getBean方法获取id对应的对象
        Person person3= context.getBean("person3",Person.class);
        System.out.println(person3);
    }
    @Test
    public void test04(){
        //1:创建容器对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2:给定配置文件名称 applicationContext.xml
        //3:调用容器的getBean方法获取id对应的对象
        Person person4= context.getBean("person4",Person.class);
        System.out.println(person4);
    }

    @Test
    public void test05(){
        //1:创建容器对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2:给定配置文件名称 applicationContext.xml
        //PersonFactory2 factory2 = new PersonFactory2();

        //Person person3= factory2.getBean();
        Person person3 = context.getBean("person5",Person.class);
        System.out.println(person3);
    }
}

PersonFactory

package com.lp.domain;

public class PersonFactory {

    private static Person getBean(){
        return new Person();
    }

}

PersonFactory2

package com.lp.domain;

public class PersonFactory2 {

    public Person getBean(){
        return new Person();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值