解决spring的xml文件cannot be open ( class path resource cannot be opened)
原创
©著作权归作者所有:来自51CTO博客作者TvT~的原创作品,请联系作者获取转载授权,否则将追究法律责任
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:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id = "userservice" class="com.spring.demo.service.Userservice">
<!-- 注入userdao对象,name属性:类里面属性名称-->
<!-- ref属性:创建userdao对象bean标签的id值-->
<property name="UserDao" ref="userDaoimp"></property>
</bean>
<bean id = "userDaoimp" class="com.spring.demo.dao.UserDaoimp"></bean>
</beans>
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之后结果: