spring测试中发现的问题

本文详细介绍了如何在Spring框架中进行Bean的定义与配置,包括将所有jar包加入构建路径,以及创建service.xml文件来定义Bean。同时,通过一个具体的例子——Student类及其测试类StudentTest,展示了如何使用Spring的ApplicationContext来获取Bean实例并进行测试。文章还提到了在测试过程中遇到的错误及解决方法,指出添加commons.logging.jar包可以解决问题。

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

我的博客:www.while0.com

我的博客:www.shishangguan.net

把spring 的lib下所有的jar包全部放入到buildpath下边,构建service.xml如下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5            http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7   <bean id="Student" class="money.spring.test.Student">
 8     <!-- collaborators and configuration for this bean go here -->
 9   </bean>
10 <!-- 
11   <bean id="StudentManager" class="money.spring.test.StudentManager">
12       <property name=""></property>
13   </bean>
14  -->
15   <!-- more bean definitions go here -->
16 
17 </beans>

Student.java的内容

 1 /**
 2  * 
 3  */
 4 package money.spring.test;
 5 
 6 /**
 7  * @author Administrator
 8  *
 9  */
10 public class Student {
11     private String name;
12     private int id;
13     
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public int getId() {
21         return id;
22     }
23     public void setId(int id) {
24         this.id = id;
25     }
26     
27 }

单元测试StudentTest.java的内容

 1 package money.spring.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class StudentTest {
 8 
 9     @Test
10     public void testStudent() {
11         ApplicationContext context =
12                 new ClassPathXmlApplicationContext("service.xml");
13         //Student stu=context.getBean("Student",Student.class);//泛型构造方法,不需要手动转换
14         Student stu=(Student)context.getBean("Student");//返回object,需要手动转换
15     }
16 
17 }

这时出现如下错误提示

添加commons.logging.jar包后问题得到解决.

转载于:https://www.cnblogs.com/yamadie/archive/2013/03/20/2971203.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值