spring1 applicationContext.xml文件7 testCase测试i方法

本文详细介绍了Spring中XML配置文件的使用,包括通过构造器、静态工厂、实例工厂创建对象,设置bean的作用域,管理bean的生命周期,以及依赖注入的setter和构造器方式。此外,还展示了如何在JUnit测试类中加载和使用这些配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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-2.0.xsd">
    <!-- spring创建对象 -->
    <!-- 1.通过构造器创建 class属性:要创建的对象的类型 id:别名, -->
    <bean class="java.util.ArrayList" id="obj1">
    </bean>
    <!-- 2.通过静态方法工厂创建对象 调用某类的静态方法来创建对象 -->
    <bean class="java.util.Calendar" factory-method="getInstance"
        id="obj2"></bean>

    <!-- 3.通过实例工厂创建对象 调用某对象的非静态方法来创建对象 factory-bean:要调用的对象的名称(id就是名称) 此处必须引用的是容器中已经声明过的对象 
        factory-method:要调用的方法 注意:所创建的对象的类型是方法返回的类型。 -->
    <bean factory-bean="obj2" factory-method="getTime" id="obj3"></bean>
    <!-- bean的作用域 singleton:Spring创建的某个类型的bean在容器中 默认只有一个实例即单例 prototype:spring创建的某个类型的bean在容器中 
        有多个实例,非单例 加上属性scope,即可改成创建多个实例 -->

    <bean class="java.util.HashMap" id="obj4" scope="prototype" ></bean>


    <!-- 管理bean的生命周期 让spring自动调用该bean的初始化及销毁方法 通过另外两个属性调用初始化,销毁方法 init-method:声明初始化方法,在容器创建bean之后自动调用 
        destroy-method:声明销毁方法,spring容器关闭时,自动调用销毁只对单例的 bean有效,非单例的bean无效,加scope属性,表示非单例 
        lazy-init延迟初始化,在容器创建时,并不会创建bean,而是在获取bean时才创建 将bean创建的时机延迟了,只对单例的bean有效 适用于低频率使用的对象 -->
    <bean class="bean.Example" id="exa" init-method="init"
        destroy-method="destroy" scope="singleton" lazy-init="true"></bean>


    <!-- Spring IOC 是由依赖注入(DI)实现的 包括两种注入方式:setter注入,构造器注入 -->

    <!-- 1.setter注入:通过bean的set方法,给他注入参数 -->
    <bean class="bean.Computer" id="computer">
        <property name="mainboard" value="技嘉" />
        <property name="hdd" value="希捷"></property>
        <property name="ram" value="金士顿" />
    </bean>

    <!-- 2.构造器注入,通过bean的构造器,给它注入参数,这种注入比setter更严格 往往是为了强制注入这些参数。 -->
    <bean class="bean.MobilePhone" id="phone">
        <constructor-arg index="0" value="ARM"></constructor-arg>
        <constructor-arg index="1" value="2G" />
    </bean>

    <!-- 自动装配;Spring在创建bean时,可以根据类型名称,从容器中找到匹配的bean 设置给这个bean的属性 -->
    <bean class="bean.Student" id="student" autowire="byType"></bean>


    <!-- 参数值注入!! -->
    <bean class="bean.Message" id="msg">
        <!-- 注入基本值,基本类型,封装类型,String类型 -->
        <property name="id" value="1"></property>
        <property name="name" value="ward"></property>
        <property name="salary" value="5000"></property>
        <!-- 注入bean对象 name:要注入的属性的名称 ref:是另外注入的bean的id -->
        <property name="phone" ref="phone"></property>
        <!-- 注入集合:List,Set,Map,Properties -->
        <property name="cities">
            <list>
                <value>北京</value>
                <value>上海</value>
                <value>广州</value>
            </list>
        </property>
        <!-- 给Map注入值 -->
        <property name="score">
            <map>
                <entry key="jsd1604" value="80" />
                <entry key="jsd1605" value="60" />
            </map>
        </property>
        
        <property name="params">
            <props>
                <prop key="user">openlab</prop>
                <prop key="pwd">open123</prop>
            </props>
        </property>

    </bean>

 

