spring学习笔记

来源:互联网 发布:两头usb数据插口线图片 编辑:程序博客网 时间:2024/06/05 14:06
-----------------------------------------------------------------
spring的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"
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-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


</beans>
---------------------------------------------------------------------------
xml配置文件
<bean id="" name="" class="" lazy-init="" abstract="" depends-on="" scope="" parent="">
</bean>
id和name都不用定义
name: 别名
lazy-init: 延迟加载(在访问时才创建)(缺省为false)
abstract: 抽象(true为不创建)
depends-on: 顺序加载
scope: prototype为多例(在getBean()时创建)singleton为单例(默认)
parent: (继承其他bean)spring容器中声明的某个bean名称
autowire: 自动注入(construct 会根据构造方法里面自动注入; byName 根据有哪些属性名和注册的bean名字一致自动注入; byType 根据有哪些属性名和注册的bean类型一致自动注入)


<bean id="" class="">
<property name="" ref="">
</property> 
</bean>
name: 想创建的属性名
ref: spring容器中声明的某个bean名称
------------------------------------------------------------------------
注册类型转换器:
<!-- spring中用于注册类型转换器的对象 -->
    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors"><!-- customEditors是一个map -->
    <map>
    <!-- java.util.Date类型,要使用DateConverter类型转换器(自己定义的)  -->
    <entry key="java.util.Date" value="com.hh.converters.DateConverter"></entry>
    </map>
    </property>
    </bean>

--------------------------------------------------------------------------------
赋值:
利用setXxxx()方法:
对于常见的类型,spring是可以自动转换的
<bean id="UserAction" class="com.hh.action.UserAction" >
<property name="size" value="10"></property>
<property name="userName" value="王浩"></property>
<property name="sex" value="true"></property>

<!-- Date类型需要自己定义类型转换器 -->
<property name="brithday" value="1995-04-27"></property>

<!-- 赋值map类型 -->
<property name="map">
<map>
<entry key="a" value="aaaa"></entry>
<entry key="b" value="bbbb"></entry>
</map>
</property>

<!-- 赋值List类型 -->
<property name="list">
<list>
<value>ceshi</value>
<value>测试</value>
</list>
</property>
</bean>

利用构造方法:(比较少用)
<bean id="UserAction" class="com.hh.action.UserAction">
<constructor-arg ref="UserService"></constructor-arg>
</bean>
-----------------------------------------------------------------------------------
引入外部文件
<!-- 引入外部文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>test.properties</value><!-- 引入第一个properties文件 -->
    </list>
    </property>
    </bean>

-----------------------------------------------------------------------------------
注解式开发:
用注解,在xml也可以注入,完全通用
<context:component-scan base-package="" />
在类名前定义:
@Component()
@Scope()
setXxx方法前或者变量前定义:
@Resource(name="")默认通过类型进行注入,可以定义name来注入

分类注解
呈现层的Action:
@Controller
业务逻辑层的Service:
@Service
持久化层的Dao:
@Repository


-------------------------------------------------------------------------------------
Aop
注解式:
Spring自动创建动态代理步骤:
引入三个jar: aopalliance.jaraspectjrt.jaraspectjweaver.jar
spring配置文件中使用标签: <aop:aspectj-autoproxy/>
需要在切面的类定义前使用注解: @Aspect
需要在切面的类的方法定义前使用注解:
@Before("execution(* 要代理对象的全路径父接口名.方法名(..))")例如:@Before("execution(* com.hh.domain.IMe.*(..))")@Before("execution(* com.hh.domain.MeInterface.play(..)) || execution(* com.hh.domain.YouInterface.play(..) ||)")
@After("execution(* 要代理对象的全路径父接口名.方法名(..))")

配置文件:
<aop:config>
<!-- 声明 切入点(精确到 方法) 当执行什么的时候-->
<aop:pointcut id="" expression="execution(* 要代理对象的全路径父接口名.方法名(..))"></aop:pointcut>

<!-- 声明 切入面-->
<aop:aspect ref="">
<aop:before pointcut-ref="" method="" />
</aop:aspect>
<!-- 声明 切入面-->
<aop:aspect ref="">
<aop:after pointcut-ref="" method="" />
</aop:aspect>
<!-- 声明 切入面-->
<aop:aspect ref="">
<aop:around pointcut-ref="" method="" />
</aop:aspect>
</aop:config>

例子:
<aop:config>
<!-- 声明 切入点(精确到 方法) 当执行什么的时候-->
<aop:pointcut id="allMeMethod" expression="execution(* com.hh.spring.domain.MeInterface.*(..))" />

<!-- 声明 切入面-->
<aop:aspect ref="cook">
<aop:before pointcut-ref="allMeMethod" method="cook"/>
</aop:aspect>
<!-- 声明 切入面-->
<aop:aspect ref="driver">
<aop:after pointcut-ref="allMeMethod" method="driver"/>
</aop:aspect>
</aop:config>

