马士兵Spring学习笔记文档格式.docx

上传人:b****6 文档编号:20185293 上传时间:2023-01-17 格式:DOCX 页数:18 大小:23.06KB
下载 相关 举报
马士兵Spring学习笔记文档格式.docx_第1页
第1页 / 共18页
马士兵Spring学习笔记文档格式.docx_第2页
第2页 / 共18页
马士兵Spring学习笔记文档格式.docx_第3页
第3页 / 共18页
马士兵Spring学习笔记文档格式.docx_第4页
第4页 / 共18页
马士兵Spring学习笔记文档格式.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

马士兵Spring学习笔记文档格式.docx

《马士兵Spring学习笔记文档格式.docx》由会员分享,可在线阅读,更多相关《马士兵Spring学习笔记文档格式.docx(18页珍藏版)》请在冰豆网上搜索。

马士兵Spring学习笔记文档格式.docx

b)UserSpecifiedEntries–add

i.Location:

D:

\share\0900_Spring\soft\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsd

ii.URI:

file:

///D:

/share/0900_Spring/soft/spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsd

iii.KeyType:

SchemaLocation

iv.Key:

http:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd

2.注入类型

a)Spring_0300_IOC_Injection_Type

b)setter(重要)

c)构造方法(可以忘记)

d)接口注入(可以忘记)

3.idvs.name

a)Spring_0400_IOC_Id_Name

b)name可以用特殊字符

4.简单属性的注入

a)Spring_0500_IOC_SimpleProperty

b)<

propertyname=…value=….>

5.<

bean中的scope属性

a)Spring_0600_IOC_Bean_Scope

b)singleton单例

c)proptotype每次创建新的对象

6.集合注入

a)Spring_0700_IOC_Collections

b)很少用,不重要!

参考程序

7.自动装配

a)Spring_0800_IOC_AutoWire

b)byName

c)byType

d)如果所有的bean都用同一种,可以使用beans的属性:

default-autowire

8.生命周期

a)Spring_0900_IOC_Life_Cycle

b)lazy-init(不重要)

c)init-methoddestroy-methd不要和prototype一起用(了解)

9.Annotation第一步:

a)修改xml文件,参考文档<

context:

annotation-config/>

10.@Autowired

a)默认按类型bytype

b)如果想用byName,使用@Qulifier

c)写在privatefield(第三种注入形式)(不建议,破坏封装)

d)如果写在set上,@qualifier需要写在参数上

11.@Resource(重要)

a)加入:

j2ee/common-annotations.jar

b)默认按名称,名称找不到,按类型

c)可以指定特定名称

d)推荐使用

e)不足:

如果没有源码,就无法运用annotation,只能使用xml

12.@Component@Service@Controller@Repository

a)初始化的名字默认为类名首字母小写

b)可以指定初始化bean的名字

13.@Scope

14.@PostConstruct=init-method;

@PreDestroy=destroy-method;

什么是AOP

1.面向切面编程Aspect-Oriented-Programming

a)是对面向对象的思维方式的有力补充

2.Spring_1400_AOP_Introduction

可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码

a)Filter

b)Struts2的interceptor

4.概念:

a)JoinPoint

b)PointCut

c)Aspect(切面)

d)Advice

e)Target

f)Weave

SpringAOP配置与应用

1.两种方式:

a)使用Annotation

b)使用xml

2.Annotation

a)加上对应的xsd文件spring-aop.xsd

b)beans.xml<

aop:

aspectj-autoproxy/>

c)此时就可以解析对应的Annotation了

d)建立我们的拦截类

e)用@Aspect注解这个类

f)建立处理方法

g)用@Before来注解方法

h)写明白切入点(execution…….)

i)让spring对我们的拦截器类进行管理@Component

3.常见的Annotation:

a)@Pointcut

b)@Before

c)@AfterReturning

d)@AfterThrowing

e)@After

f)@Around