</beans>  

------------

-------------------------------------------------------------------------------

package test;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

import bean.Computer;
import bean.Example;
import bean.Message;
import bean.MobilePhone;
import bean.Student;

public class TestCase {
    
    /**
     * Junit专门用来测试,方法前必须加@Test才能独立运行
     * 选中方法,鼠标右键,点击Run as Junit Test
     */
    @Test
    public void test1() {
        System.out.println("Junit专业测试");
        
    }
    /**
     * 创建Spring容器,
     * Spring容器用来管理对象
     * Spring容器是IOC的核心。
     * Spring容器本身也是一个对象
     * 
     */
    @Test
    public void test2() {
        //接口-new实现类
        //创建容器时,需要声明所需要的配置文件applicationContext.xml
        //容器对象会从classpath下读取此文件
        //在实例化容器后,它会自动读取此文件,
        ApplicationContext ctx=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        
    }
    /**
     * 1.通过构造器创建Bean
     * 2.通过静态工厂方法创建Bean
     * 3.通过实例工厂创建Bean
     */
    
    @Test
    public void test3() {
        //1.创建Spring容器,容器自动加载配置文件
        //自动创建文件中所声明的bean
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.从容器中获取指定(通过id获取)的bean
        List list=(List)ctx.getBean("obj1");
        System.out.println(list);
        System.out.println("-------------");
        Calendar cal= (Calendar)ctx.getBean("obj2");
        System.out.println(cal);
        Date date=(Date)ctx.getBean("obj3");
        System.out.println(date);
    }
    /**
     * bean的作用域:每个类型的bean在容器中默认都是单例的
     */
    @Test
    public void test4() {
        ApplicationContext ctx=new ClassPathXmlApplicationContext
                ("applicationContext.xml");
        Map map1=(Map)ctx.getBean("obj4");
        Map map2=(Map)ctx.getBean("obj4");
        System.out.println("map1:"+map1);
        System.out.println("map2:"+map2);
        System.out.println(map1==map2);
    }
    
    //容器管理bean的生命周期
    @Test
    public void test5() {
        //ClassPathXmlApplicatonContext
        //继承于AbstractApplicationContext
        //后者实现了ApplicationContext接口。
        //AbstractApplicaitonContext里面声明了
        //关闭容器的方法close.
        AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("------1-------");
        Example exa=(Example)ctx.getBean("exa");
        System.out.println(exa);
        System.out.println("-----2-----");
        ctx.close();
        
    }
    /**
     * 依赖-setter注入
     */
    @Test
    public void test6() {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Computer computer=(Computer)ctx.getBean("computer");
        System.out.println(computer.getHdd());
        System.out.println(computer.getMainboard());
        System.out.println(computer.getRam());
        
    }
    //依赖注入-构造器注入
    @Test
    public void test7() {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
            MobilePhone phone=(MobilePhone) ctx.getBean("phone");
            System.out.println(phone.getCpu());
            System.out.println(phone.getRam());
    }
    
    //自动装配
    @Test
    public void test8() {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu=(Student) ctx.getBean("student");
        System.out.println(stu.getComputer().getMainboard());
    System.out.println(stu.getphone().getCpu());
    }
    @Test
    public void test9() {
        //Spring在给参数传值的时候能自动转型
        //参数注入
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Message msg=(Message)ctx.getBean("msg");
        System.out.println(msg.getId());
        System.out.println(msg.getName());
        System.out.println(msg.getSalary());
        System.out.println(msg.getPhone().getCpu());
        System.out.println(msg.getCities().get(0));
        System.out.println(msg.getScore().get("jsd1604"));
        System.out.println(msg.getParams().getProperty("user"));
    }
    
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值