本文共 1227 字,大约阅读时间需要 4 分钟。
xml文件一直显示cannot be open
报错语句有一条:
class path resource [bean2.xml] cannot be opened because it does not exist at(后面的没有复制了)然后好像是xml没有被编译的原因?
设置了一个resourse mark as test resources root
现在目录如下:
然后问题解决了。 Userservice:package com.spring.demo.service;import com.spring.demo.dao.UserDao;import com.spring.demo.dao.UserDaoimp;public class Userservice { //创建userdao类属性 private UserDao userdao; public void setUserdao(UserDao userdao) { this.userdao = userdao; } public void add() { System.out.println("servicee add.."); } public void setUserDao(UserDaoimp userDao) { this.userdao=userDao; }}
bean2.xml:
Testbean.java:
package com.spring.demo.test;import com.spring.demo.service.Userservice;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.testng.annotations.Test;public class Testbean { @Test public void testAdd() { ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); Userservice userservice = context.getBean("userservice", Userservice.class); userservice.add(); }}
run之后结果:
转载地址:http://uvten.baihongyu.com/