spring学习笔记

-----------------------------------------------------------------
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.jar aspectjrt.jar aspectjweaver.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>
-------------------------------------------------------------------------------------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值