spring入门提问

问题描述

运行spring程序需要哪些包的? 谢谢!第一天学习spring,按照《Spring开发指南》中的第一个例子。-- Action.javapackage net.xiaxin.spring.qs;public interface Action {public String execute(String str);}-- UpperAction.javapackage net.xiaxin.spring.qs;public class UpperAction implements Action {private String message;public String getMessage() {return message;}public void setMessage(String string) {message = string;}public String execute(String str) {return (getMessage() + str).toUpperCase();}}-- LowerAction.javapackage net.xiaxin.spring.qs;public class LowerAction implements Action {private String message;public String getMessage() {return message;}public void setMessage(String string) {message = string;}public String execute(String str) {return (getMessage()+str).toLowerCase();}}-- bean.xml<beans><description>Spring Quick Start</description><bean id="TheAction" class="net.xiaxin.spring.qs.UpperAction"><property name="message"><value>HeLLo</value></property></bean></beans>-- SimpleTest.javapackage test;import net.xiaxin.spring.qs.Action;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class SimpleTest {public static void main(String[] args) {ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");Action action = (Action) ctx.getBean("TheAction");System.out.println(action.execute("Rod Johnson"));}}报如下错误2008-8-12 10:25:52 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1bf52a5: display name [org.springframework.context.support.FileSystemXmlApplicationContext@1bf52a5]; startup date [Tue Aug 12 10:25:51 CST 2008]; root of context hierarchy2008-8-12 10:25:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from file [D:projectSpringTestbean.xml]Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from file [D:projectSpringTestbean.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80)at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:423)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:353)at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)at test.SimpleTest.main(SimpleTest.java:11)Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)... 14 more

解决方案

不知道你使用的是哪个版本, 在bean的配置文件中加上namespace, spring要做检查的, 给个spring2.5的例子<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><bean id="TheAction" class="net.xiaxin.spring.qs.UpperAction"><property name="message"><value>HeLLo</value></property></bean> </beans>
解决方案二:
http://forum.springframework.org/showthread.php?t=37877在bean.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">另外,查看一下有没有重复引用的jar包.
解决方案三:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"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-2.0.xsd"><bean name="" .........></bean></beans>bean.xml上面要加上这些注释
解决方案四:
ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); 这句话说找不到bean.xml 换成ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:bean.xml"); 应该就可以了

时间: 2024-10-28 10:52:10

spring入门提问的相关文章

spring入门(2)---第一个spring案例

直接上图: 源码: HelloDao.java package www.csdn.spring.dao; public interface HelloDao { public void sayHello(); } HelloDAoImpl.java package www.csdn.spring.dao; public class HelloDaoImpl implements HelloDao{ public HelloDaoImpl() { System.out.println("Hello

spring入门(1)---开始spring之旅

Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发 . IOC 控制反转 public class PersonServiceBean { private PersonDao personDao = new PersonDaoBean(); public void save(Person person){ personDao.save(person); } } PersonDaoBean 是在应用内部创

《Spring 5 官方文档》1. Spring入门指南(一)

译者注:为了方便理解,本译文里面部分名词翻译如下,如有异议,请在评论区指出. Dependency Injection - 依赖注入 Inversion of Control – 控制反转 Instrumentation – 植入 AOP - 面向切面​​的编程 Milestones – 里程碑 Snapshots – 快照 Artifact - 工件(这个词在文中大部分地方都没有翻译,用过Maven或Gradle的都明白,如果有更好的中文翻译,请写在评论区) 1.Spring入门指南 本参考指

spring入门(14)ssh中事务处理spring配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/t

spring入门(8) 装配Bean之自动装配

Spring_Autowiring collaborators 在Spring3.2.2中自动装配类型,分别为:no(default)(不采用自动装配). byName,byType,constructor下面来分别介绍一下这些是如何自动装配的 <bean id="foo" class="...Foo" autowire="autowire type"> Mode            Explanation no: (Default

spring入门(4) spring中Bean的生命周期总结

Spring中Bean的生命周期,在学习spring的过程中bean的生命周期理解对学习spring有很大的帮助,下面我就分别介绍在 ApplicationContext和BeanFactory中Bean的生命周期. 1.在ApplicationContext中Bean的生命周期 生命周 期执行的过程如下: 1.需找所有的bean根据bean定义的信息来实例化bean 2.使用依赖注入,spring按bean 定义信息配置bean的所有属性 3.若bean实现了BeanNameAware接口,工

最简单的Spring入门示例

应一位朋友的要求,写一个最简单的spring示例,使用spring的MVC,并应用了spring的依赖注入,实现简单应用,索性放在这里供还没入门的spring爱好者参考,初步感受一下spring应用(spring高手就不必看了,这里并没有涉及高级特性,比如与ORM框架的整合,事务管理,远程调用,代理等这些功能) spring至关重要的一环就是装配,即配置文件的编写,接下来我按刚才实际过程中一步步简单讲解. 首先,要在web.xml中配置DispatcherServlet,它是作为Spring M

【SSH系列】初识spring+入门demo

       学习过了hibernate,也就是冬天,经过一个冬天的冬眠,当春风吹绿大地,万物复苏,我们迎来了spring,在前面的一系列博文中,小编介绍hibernate的相关知识,接下来的博文中,小编将继续介绍SSH中的spring,Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建.简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架.今天这篇博文,小编将从what.why

spring入门编程问题集锦

1.如何学习Spring? 你可以通过下列途径学习spring: (1) spring下载包中doc目录下的MVC-step-by-step和sample目录下的例子都是比较好的spring开发的例子. (2) AppFuse集成了目前最流行的几个开源轻量级框架或者工具Ant,XDoclet,Spring,Hibernate(iBATIS),JUnit,Cactus,StrutsTestCase,Canoo's WebTest,Struts Menu,Display Tag Library,OS