-------------------------------------------------------------------------------------
SSH集成
spring与hibernate集成:
流程图:
DataSource对象
|
|注入(DI)
|
    注入(DI)↓
 TransactionManager对象<--SessionFactory对象
| |
TxAdvice   |织入(AOP) |注入(DI)
实现└----------↓
Actions    ----->    Services    ----->    Dao
<--注入(DI) <--注入(DI)

步骤:
依赖包:
spring核心包(包括AOP相关依赖包)
hibernate核心包
数据源依赖包(依情况而定)
配置文件:
DateSource
SessionFactory
TransactionManager
TxAdvice
AOP 

applicationContext-common.xml
<context:component-scan base-package="" />

<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value=""></property>
<property name="dirverClassName" value=""></property>
<property name="username" value=""></property>
<property name="password" value=""></property>
</bean>

<!-- SessionFactory配置 -->
<bean id="sessionFactor" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 这个类是创建SessionFactory的类,不是SessionFactory类 -->
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />

<!-- 配置属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"></prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>

<!-- 映射文件列表 -->
<property name="mappingResources">
<list>

</list>
</property>
</bean>

<!-- TransactionManager配置 -->
<bean id="" class="org.springframework.orm.hibernate3.HibernateTransactionManager">\
<property name="sessionFactor" ref="sessionFactor" />
</bean>

<!-- 
txAdvice配置 
它专门用来控制事务管理
所以针对txAdvice,主要需要配置事务管理的有关特性
这些特性包括:
事务的隔离级别
事务的传播特性
timeout: 事务的超时时间
read-only: 是否只读(有利于提高查询效率)
抛出哪些异常应该进行回滚(默认抛出RuntimeException或其子类型进行回滚)
抛出哪些异常不应该回滚(默认抛出Exception及其子类型不会进行回滚)
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" isolation="" no-rollback-for="" propagation="" read-only="" rollback-for="" timeout=""/>
<tx:method name="add*" read-only="false" />
<tx:method name="update*" read-only="false" />
<tx:method name="del*" read-only="false" />
<tx:method name="find*" read-only="false" />
</tx:attributes>
</tx:advice>

<!-- AOP配置,拦截所有Service对象的方法调用 -->
<aop:config>
<aop:pointcut id="allServiceMethods" expression="excution(* com.hh.service.*(..))" />
<aop:advistor pointcut-ref="allServiceMethods" advice-ref="txAdvice"/>
</aop:config>

spring与Struts2集成:
把Action交给Spring去管理
BeanFactory对象需要在应用服务器启动的时候被创建(监听器)

依赖包:
Struts2相关的依赖包(去重)
Spring相关的依赖包
Struts2与spring集成的plugin依赖包
配置文件的编写
web.xml
Struts2自身的Filter配置
定义一个Listener,用于在应用服务器启动的时候,加载BeanFactory

web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPerepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>

<lister>
<lister-class>org.springframework.web.context.ContextLoaderListener</lister-class>
</lister>
-------------------------------------------------------------------------------------
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在淘宝主页搜不到我的店铺名怎么办 淘宝发货单号填到别人那去了怎么办 买家申请淘宝介入后同意退款怎么办 淘宝卖家手机版购买装修模块怎么办 天猫店铺和淘宝店铺想要装修怎么办 支付宝转账银行卡卡号错误怎么办 淘宝图片空间照片全部删除了怎么办 我把淘宝图片空间照片删除了怎么办 淘宝发布宝贝怎么没知道品牌怎么办 淘宝提前确认收货了怎么办已经发货 苹果支付安全提示问题忘记了怎么办 没有在规定日期交首付款怎么办 淘宝申请退款又不想退了怎么办 淘宝申请退款后又不想退了怎么办 申请退款后如果不想退了怎么办 世纪明德申请退款但不想退了怎么办 天猫申请换货商家不换怎么办 乐视手机刷机不想清除数据怎么办 捡的苹果手机刷机了要id怎么办 苹果6plus玩王者荣耀卡怎么办 华为荣耀3c的手机内存不足怎么办 红米4x玩王者荣耀卡怎么办 华为手机荣耀10一直重启怎么办 荣耀9青春版老自动重启怎么办 手机开不开机停在华为界面怎么办 华为荣耀9老是反复的重启怎么办 华为荣耀4x老是反复的重启怎么办 手机更新系统后开不了机怎么办 荣耀畅玩7x没有4g网络怎么办 华为4x数字解锁不对中怎么办 华为手机需要解锁后才能刷机怎么办 畅玩6x锁屏壁纸黑了怎么办 指纹密码解锁的手机解不开了怎么办 客户说物流太慢了要退货怎么办 天猫买的手机商家不给发票怎么办 天猫超市下单付款后缺货怎么办 淘宝卖家填写假的单号不发货怎么办 天猫商家72小时未发货怎么办 天猫精灵方糖不按顺序播放怎么办 在天猫购物已付款不发货怎么办 淘宝退货商家收到货不退款怎么办