4.织入点语法

a)void!

void

b)参考文档(*..)

5.xml配置AOP

a)把interceptor对象初始化

config

i.<

aspect…..

1.<

pointcut

2.<

before

Spring整合Hibernate

1.Spring指定datasource

a)参考文档,找dbcp.BasicDataSource

i.c3p0

ii.dbcp

iii.proxool

b)在DAO或者Service中注入dataSource

c)在Spring中可以使用PropertyPlaceHolderConfigure来读取Properties文件的内容

2.Spring整合Hibernate

a)<

bean..AnnotationSessionFactoryBean>

propertydataSource

ii.<

annotatedClasses

b)引入hibernate系列jar包

c)User上加Annotation

d)UserDAO或者UserServie注入SessionFactory

e)jar包问题一个一个解决

3.声明式的事务管理

a)事务加在DAO层还是Service层?

i.加入annotation.xsd

ii.加入txManagerbean

iii.<

tx:

annotation-driven

iv.在需要事务的方法上加:

@Transactional

v.需要注意,使用SessionFactory.getCurrentSession不要使用OpenSession

c)@Transactional详解

i.什么时候rollback

1.运行期异常,非运行期异常不会触发rollback

2.必须uncheck(没有catch)

3.不管什么异常,只要你catch了,spring就会放弃管理

4.事务传播特性:

propagation_required

5.read_only

d)xml(推荐,可以同时配置好多方法)

beantxmanager

config

advisorpointcut-refadvice-ref

advice:

idtransaction-manager=

<

?

xmlversion="

1.0"

encoding="

UTF-8"

>

beansxmlns="

http:

//www.springframework.org/schema/beans"

xmlns:

xsi="

//www.w3.org/2001/XMLSchema-instance"

context="

//www.springframework.org/schema/context"

aop="

//www.springframework.org/schema/aop"

tx="

//www.springframework.org/schema/tx"

xsi:

schemaLocation="

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/contexthttp:

//www.springframework.org/schema/context/spring-context-2.5.xsd

//www.springframework.org/schema/aop

//www.springframework.org/schema/aop/spring-aop-2.5.xsd

//www.springframework.org/schema/tx

//www.springframework.org/schema/tx/spring-tx-2.5.xsd"

<

component-scanbase-package="

com.bjsxt"

/>

!

--

<

beanid="

dataSource"

class="

mons.dbcp.BasicDataSource"

destroy-method="

close"

propertyname="

driverClassName"

value="

com.mysql.jdbc.Driver"

url"

jdbc:

mysql:

//localhost:

3306/spring"

username"

root"

password"

bjsxt"

/bean>

-->

bean

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"

locations"

<

value>

classpath:

jdbc.properties<

/value>

/property>

destroy-method="

value="

${jdbc.driverClassName}"

${jdbc.url}"

${jdbc.username}"

${jdbc.password}"

sessionFactory"

org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"

ref="

annotatedClasses"

list>

<

com.bjsxt.model.User<

com.bjsxt.model.Log<

/list>

-->

packagesToScan"

com.bjsxt.model<

hibernateProperties"

props>

propkey="

hibernate.dialect"

org.hibernate.dialect.MySQLDialect

/prop>

hibernate.show_sql"

true<

/props>

txManager"

org.springframework.orm.hibernate3.HibernateTransactionManager"

config>

pointcutid="

bussinessService"

expression="

execution(public*com.bjsxt.service..*.*(..))"

advisorpointcut-ref="

advice-ref="

txAdvice"

/aop:

adviceid="

transaction-manager="

attributes>

methodname="

getUser"

read-only="

true"

add*"

propagation="

REQUIRED"

/>

/tx:

advice>

 

/beans>

e)HibernateTemplate、HibernateCallback、HibernateDaoSupport(不重要)介绍

i.设计模式:

TemplateMethod

ii.Callback:

回调/钩子函数

