国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常...

一 開(kāi)發(fā)環(huán)境

1、動(dòng)態(tài)web工程

2、部分依賴(lài)

 

java代碼:
  1. hibernate-release-4.1.0.Final.zip  
  2. hibernate-validator-4.2.0.Final.jar  
  3. spring-framework-3.1.1.RELEASE-with-docs.zip  
  4. proxool-0.9.1.jar  
  5. log4j 1.2.16  
  6. slf4j -1.6.1  
  7. mysql-connector-java-5.1.10.jar  
  8. hamcrest 1.3.0RC2  
  9. ehcache 2.4.3  

 

3、為了方便學(xué)習(xí),暫沒(méi)有使用maven構(gòu)建工程

 

二 工程主要包括內(nèi)容

1、springMVC + spring3.1.1 + hibernate4.1.0集成

2、通用DAO層 和 Service層

3、二級(jí)緩存 Ehcache

4、REST風(fēng)格的表現(xiàn)層

5、通用分頁(yè)(兩個(gè)版本)

5.1、首頁(yè) 上一頁(yè),下一頁(yè) 尾頁(yè) 跳轉(zhuǎn)

5.2、上一頁(yè) 1 2 3 4 5 下一頁(yè)

6、數(shù)據(jù)庫(kù)連接池采用proxool

7、spring集成測(cè)試    

8、表現(xiàn)層的 java validator框架驗(yàn)證(采用hibernate-validator-4.2.0實(shí)現(xiàn))

9、視圖采用JSP,并進(jìn)行組件化分離

 

三 TODO LIST  將本項(xiàng)目做成腳手架方便以后新項(xiàng)目查詢

1、Service層進(jìn)行AOP緩存(緩存使用Memcached實(shí)現(xiàn))

2、單元測(cè)試(把常見(jiàn)的樁測(cè)試、偽實(shí)現(xiàn)、模擬對(duì)象演示一遍 區(qū)別集成測(cè)試)

3、監(jiān)控功能

后臺(tái)查詢hibernate二級(jí)緩存 hit/miss率功能      

   后臺(tái)查詢當(dāng)前服務(wù)器狀態(tài)功能(如 線程信息、服務(wù)器相關(guān)信息)

4、spring RPC功能

5、spring集成 quartz 進(jìn)行任務(wù)調(diào)度

6、spring集成 java mail進(jìn)行郵件發(fā)送

7、DAO層將各種常用框架集成進(jìn)來(lái)(方便查詢)

8、把工作中經(jīng)常用的東西 融合進(jìn)去,作為腳手架,方便以后查詢

 

四 集成重點(diǎn)及常見(jiàn)問(wèn)題

1、spring-config.xml 配置文件:

1.1、該配置文件只加載除表現(xiàn)層之外的所有bean,因此需要如下配置:

 

java代碼:
  1. <context:component-scan base-package="cn.javass">  
  2.     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  3. </context:component-scan>  

通過(guò)exclude-filter 把所有 @Controller注解的表現(xiàn)層控制器組件排除

   

 

1.2、國(guó)際化消息文件配置

 

java代碼:
  1. <!-- 國(guó)際化的消息資源文件 -->  
  2.     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
  3.         <property name="basenames">  
  4.             <list>  
  5.                 <!-- 在web環(huán)境中一定要定位到classpath 否則默認(rèn)到當(dāng)前web應(yīng)用下找  -->  
  6.                 <value>classpath:messages</value>  
  7.             </list>  
  8.         </property>  
  9.         <property name="defaultEncoding" value="UTF-8"/>  
  10.         <property name="cacheSeconds" value="60"/>  
  11.     </bean>  

此處basenames內(nèi)一定是 classpath:messages ,如果你寫(xiě)出“messages”,將會(huì)到你的web應(yīng)用的根下找 即你的messages.properties一定在 web應(yīng)用/messages.propertis。

 

1.3、hibernate的sessionFactory配置 需要使用org.springframework.orm.hibernate4.LocalSessionFactoryBean,其他都是類(lèi)似的,具體看源代碼。

 

1.4、<aop:aspectj-autoproxy expose-proxy="true"/> 實(shí)現(xiàn)@AspectJ注解的,默認(rèn)使用AnnotationAwareAspectJAutoProxyCreator進(jìn)行AOP代理,它是BeanPostProcessor的子類(lèi),在容器啟動(dòng)時(shí)Bean初始化開(kāi)始和結(jié)束時(shí)調(diào)用進(jìn)行AOP代理的創(chuàng)建,因此只對(duì)當(dāng)容器啟動(dòng)時(shí)有效,使用時(shí)注意此處。

 

