你Resource res = new ClassPathResource("javamxj/spring/beginning3/HelloBean.xml");这个地方错了,这里应该是你spring的配置文件的路径,spring默认的是ApplicationContext.xml文件,你的是HelloBean.xml??,你把Spring的配置文件路径指正确了就可以了。
如果你测试程序的话,你把ApplicationContext.xml文件拷贝到src目录下,这样写就可以了。
public static void main(String[] args) {
HelloBean helloBean = new HelloBean();
System.out.println(helloBean.getHelloWorld()); // 利用Spring调用HelloBean
Resource res = new ClassPathResource("applicationContext.xml"); BeanFactory factory = new XmlBeanFactory(res);
helloBean = (HelloBean) factory.getBean("HelloBean");
System.out.println(helloBean.getHelloWorld());
}
如果你测试程序的话,你把ApplicationContext.xml文件拷贝到src目录下,这样写就可以了。
public static void main(String[] args) {
HelloBean helloBean = new HelloBean();
System.out.println(helloBean.getHelloWorld()); // 利用Spring调用HelloBean
Resource res = new ClassPathResource("applicationContext.xml"); BeanFactory factory = new XmlBeanFactory(res);
helloBean = (HelloBean) factory.getBean("HelloBean");
System.out.println(helloBean.getHelloWorld());
}
并把Resource res = new ClassPathResource("javamxj/spring/beginning3/HelloBean.xml");
改为Resource res = new ClassPathResource("HelloBean.xml");
就可以了因为ClassPathResource是在你classes目录下去查找XML文件的
正确的应该是classes/javamxj/spring/beginning3目录下
http://community.csdn.net/Expert/topic/5099/5099650.xml?temp=.2599451