iii.第一种:

(建议)

1.在spring中初始化HibernateTemplate,注入sessionFactory

2.DAO里注入HibernateTemplate

3.save写getHibernateTemplate.save();

iv.第二种:

1.从HibernateDaoSupport继承

2.必须写在xml文件中,无法使用Annotation,因为set方法在父类中,而且是final的

f)spring整合hibernate的时候使用packagesToScan属性,可以让spring自动扫描对应包下面的实体类

Struts2.1.6+Spring2.5.6+Hibernate3.3.2

1.需要的jar包列表

jar包名称

所在位置

说明

antlr-2.7.6.jar

hibernate/lib/required

解析HQL

aspectjrt

spring/lib/aspectj

AOP

aspectjweaver

..

cglib-nodep-2.1_3.jar

spring/lib/cglib

代理,二进制增强

common-annotations.jar

spring/lib/j2ee

@Resource

commons-collections-3.1.jar

集合框架

commons-fileupload-1.2.1.jar

struts/lib

struts

commons-io-1.3.2

commons-logging-1.1.1

单独下载,删除1.0.4(struts/lib)

spring

dom4j-1.6.1.jar

hibernate/required

解析xml

ejb3-persistence

hibernate-annotation/lib

@Entity

freemarker-2.3.13

hibernate3.jar

hibernate

hibernate-annotations

hibernate-annotation/

hibernate-common-annotations

javassist-3.9.0.GA.jar

hiberante/lib/required

jta-1.1.jar

hibernatetransaction

junit4.5

mysql-

ognl-2.6.11.jar

slf4j-api-1.5.8.jar

hibernate-log

slf4j-nop-1.5.8.jar

spring.jar

spring/dist

struts2-core-2.1.6.jar

xwork-2.1.2.jar

struts2

commons-dbcp

spring/lib/jarkata-commons

commons-pool.jar

struts2-spring-plugin-2.1.6.jar

2.BestPractice:

a)将这些所有的jar包保存到一个位置,使用的时候直接copy

3.步骤

a)加入jar包

b)首先整合Spring+Hibernate

i.建立对应的package

1.dao/dao.impl/model/service/service.impl/test

ii.建立对应的接口与类框架

1.S2SH_01

iii.建立spring的配置文件(建议自己保留一份经常使用的配置文件,以后用到的时候直接copy改)

iv.建立数据库

v.加入Hibernate注解

1.在实体类上加相应注解@Entity@Id等

2.在beans配置文件配置对应的实体类,使之受管

vi.写daoservice的实现

vii.加入Spring注解

1.在对应Service及DAO实现中加入@Component,让spring对其初始化

2.在Service上加入@Transactional或者使用xml方式(此处建议后者,因为更简单)

3.在DAO中注入sessionFactory

4.在Service中注入DAO

5.写DAO与Service的实现

viii.写测试

c)整合Struts2

i.结合点:

Struts2的Action由Spring产生

ii.步骤:

1.修改web.xml加入struts的filter

2.再加入spring的listener,这样的话,webapp一旦启动,spring容器就初始化了

3.规划struts的action和jsp展现

4.加入struts.xml

a)修改配置,由spring替代struts产生Action对象

5.修改action配置

a)把类名改为bean对象的名称,这个时候就可以使用首字母小写了

b)@Scope(“prototype”)不要忘记

iii.struts的读常量:

1.struts-default.xml

2.struts-plugin.xml

3.struts.xml

4.struts.properties

5.web.xml

iv.中文问题:

1.Struts2.1.8已经修正,只需要改i18n.encoding=gbk

2.使用spring的characterencoding

3.需要严格注意filter的顺序

4.需要加到Struts2的filter前面

v.LazyInitializationException

1.OpenSessionInViewFilter

2.需要严格顺序问题

3.需要加到struts2的filter前面

web-appversion="

2.5"

xmlns="

//jav

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 水产渔业

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1