1.5、聲明式容器管理事務(wù)

建議使用聲明式容器管理事務(wù),而不建議使用注解容器管理事務(wù)(雖然簡(jiǎn)單),但太分布式了,采用聲明式容器管理事務(wù)一般只對(duì)service層進(jìn)行處理。

 

java代碼:
  1. <tx:advice id="txAdvice" transaction-manager="txManager">  
  2.     <tx:attributes>  
  3.         <tx:method name="save*" propagation="REQUIRED" />  
  4.         <tx:method name="add*" propagation="REQUIRED" />  
  5.         <tx:method name="create*" propagation="REQUIRED" />  
  6.         <tx:method name="insert*" propagation="REQUIRED" />  
  7.         <tx:method name="update*" propagation="REQUIRED" />  
  8.         <tx:method name="merge*" propagation="REQUIRED" />  
  9.         <tx:method name="del*" propagation="REQUIRED" />  
  10.         <tx:method name="remove*" propagation="REQUIRED" />  
  11.         <tx:method name="put*" propagation="REQUIRED" />  
  12.         <tx:method name="use*" propagation="REQUIRED"/>  
  13.         <!--hibernate4必須配置為開(kāi)啟事務(wù) 否則 getCurrentSession()獲取不到-->  
  14.         <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
  15.         <tx:method name="count*" propagation="REQUIRED" read-only="true" />  
  16.         <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
  17.         <tx:method name="list*" propagation="REQUIRED" read-only="true" />  
  18.         <tx:method name="*" read-only="true" />  
  19.     </tx:attributes>  
  20. </tx:advice>  
  21. <aop:config expose-proxy="true">  
  22.     <!-- 只對(duì)業(yè)務(wù)邏輯層實(shí)施事務(wù) -->  
  23.     <aop:pointcut id="txPointcut" expression="execution(* cn.javass..service..*.*(..))" />  
  24.     <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>  
  25. </aop:config>  

 

此處一定注意 使用 hibernate4,在不使用OpenSessionInView模式時(shí),在使用getCurrentSession()時(shí)會(huì)有如下問(wèn)題:

 

當(dāng)有一個(gè)方法list 傳播行為為Supports,當(dāng)在另一個(gè)方法getPage()(無(wú)事務(wù))調(diào)用list方法時(shí)會(huì)拋出org.hibernate.HibernateException: No Session found for current thread 異常。

這是因?yàn)間etCurrentSession()在沒(méi)有session的情況下不會(huì)自動(dòng)創(chuàng)建一個(gè),不知道這是不是Spring3.1實(shí)現(xiàn)的bug,歡迎大家討論下。

 

因此最好的解決方案是使用REQUIRED的傳播行為。

 

 

二、spring-servlet.xml

2.1、表現(xiàn)層配置文件,只應(yīng)加裝表現(xiàn)層Bean,否則可能引起問(wèn)題。

 

java代碼:
  1. <!-- 開(kāi)啟controller注解支持 -->  
  2. <!-- 注:如果base-package=cn.javass 則注解事務(wù)不起作用-->  
  3. <context:component-scan base-package="cn.javass.demo.web.controller">  
  4.     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  5. </context:component-scan>  

 

此處只應(yīng)該加載表現(xiàn)層組件,如果此處還加載dao層或service層的bean會(huì)將之前容器加載的替換掉,而且此處不會(huì)進(jìn)行AOP織入,所以會(huì)造成AOP失效問(wèn)題(如事務(wù)不起作用),再回頭看我們的1.4討論的。

 

 

2.2、<mvc:view-controller path="/" view-name="forward:/index"/> 表示當(dāng)訪問(wèn)主頁(yè)時(shí)自動(dòng)轉(zhuǎn)發(fā)到index控制器。

 

 

2.3、靜態(tài)資源映射

 

java代碼:
  1. <!-- 當(dāng)在web.xml 中   DispatcherServlet使用     <url-pattern>/</url-pattern> 映射時(shí),能映射靜態(tài)資源 -->  
  2. <mvc:default-servlet-handler/>  
  3. <!-- 靜態(tài)資源映射 -->  
  4. <mvc:resources mapping="/images/**" location="/WEB-INF/images/" />  
  5. <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />  
  6. <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />  

