瞳孔中的彼岸 2016-04-27 13:26 采纳率: 0%
浏览 1276

新人学spring框架xml路径不会填

package spring;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.core.env.StandardEnvironment;
public class DItest{

@SuppressWarnings("resource")  
public static void main(String[] args) {  
    ApplicationContext atx = new ClassPathXmlApplicationContext("beans.xml");  
    UserDao userDao = (UserDao) atx.getBean("userDao");  
    userDao.add();  
}  

}

package spring;

public class UserDao {

private User user;

public void add(){  
    System.out.println("add from userdao");  
    System.out.println(user.toString());  
}  

public User getUser() {  
    return user;  
}  

public void setUser(User user) {  
    this.user = user;  
}  

}

package spring;

public class User {

private String name;

private int age;

private int score;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

public String toString(){  
    return "tostring from user";  
}  

}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">







基本都是抄的代码,想运行试试结果发现报错
Cannot find class [spring.spring.User] for bean with name 'user' defined in class path resource [beans.xml];
应该是路径问题但是改了好多种都不对啊(╯‵□′)╯︵┻━┻,求教

  • 写回答

3条回答 默认 最新

  • 破小孩儿 2016-04-28 06:51
    关注

    学得不精呀。嘻嘻。
    ClassPathXmlApplicationContext,如果要用这个,把你的beans.xml放到你的源代码根目录下。
    换句话说,看看你的编译目标目录下(比如classes或target),有没有这个文件产生。

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP