【问题标题】:spring sample application in netbeansnetbeans中的spring示例应用程序
【发布时间】:2013-11-08 03:06:09
【问题描述】:

这是我的代码。我得到了一些错误。谁能帮我解决问题?我遇到了一些转换错误,例如将 ClassPathResource 转换为 Resource 并将资源对象 r 转换为资源。

package firstspring;

import javax.annotation.Resource;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

public class test {

public static void main(String[] args){

Resource r =new ClassPathResource("applicationContext.xml");


BeanFactory bf=new XmlBeanFactory(r);

Student stud=(Student)bf.getBean("first");

stud.display();

} 

}

这是我的 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:p="http://www.springframework.org/schema/p"

 xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="first" class="Student">

<property name="name" value="sneha"></property>    

</bean>

</beans>

【问题讨论】:

  • 它显示了一些错误所有这些错误是什么?
  • 格式化您的问题,使其更具可读性。
  • @PradeepSimha 错误是将 classpathresorce 转换为 cast..
  • 你在哪里声明了 eResource,或者它是一些内置类,如果是的话......你错过了导入。
  • eResource 似乎是一个错字。应该是Resource

标签: java spring spring-mvc netbeans


【解决方案1】:

而不是导入

javax.annotation.Resource

使用,

org.springframework.core.io.Resource

【讨论】:

    【解决方案2】:

    您同时使用了javax.annotation.Resourceorg.springframework.core.io.Resource 等资源,这导致了冲突。

    你需要像这样使用它们

    org.springframework.core.io.Resource r = new ClassPathResource("applicationContext.xml");
    

    然后程序将执行。

    【讨论】:

    • java.io.FileNotFoundException: 类路径资源 [applicationContext.xml] 无法打开,因为它不存在:我该怎么办?
    • 确保您是否使用独立的 java 程序将 applicationContext.xml 放入 src 文件夹。或者放入包中,然后像 new ClassPathResource("packagename/Applicationcontext.xml"); 这样的代码
    • 解决了..然后又出现了另一个问题:org.springframework.beans.factory.CannotLoadBeanClassException:找不到类路径资源中定义的名称为“first”的bean的类[Student] [firstspring/applicationContext .xml];
    • @R.J :org.springframework.beans.factory.CannotLoadBeanClassException: 找不到在类路径资源 [firstspring/applicationContext.xml] 中定义的名称为 'first' 的 bean 的类 [Student]; ——
    猜你喜欢
    • 1970-01-01
    • 2010-10-21
    • 2014-01-22
    • 1970-01-01
    • 2014-10-19
    • 2012-02-06
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    相关资源
    最近更新 更多