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过重,其他正常