SSH2 - Struts2+Hibernate+Spring项目小结2

来源:百度文库 编辑:神马文学网 时间:2024/04/24 14:26:24
1、在web.xml中加载Spring的配置文件

contextConfigLocation
classpath*:spring/*.xml





org.springframework.web.context.ContextLoaderListener




2、配置属性文件

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">


classpath*:config/ct-db-c3p0.properties
classpath*:config/mail.properties




3、配置数据源






















....bean.AddressBook
.......




org.hibernate.dialect.Oracle9Dialect
false
false
auto
true
org.hibernate.cache.EhCacheProvider
true










4、依赖注入配置




注:对beans的设置
...



5、Spring怎样注入Class(泛型类型对象)?
在事务配置时,将proxy-target-class设置为false即可。不能为true,因为要针对接口代理。
如:

6、Spring中采用构造方法注入注意要点:
在配置文件中显式书写注入的参数。如:
id="retJobService">


多个参数的构造函数示例
id="typeService">


false




7、Spring 2.0 结合AspectJ pointcut语法配置AOP详解
Spring参考文档 7.3 chema-based AOP support 提供了aspect,advisor,advide三种组装方法的解释,其中aspect是aspectJ原装,但稍复杂,




以上几句定义使用cglib创建Proxy, 为BookManager的save()和remove()加上lowStockBookFlushingAdvice,为 BookStockChecker.getLowStockBooks加上lowStockBookCachingAdvice.

execution(* *..BookManager.save(..))
第一颗* 代表ret-type-pattern 返回值可任意,
*..BookManager 代表任意Pacakge里的BookManager类。
如果写成com.xyz.service.* 则代表com.xyz.service下的任意类
com.xyz.service..* com.xyz.service则代表com.xyz.service及其子package下的任意类
save代表save方法,也可以写save* 代表saveBook()等方法
(..) 匹配0个参数或者多个参数的,任意类型
(x,..) 第一个参数的类型必须是X
(x,*,*,s,..) 匹配至少4个参数,第一个参数必须是x类型,第二个和第三个参数可以任意,第四个必须是s类型。

8、事务配置的不同形式
事务配置一:





pointcut="execution(* ....recruitment.service.impl..*.*(..)) || execution(* ....employeeinfo.service.impl..*Service.*(..))"
advice-ref="txAdvice" />













事务配置二:





pointcut="execution(* ....employeeinfo.service.impl..*Service.*(..))"
advice-ref="txAdvice" />

pointcut="execution(* ....recruitment.service.impl..*.*(..)) "
advice-ref="txAdviceRet" />





......


transaction-manager="transactionManager">

.........



事务配置三:(在java文件中配置)
需要单独配置事务的类名前配置事务开启
@Transactional(propagation = Propagation.SUPPORTS)
需要使用事务的方法前配置开启事务的信息
@Transactional(propagation = Propagation.REQUIRED)
如:
@Transactional(propagation = Propagation.SUPPORTS)
public class ApplierService extends BaseService implements
IApplierService {
@Transactional(propagation = Propagation.REQUIRED)
public void addOrModify(Applier t) {
applierDao.saveOrUpdate(toDBValue(t));
}
}

9、正确配置重写父类方法时事务
参阅:http://aumy2008.javaeye.com/admin/blogs/152928

10、spring中bean的作用域详解
bean属性scope的选取(prototype、request、session、global session):
class="....CompanyAction" scope="prototype"/>
s:datetimepicker录入框提交正常。
prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)
都会产生一个新的bean实例,相当一个new的操作,对于prototype作用域的bean,有一点非常重要,
那就是Spring不能对一个prototype bean的整个生命周期负责,
容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,
随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,
而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。
清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责。
(让Spring容器释放被singleton作用域bean占用资源的一种可行方式是,通过使用bean的后置处理器,
该处理器持有要被清除的bean的引用。)

request、session、global session使用的时候首先要在web.xml中做如下配置:
如果你使用的是Servlet 2.4及以上的web容器,那么你仅需要在web应用的XML声明文件web.xml中增加下述ContextListener即可:

org.springframework.web.context.request.RequestContextListener


class="....action.CompanyAction" scope="request"/>
s:datetimepicker录入框提交正常。

class="....action.CompanyAction" scope="session"/>
s:datetimepicker录入框不能正常提交。

class="....action.CompanyAction" scope="global session"/>
java.lang.IllegalStateException: No Scope registered for scope 'global session'
分析:global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。

Spring中bean的作用域相关网站:
http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch03s04.html