spring笔记

1,用spring托管对hibernate的操作,DAO包都继承自HibernateDAOSupport。用其中方法得到的HibernateTemplate
来进行对数据库的操作。

2,在MyEclipse中点击要查看的方法实现,用ctrl+T快捷键

3,在整合时。struts配置文件中的action中的class不用是具体的类,可以用spring提供的对象的别名。

4,在spring的配置中,action的配置有状态,要给其scope属性设置为prototype。因为默认singleton表示唯一的实例,则出现错误后会不断将错误消息加入fielderror中,故会出现错误重复。

5,spring的控制反转就是依赖注入,减少了类之间的耦合。但我现在只发现代码只是少了实例化类的语句,却多了很多配置文件,很是麻烦。

6,spring的事务管理
6.1spring+hibernate时。若为了使用hibernate的lazy属性,使用了
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
则会将声明事务为只读,则会将hibernate的flush模式设置为FLUSH_NEVER。这告诉hibernate避免和数据库进行不必要的对象同步,从而把所有更新延迟到事务的结束。可以自己在数据库的操作中重新配置事务,从而改变事物的readOnly属性。

6.2,在编程式事务中,最主要是将事务方法用TransactionTemplate.execute()方法来执行。

6.3 在aop声明式事务中,因为采用反射的方式,在测试的时候
ApplicationContext ac= new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml");
UserService impl = (UserService)ac.getBean("userService");
这个impl要为实际对象的接口,不能为实现的类,否则会报错java.lang.ClassCastException: $Proxy1。

7.spring的AOP
简单的一个AOP例子
<aop:config>
<aop:aspect ref="aspectBean">(创建作为切面的bean)
<aop:pointcut id="thispointcut" expression="execution(* package.class.method*.(..)) and target(bean)"/>(被切面的目标方法,也就是切入点,出发该方法就可触发切面)
<aop:before method="beforeMethod" pointcut-ref="thispointcut" arg-name="bean"/>
<aop:after-returning method="afterMethod" pointcut-ref="thispointcut" arg-name="bean"/>
</aop:aspect>
</aop:config>
在AOP应用中可以实现事务切面和安全。感觉和struts中的拦截器很相像。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值