spring框架简单测试案例

一.spring框架介绍

二.spring简单测试案例实现

1.环境搭建

(1)导包:新建web项目文件夹,在Web-Content目录下的WEB-INF下的lib文件夹中导入需要的包,如下图所示:


(2)导入约束:在eclipse中点击Window》-Preference,然后在搜索框中输入“catalog”,选择XML Catalog,点击右侧的add按钮,详情如下:


接下来操作如图所示,点击File System,找到需要导入的约束文件:




在eclipse中的web项目中新建一个applicationContext.xml配置文件,打开,添加<beans></beans>,


点击eclipse编辑界面左下角的Design|Source切换视图,


选择Edit NameSpaces,





选择刚才导入的约束



将视图切换到Source视图,可看到配置(同时按下Alt+/有代码提示代表约束导入成功)


(3)测试案例的编写

Customer类:

package cn.hj.bean;
public class Customer {

private String name;
private Integer age;
private  Movie  movie;      //此处的movie是一个Movie类型的数据
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

public Movie getMovie() {
return movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
@Override
public String toString() {
return "Customer [name=" + name + ", age=" + age + ", movie=" + movie + "]";
}
}

Movie类:

package cn.hj.bean;
public class Movie {

@Override
public String toString() {
return "Movie [name=" + name + ", director=" + director + "]";
}
private String name;
private String director;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}

}

applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beanxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">


//首先将Movie对象配置到Spring容器中,使用set方法注入

   <bean name="movie1" class="cn.hj.bean.Movie">

     //bean元素代表需要交给Spring容器管理的类

     //值类型注入

    <property name="name" value="transformer"></property>

    <property name="director" value="famous"></property>

   </bean>

//将Movie对象注入到Customer对象的movie属性当中

<bean name="customer" class="cn.hj.bean.Customer">

              //值类型注入(使用value)

<property name="age" value="18"></property>

<property name="name" value="jerry"></property>

          //引用类型注入(使用ref)

<property name="movie" ref="movie1"></property>
</bean>
</beans>


测试类:

package cn.hj.bean;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {
@Test
public void fun1(){
//读取配置文件,创建容器对象,

ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

              //向Spring容器索要customer对象(在applicationContext.xml文件中配置好的名为customer的对象)

                //Spring容器会把类中的方法属性封装成一个Bean实体,这样我们可以随时通过实体获得其方法或者属性值

Customer c = (Customer)ac.getBean("customer");
System.out.println(c);
}
}

测试结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.

Customer [name=jerry, age=18, movie=Movie [name=transformer, director=famous]]


(4)测试案例编写过程中遇到的问题:

1.Customer类中的movie属性类型定义错误,movie应该是Movie类型的数据,private Movie movie;

而不是private  String  movie;


猜你喜欢

转载自blog.csdn.net/JayBillions/article/details/80329230