spring sample application in netbeans

2019-09-21 16:11发布

This is my code. I have gotten some errors. Can anyone help me solve the problem? I get some casting errors, like cast ClassPathResource to Resource and resource object r to resource.

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();

} 

}

This is my XML file;

<?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>

2条回答
beautiful°
2楼-- · 2019-09-21 16:42

You are using both the resources like javax.annotation.Resource and org.springframework.core.io.Resource, which has resulted in a conflict.

You need to use them like this

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

then program will execute.

查看更多
姐就是有狂的资本
3楼-- · 2019-09-21 17:05

Instead of importing

javax.annotation.Resource

use,

org.springframework.core.io.Resource
查看更多
登录 后发表回答