Spring简单用例所需jarSpring配置问题注意一:注入beanXML文件源码(文件的标头)encoding="UTF-8"?>hema/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.xsd">anid="hello"class="com.zx.cn.HelloWord">nt"class="com.zx.cn.Student">ass="com.zx.cn.StudentService"> 配置中的ref指定的是Bean注意二:属性注入属性注册,顺序可改变注意三:重 写toString方法使用直接输出时需要重写toString方法。注意四:构造方法注入之类型注入参数的顺序不可以改变;此处有个几个 参数就找几个参数的构造方法Exceptioninthread"main"org.springframework.beans .factory.BeanCreationException:Errorcreatingbeanwithname''pe oper3''definedinclasspathresource[beans.xml]:Couldnotreso lvematchingconstructor(hint:specifyindex/type/namearguments forsimpleparameterstoavoidtypeambiguities)如报此错误是:类中没有有参构造方 法!注意五:构造方法注入控制台错误及原因Exceptioninthread"main"org.springframewor k.beans.factory.BeanCreationException:Errorcreatingbeanwithn ame''peoper''definedinclasspathresource[beans.xml]:Instanti ationofbeanfailed;nestedexceptionisorg.springframework.bea ns.BeanInstantiationException:Couldnotinstantiatebeanclass[ com.zx.entity.Peoper]:Nodefaultconstructorfound;nestedexcep tionisjava.lang.NoSuchMethodException:com.zx.entity.Peoper.it>()原因是缺少无参构造方法注意六:构造方法注入之顺序注入注意:顺序从0开始,顺序注入可添加类型格式。注意七:工厂方法注入之动 态注入创建工厂类及方法。结果:注意八:工厂方法注入之静态注入注意九:注入参数之bean间的嵌套(注入bean)注意十:注入参数之 内部bean注意十一:集合注入ListSetMap系统配置propertiesSpring自动装配通过配置default-auto wire属性,SpringIOC容器可以自动为程序注入bean;默认default,no,不启动自动装配按照名称装配注意一:按照 名称装配必须跟类中的名称匹配起来按照类型装配注意一:按照类型装配问题:原因是存在两个类型一样的!注意二:按类型装配(id问题)此 处的id可不与类中的名一致!按照构造方法装配注意一:按构造方法装配(id问题)此处的id可不与类中的名一致!方法注入Sprin gbean作用默认是单例,通过配置prototype实现多利方法注入lookup-methodBean之间的关系继承Pare nt属性依赖未添加依赖:添加依赖:依赖属性depends-on引用如spring配置注意九Bean的作用范围参数:scope默认 singletonSpringioc容器中仅有一个bean实例,bean以单例的方式存在Prototype每次从容器中调 用Bean时,都产生一个新的实例。SpringAOP实例实例一:前置通知ServiceAspect接口及实现类ServiceAs pectIplPeopleService接口及实现类PeopleServiceIploding="UTF-8"?>a/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xm lns:aop="http://www.springframework.org/schema/aop"xsi:schemaLoc ation="http://www.springframework.org/schema/beanshttp://www.spr ingframework.org/schema/beans/spring-beans.xsdhttp://www.springf ramework.org/schema/aophttp://www.springframework.org/schema/ao p/spring-aop-2.0.xsd">rvice.ipl.PeopleServiceIpl">="com.zx.service.ipl.ServiceAspectIpl">p:config>erviceAspect"ref="serviceAspect">tion(com.zx.service..(..))"id="aspect"/>add"pointcut-ref="aspect"/>启动报 错及原因原因:缺少jar包AOP所需的jar包获取切点的类及方法名后置通知环绕通知异常通知Spring对JDBC的支持Xml标头引 入命名空间:www.springframework.org/schema/beans"xmlns:context="http://www.sp ringframework.org/schema/context"xmlns:xsi="http://www.w3.org/20 01/XMLSchema-instance"xmlns:aop="http://www.springframework.org/ schema/aop"xsi:schemaLocation="http://www.springframework.org/sc hema/beanshttp://www.springframework.org/schema/beans/spring-bea ns.xsdhttp://www.springframework.org/schema/aophttp://www.spri ngframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springfr amework.org/schema/contexthttp://www.springframework.org/schema /context/spring-context-2.5.xsd">数据源代码XML文件代码:on="1.0"encoding="UTF-8"?>rk.org/schema/beans"xmlns:context="http://www.springframework.org /schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst ance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:s chemaLocation="http://www.springframework.org/schema/beanshttp:/ /www.springframework.org/schema/beans/spring-beans.xsdhttp://www .springframework.org/schema/aophttp://www.springframework.org/s chema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schem a/contexthttp://www.springframework.org/schema/context/spring-c ontext-2.5.xsd">ty-placeholderlocation="classpath:jdbc.properties"/>ontext:property-placeholderlocation="classpath:jdbc.properties" />bcp.BasicDataSource"destroy-method="close">erClassName"value="${driverClassName}"/>alue="${url}"/> 创建dao层接口及实现Bean注入Dao实现添加数据更新方法:删除方法:查看方法:集成JdbcDaoSupportSpring事务编程式事务在方法中写入此代码在XML中配置java事务管理器声明式事务XML配置声明式事务添加事务的命名空间注解配置声明式事务配置事务通知S2SH整合Jar包 |
|