以上是配置文件部分,接下來(lái)來(lái)看具體代碼。

 

 

三、通用DAOHibernate4實(shí)現(xiàn)

為了減少各模塊實(shí)現(xiàn)的代碼量,實(shí)際工作時(shí)都會(huì)有通用DAO層實(shí)現(xiàn),以下是部分核心代碼:

 

java代碼:
  1. public abstract class BaseHibernateDao<M extends java.io.Serializable, PK extends java.io.Serializable> implements IBaseDao<M, PK> {  
  2.    
  3.     protected static final Logger LOGGER = LoggerFactory.getLogger(BaseHibernateDao.class);  
  4.    
  5.     private final Class<M> entityClass;  
  6.     private final String HQL_LIST_ALL;  
  7.     private final String HQL_COUNT_ALL;  
  8.     private final String HQL_OPTIMIZE_PRE_LIST_ALL;  
  9.     private final String HQL_OPTIMIZE_NEXT_LIST_ALL;  
  10.     private String pkName = null;  
  11.    
  12.     @SuppressWarnings("unchecked")  
  13.     public BaseHibernateDao() {  
  14.         this.entityClass = (Class<M>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];  
  15.         Field[] fields = this.entityClass.getDeclaredFields();  
  16.         for(Field f : fields) {  
  17.             if(f.isAnnotationPresent(Id.class)) {  
  18.                 this.pkName = f.getName();  
  19.             }  
  20.         }  
  21.          
  22.         Assert.notNull(pkName);  
  23.         //TODO @Entity name not null  
  24.         HQL_LIST_ALL = "from " + this.entityClass.getSimpleName() + " order by " + pkName + " desc";  
  25.         HQL_OPTIMIZE_PRE_LIST_ALL = "from " + this.entityClass.getSimpleName() + " where " + pkName + " > ? order by " + pkName + " asc";  
  26.         HQL_OPTIMIZE_NEXT_LIST_ALL = "from " + this.entityClass.getSimpleName() + " where " + pkName + " < ? order by " + pkName + " desc";  
  27.         HQL_COUNT_ALL = " select count(*) from " + this.entityClass.getSimpleName();  
  28.     }  
  29.          
  30.     @Autowired  
  31.     @Qualifier("sessionFactory")  
  32.     private SessionFactory sessionFactory;  
  33.    
  34.     public Session getSession() {  
  35.         //事務(wù)必須是開(kāi)啟的,否則獲取不到  
  36.         return sessionFactory.getCurrentSession();  
  37.     }  
  38. ……  
  39. }  

Spring3.1集成Hibernate4不再需要HibernateDaoSupport和HibernateTemplate了,直接使用原生API即可。

 

 

四、通用Service層代碼 此處省略,看源代碼,有了通用代碼后CURD就不用再寫(xiě)了。

 

java代碼:
  1. @Service("UserService")  
  2. public class UserServiceImpl extends BaseService<UserModel, Integer> implements UserService {  
  3.    
  4.     private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);  
  5.    
  6.     private UserDao userDao;  
  7.    
  8.     @Autowired  
  9.     @Qualifier("UserDao")  
  10.     @Override  
  11.     public void setBaseDao(IBaseDao<UserModel, Integer> userDao) {  
  12.         this.baseDao = userDao;  
  13.         this.userDao = (UserDao) userDao;  
  14.     }  
  15.      
  16.    
  17.    
  18.     @Override  
  19.     public Page<UserModel> query(int pn, int pageSize, UserQueryModel command) {  
  20.         return PageUtil.getPage(userDao.countQuery(command) ,pn, userDao.query(pn, pageSize, command), pageSize);  
  21.     }  
  22. }  
  23.    

 

 

 

五、表現(xiàn)層 Controller實(shí)現(xiàn)

采用SpringMVC支持的REST風(fēng)格實(shí)現(xiàn),具體看代碼,此處我們使用了java Validator框架 來(lái)進(jìn)行 表現(xiàn)層數(shù)據(jù)驗(yàn)證

 

在Model實(shí)現(xiàn)上加驗(yàn)證注解

 

 

