解决spring的xml文件cannot be open ( class path resource cannot be opened)
生活随笔
收集整理的這篇文章主要介紹了
解决spring的xml文件cannot be open ( class path resource cannot be opened)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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:
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 {@Testpublic void testAdd() {ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");Userservice userservice = context.getBean("userservice", Userservice.class);userservice.add();} }run之后結果:
總結
以上是生活随笔為你收集整理的解决spring的xml文件cannot be open ( class path resource cannot be opened)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1作业python数据类型 条件循环 列
- 下一篇: springboot的yml配置文件绑定