Struts2+Hibernate+Spring项目小结
2013-08-13 21:27
197 查看
原文地址:http://www.wajava.com/bbs/viewthread_thread,51 1、在web.xml中加载Spring的配置文件 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/*.xml</param-value> </context-param> <!--Spring ApplicationContext 载入 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> 2、配置属性文件 <!-- 属性文件读入 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:config/ct-db-c3p0.properties</value> <value>classpath*:config/mail.properties</value> </list> </property> </bean> 3、配置数据源 <!-- 数据源定义,使用Apache c3p0 连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${....common.dbutils.c3p0.driver}" /> <property name="jdbcUrl" value="${....common.dbutils.c3p0.url}" /> <property name="user" value="${....common.dbutils.c3p0.user}" /> <property name="password" value="${....common.dbutils.c3p0.password}" /> <property name="maxStatementsPerConnection" value="${....common.dbutils.c3p0.maxStatementsPerConnection}"/> <property name="checkoutTimeout" value="${....common.dbutils.c3p0.checkoutTimeout}"/> <property name="initialPoolSize" value="${....common.dbutils.c3p0.initialPoolSize}"/> <property name="minPoolSize" value="${....common.dbutils.c3p0.minPoolSize}"/> <property name="maxPoolSize" value="${....common.dbutils.c3p0.maxPoolSize}"/> <property name="maxStatements" value="${....common.dbutils.c3p0.maxStatements}"/> <property name="acquireIncrement" value="${....common.dbutils.c3p0.acquireIncrement}"/> <property name="maxIdleTime" value="${....common.dbutils.c3p0.maxIdleTime}"/> </bean> <!--Hibernate SessionFatory--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <!--添加bean --> <value>....bean.AddressBook</value> ....... </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.generate_statistics">false</prop> <prop key="hibernate.connection.release_mode">auto</prop> <prop key="hibernate.autoReconnect">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <prop key="hibernate.cache.use_query_cache">true</prop> </props> </property> </bean> <!--Hibernate TransactionManager--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> 4、依赖注入配置 <bean id="companyAction" class="....action.CompanyAction" scope="request"></bean> <bean name="companyBaseModel" class="....CompanyBaseModel"></bean> <bean class="....CompanyService" id="companyService"></bean> <bean class="....CompanyDAO" id="companyDAO"></bean> 注:对beans的设置 <beans default-autowire="byName" default-lazy-init="true">...</beans> 5、Spring怎样注入Class<T>(泛型类型对象)? 在事务配置时,将proxy-target-class设置为false即可。不能为true,因为要针对接口代理。 如:<aop:config proxy-target-class="false"> 6、Spring中采用构造方法注入注意要点: 在配置文件中显式书写注入的参数。如: <bean class="....impl.JobService" id="retJobService"> <constructor-arg ref="retJobDao" /> </bean> 多个参数的构造函数示例 <bean class="....impl.TypeService" id="typeService"> <constructor-arg index="0" ref="typeDAO" /> <constructor-arg index="1" type="boolean"> <value>false</value> </constructor-arg> </bean> 7、Spring 2.0 结合AspectJ pointcut语法配置AOP详解 Spring参考文档 7.3 chema-based AOP support 提供了aspect,advisor,advide三种组装方法的解释,其中aspect是aspectJ原装,但稍复杂, <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* *..BookManager.save(..))||execution(* *..BookManager.remove(..))" advice-ref="lowStockBookFlushingAdvice"/> <aop:advisor pointcut="execution(* *..BookStockChecker.getLowStockBooks())" advice-ref="lowStockBookCachingAdvice"/> </aop:config> 以上几句定义使用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、事务配置的不同形式 事务配置一: <!-- 支持 @AspectJ 标记--> <aop:aspectj-autoproxy /> <!-- 以AspectJ方式 定义 AOP --> <aop:config proxy-target-class="false"> <aop:advisor pointcut="execution(* ....recruitment.service.impl..*.*(..)) || execution(* ....employeeinfo.service.impl..*Service.*(..))" advice-ref="txAdvice" /> </aop:config> <!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置. 默认的设置请参考Spring文档事务一章. --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="REQUIRED" /> <tx:method name="load*" read-only="true" propagation="REQUIRED" /> <tx:method name="find*" read-only="true" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> 事务配置二: <!-- 支持 @AspectJ 标记--> <aop:aspectj-autoproxy /> <!-- 以AspectJ方式 定义 AOP --> <aop:config proxy-target-class="false"> <aop:advisor pointcut="execution(* ....employeeinfo.service.impl..*Service.*(..))" advice-ref="txAdvice" /> <aop:advisor pointcut="execution(* ....recruitment.service.impl..*.*(..)) " advice-ref="txAdviceRet" /> </aop:config> <!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置. 默认的设置请参考Spring文档事务一章. --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> ...... </tx:attributes> </tx:advice> <tx:advice id="txAdviceRet" transaction-manager="transactionManager"> <tx:attributes> ......... </tx:attributes> </tx:advice> 事务配置三:(在java文件中配置) 需要单独配置事务的类名前配置事务开启 @Transactional(propagation = Propagation.SUPPORTS) 需要使用事务的方法前配置开启事务的信息 @Transactional(propagation = Propagation.REQUIRED) 如: @Transactional(propagation = Propagation.SUPPORTS) public class ApplierService extends BaseService<Applier> 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): <bean name="companyAction" 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即可: <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <bean name="companyAction" class="....action.CompanyAction" scope="request"/> s:datetimepicker录入框提交正常。 <bean name="companyAction" class="....action.CompanyAction" scope="session"/> s:datetimepicker录入框不能正常提交。 <bean name="companyAction" 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 |
相关文章推荐
- 项目中Spring注入报错小结
- 项目小结(hibernate + spring + webwork)
- 项目中Spring注入报错小结
- elasticsearch,spring boot,mybatis项目小结
- SpringMVC+Spring+mybatis+redis项目从零开始--redis缓存策略和配置实现
- springboot+springmvc+mybatis项目整合
- SpringBoot项目的云服务器部署
- Spring+SpringMVC+Mybatis+Maven搭建项目
- spring 集成的项目,Properties配置文件外移
- 启动SpringBoot项目报错:The specified JRE installation does not exist
- java web项目中Spring整合Redis的使用
- spring项目杂记5
- servlet3.0下构建spring web项目
- 2 springboot项目集成使用disconf,基于docker环境
- springboot+mybatis项目自动生成
- Axis2与Web项目整合及Axis2在Web项目中整合Spring
- 使用CXF+spring+restful创建一个web的接口项目
- maven构建spring web项目报错Java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherSer
- spring+hibernate的第一个项目成功后的总结
- 使用Intellij IDEA 开发 Spring-boot项目 热部署,自动部署