Spring配置参考资料_第1页
Spring配置参考资料_第2页
Spring配置参考资料_第3页
Spring配置参考资料_第4页
Spring配置参考资料_第5页
已阅读5页,还剩39页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Spring简单用例所需jarSpring配置问题注意一:注入beanXML文件源码(文件的标头)<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd"> <beanid="hello"class=".HelloWord"></bean> <beanid="student"class=".Student"></bean> <beanid="studentService"class=".StudentService"> <propertyname="jk"ref="student"></property> </bean></beans>配置中的ref指定的是Bean注意二:属性注入属性注册,顺序可改变注意三:重写toString方法使用直接输出时需要重写toString方法。注意四:构造方法注入之类型注入参数的顺序不可以改变;此处有个几个参数就找几个参数的构造方法Exceptioninthread"main"org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'peoper3'definedinclasspathresource[beans.xml]:Couldnotresolvematchingconstructor(hint:specifyindex/type/nameargumentsforsimpleparameterstoavoidtypeambiguities)如报此错误是:类中没有有参构造方法!注意五:构造方法注入控制台错误及原因Exceptioninthread"main"org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'peoper'definedinclasspathresource[beans.xml]:Instantiationofbeanfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Couldnotinstantiatebeanclass[com.zx.entity.Peoper]:Nodefaultconstructorfound;nestedexceptionisjava.lang.NoSuchMethodException:com.zx.entity.Peoper.<init>()原因是缺少无参构造方法注意六:构造方法注入之顺序注入注意:顺序从0开始,顺序注入可添加类型格式。注意七:工厂方法注入之动态注入创建工厂类及方法。 结果:注意八:工厂方法注入之静态注入注意九:注入参数之bean间的嵌套(注入bean)注意十:注入参数之内部bean注意十一:集合注入ListSetMap系统配置propertiesSpring自动装配通过配置default-autowire属性,SpringIOC容器可以自动为程序注入bean;默认default,no,不启动自动装配按照名称装配注意一:按照名称装配必须跟类中的名称匹配起来按照类型装配注意一:按照类型装配问题:原因是存在两个类型一样的!注意二:按类型装配(id问题)此处的id可不与类中的名一致!按照构造方法装配注意一:按构造方法装配(id问题)此处的id可不与类中的名一致!方法注入Springbean作用默认是单例,通过配置prototype实现多利方法注入lookup-methodBean之间的关系继承Parent属性依赖未添加依赖:添加依赖:依赖属性depends-on引用如spring配置注意九Bean的作用范围参数:scope默认singletonSpringioc容器中仅有一个bean实例,bean以单例的方式存在Prototype每次从容器中调用Bean时,都产生一个新的实例。SpringAOP实例实例一:前置通知ServiceAspect接口及实现类ServiceAspectIplPeopleService接口及实现类PeopleServiceIpl<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/aop/schema/aop/spring-aop-2.0.xsd"> <beanid="peopleService"class="com.zx.service.ipl.PeopleServiceIpl"></bean> <beanid="serviceAspect"class="com.zx.service.ipl.ServiceAspectIpl"></bean> <!-- 设置千面切入点--> <aop:config> <!-- 将serviceAspect切入进peopleService--> <aop:aspectid="serviceAspect"ref="serviceAspect"> <aop:pointcutexpression="execution(*com.zx.service.*.*(..))"id="aspect"/> <aop:beforemethod="add"pointcut-ref="aspect"/> </aop:aspect> </aop:config></beans>启动报错及原因原因:缺少jar包AOP所需的jar包获取切点的类及方法名后置通知环绕通知异常通知Spring对JDBC的支持Xml标头引入命名空间:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans" xmlns:context="/schema/context"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/aop/schema/aop/spring-aop-2.0.xsd/schema/context/schema/context/spring-context-2.5.xsd"></beans>数据源代码XML文件代码:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans" xmlns:context="/schema/context"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/aop/schema/aop/spring-aop-2.0.xsd/schema/context/schema/context/spring-context-2.5.xsd"><!--加载指定路径properties--><context:property-placeholderlocation="classpath:perties"/> <!--数据源--><beanid="dataSource"class="mons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"value="${driverClassName}"/><propertyname="url"value="${url}"/><propertyname="username"value="${username}"/><propertyname="password"value="${password}"/></bean></beans>此处用到了dbcp的连接池!数据源代码所需要的jar包数据源链接driverClassName=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@localhost:1521:ORCLusername=scottpassword=tiger数据源链接所需的jar包添加oracle驱动jdbc所用到的包使用Jdbctemplate对象来完成jdbc操作<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans" xmlns:context="/schema/context"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/aop/schema/aop/spring-aop-2.0.xsd/schema/context/schema/context/spring-context-2.5.xsd"><!--加载指定路径properties--><context:property-placeholderlocation="classpath:perties"/> <!--数据源--><beanid="dataSource"class="mons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"value="${driverClassName}"/><propertyname="url"value="${url}"/><propertyname="username"value="${username}"/><propertyname="password"value="${password}"/></bean><!--使用Jdbctemplate对象来完成jdbc操作--><beanid="jdbcTemplate"class

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论