java代碼:
  1. @Pattern(regexp = "[A-Za-z0-9]{5,20}", message = "{username.illegal}"//java validator驗(yàn)證(用戶名字母數(shù)字組成,長(zhǎng)度為5-10)  
  2. private String username;  
  3.   
  4. @NotEmpty(message = "{email.illegal}")  
  5. @Email(message = "{email.illegal}"//錯(cuò)誤消息會(huì)自動(dòng)到MessageSource中查找  
  6. private String email;  
  7.   
  8. @Pattern(regexp = "[A-Za-z0-9]{5,20}", message = "{password.illegal}")  
  9. private String password;  
  10.   
  11. @DateFormat( message="{register.date.error}")//自定義的驗(yàn)證器  
  12. private Date registerDate;  

 

在Controller中相應(yīng)方法的需要驗(yàn)證的參數(shù)上加@Valid即可

 

java代碼:
  1. @RequestMapping(value = "/user/add", method = {RequestMethod.POST})  
  2. public String add(Model model, @ModelAttribute("command"@Valid UserModel command, BindingResult result)  

 

 

六、Spring集成測(cè)試

使用Spring集成測(cè)試能很方便的進(jìn)行Bean的測(cè)試,而且使用@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)能自動(dòng)回滾事務(wù),清理測(cè)試前后狀態(tài)。

 

java代碼:
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = {"classpath:spring-config.xml"})  
  3. @Transactional  
  4. @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)  
  5. public class UserServiceTest {  
  6.      
  7.     AtomicInteger counter = new AtomicInteger();  
  8.      
  9.     @Autowired  
  10.     private UserService userService;  
  11.     ……    
  12. }  

 

其他部分請(qǐng)直接看源碼,歡迎大家討論。

 

 

 

 

 

 

 

補(bǔ)充spring3.1.1源代碼分析當(dāng) 傳播行為為 Support時(shí)報(bào) org.hibernate.HibernateException: No Session found for current thread 異常:


spring3.1開(kāi)始 不提供(沒(méi)有這個(gè)東西了)Hibernate4的 DaoSupport和Template,,而是直接使用原生的Hibernate4 API 


如在 Hibernate3中 HibernateTemplate中有如下代碼 

 

Java代碼   
  1. protected Session getSession() {  
  2.         if (isAlwaysUseNewSession()) {  
  3.             return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());  
  4.         }  
  5.         else if (isAllowCreate()) {//默認(rèn)是true,也就是即使你的傳播行為是Supports也一定會(huì)有session存在的  
  6.             return SessionFactoryUtils.getSession(  
  7.                     getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());  
  8.         }  
  9.         else if (SessionFactoryUtils.hasTransactionalSession(getSessionFactory())) {  
  10.             return SessionFactoryUtils.getSession(getSessionFactory(), false);  
  11.         }  
  12.         else {  
  13.             try {  
  14.                 return getSessionFactory().getCurrentSession();  
  15.             }  
  16.             catch (HibernateException ex) {  
  17.                 throw new DataAccessResourceFailureException("Could not obtain current Hibernate Session", ex);  
  18.             }  
  19.         }  
  20.     }  


但我們使用的是Hibernate4原生API,使用SpringSessionContext獲取session,而這個(gè)isAllowCreate選項(xiàng)默認(rèn)為false 

Java代碼   
  1. /** 
  2.  * Retrieve the Spring-managed Session for the current thread, if any. 
  3.  */  
  4. public Session currentSession() throws HibernateException {  
  5.     try {  
  6.         return (org.hibernate.classic.Session) SessionFactoryUtils.doGetSession(this.sessionFactory, false);//最后的false即是  
  7.     }  
  8.     catch (IllegalStateException ex) {  
  9.         throw new HibernateException(ex.getMessage());  
  10.     }  
  11. }  




SessionFactoryUtils類(lèi) 

Java代碼   
  1. public static Session doGetSession(SessionFactory sessionFactory, boolean allowCreate)  
  2.         throws HibernateException, IllegalStateException {  
  3.   
  4.     return doGetSession(sessionFactory, nullnull, allowCreate);  
  5. }  


可否認(rèn)為這是集成Hibernate4的bug,或者采用OpenSessionInView模式解決或使用Required傳播行為。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
主題:通過(guò)hibernate攔截器實(shí)現(xiàn)自定義分表策略
Spring3整合Hibernate3
回調(diào) Spring Hibernate DAO 持久層開(kāi)發(fā),
junit測(cè)試之hiberante延遲加載問(wèn)題(Could not obtain transaction-synchronized Session for current threa
struts2 hibernate spring配置管理(一)——配置文件 - Strut...
Spring對(duì)hibernate配置文件hibernate.cfg.xml的集成,來(lái)取代hibernate.cfg.xml的配置
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服