spring-day2- 测试代码及练习

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

import ioc.MessageBean;

public class TestCase {
/**
 * 测试类
 */
    @Test
    public void test1() {
        //启动spring容器
        ApplicationContext ac=new 
        ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器获取一个bean实例
        //getBean的重载方法,此处就不用强转类型了
        MessageBean mb=ac.getBean("msg",MessageBean.class);
        
        System.out.println(mb);
    }
    @Test
    public void test2() {
        //测试  读取properties文件的内容
        ApplicationContext ac=new 
        ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println(ac.getBean("config"));                
    }
    @Test
    public void test3() {
        //测试spring表达式,读取其他bean的属性
        ApplicationContext ac=new 
                ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println(ac.getBean("eb"));
    }
    
}

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

package test;

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

import annotation.Hotel;
import annotation.Manager;
import annotation.Restaurant;
import annotation.Student;

public class TestCase2 {
    @Test
    //测试组件扫描
    public void test1() {
        ApplicationContext ac=new 
        ClassPathXmlApplicationContext("app.xml");
        Student stu=ac.getBean("stu",Student.class);
        System.out.println(stu);
    }
    //测试作用域
    @Test
    public void test2() {
        ApplicationContext ac=new 
        ClassPathXmlApplicationContext("app.xml");
        Student stu=ac.getBean("stu",Student.class);
        Student stu2=ac.getBean("stu",Student.class);
        System.out.println(stu==stu2);
    }
    @Test
    //测试延迟加载
    public void test3() {
        ApplicationContext ac=new 
        ClassPathXmlApplicationContext("app.xml");
        
    }
    @Test
    //测试生命周期
    public void test4() {
        AbstractApplicationContext ac=new 
        ClassPathXmlApplicationContext("app.xml");
        Student stu=ac.getBean("stu",Student.class);
        System.out.println(stu);
        ac.close();
        
    }
    @Test
    public void test5() {
        ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
        Restaurant rest=ac.getBean("rest",Restaurant.class);
        System.out.println(rest);
        
        Hotel hotel=ac.getBean("hotel",Hotel.class);
        System.out.println(hotel);        
    }
    //测试@Resource来完成注入
    @Test
    public void test6() {
        ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
        Manager manager=ac.getBean("manager",Manager.class);
        System.out.println(manager);
        
    }
    //测试@Value.
    @Test
    public void test7() {
        ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
        Manager mg=ac.getBean("manager",Manager.class);
        System.out.println(mg);
        
    }
}    

-------------------------------------练习------

有两个类LoginService和AdminDAO,用注解的方式将AdminDAO注入到LoginService中。
有一i个类MessageService,用注解的方式为该类添加初始化方法和销毁方法
有一个ExampleBean,用引用的方式注入各种集合类型

计算一个人的bmi指数,

bmi=体重/身高/身高

bmi<19过轻,>24过重,其他正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值