zoukankan      html  css  js  c++  java
  • 对于框架整合的总结(全注解)

      学习框架也有一段时间了,每天来来回回地也要在myeclipse或是eclipse上把三大框架整合几遍,今天做个小结,以方便以后复习。

      昨天刚下载了MyEclipse 10.7版本的,今天就拿过来试试手吧。MyEclipse这个工具就我个人使用感觉吧,集成了很多插件和类库确实方便很多,但是性能太差。原本就对于eclipse启动服务器的速度颇有微词,用了MyEclipse后,嚯,启动一个tomcat的间隙我可以泡杯茶了。

      开始。首先建好项目,建立一个数据连接,如果看不到DB Browser,可以在上面菜单栏window-->show view-->other-->MyEclipse Database-->选中DB Browser。然后new 一个数据库连接,这一步因人而异,你要选择什么数据库工具,添加对应的jar包,连接账号密码等等。

      接着添加能力:右键-->MyEclipse-->Add * Capabilities,对于这一步,我习惯性的是先添加SpringCapabilities-->然后添加HibernateCapabilities-->接着从DB Browser里选中要使用的数据表reverse成POJO类-->最后添加StrutsCapabilities,这些过程要注意的有几点:允许使用全注解,hibernate配置交给spring管理,添加StrutsCapabilities时要remove掉一个会导致重复的包。

      接下来重点在于几个配置文件的编写,下面我直接在代码段里解释说明:

      首先是web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app version="2.5" 
     3     xmlns="http://java.sun.com/xml/ns/javaee" 
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     7   <display-name></display-name>    
     8     
     9   <welcome-file-list>
    10     <welcome-file>index.jsp</welcome-file>
    11   </welcome-file-list>
    12   
    13   <!-- 定义了要装入的 Spring 配置文件 -->
    14   <context-param>
    15       <param-name>contextConfigLocation</param-name>
    16       <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    17   </context-param>
    18   
    19   <!-- 监听器 -->
    20   <listener>
    21       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    22   </listener>
    23   
    24   <!-- 核心控制器 -->
    25   <filter>
    26       <filter-name>struts2</filter-name>
    27       <filter-class>
    28     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    29       </filter-class>
    30   </filter>
    31   <filter-mapping>
    32       <filter-name>struts2</filter-name>
    33       <url-pattern>/*</url-pattern>
    34   </filter-mapping>
    35   
    36   </web-app>

      

      接下来是struts.xml的配置:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
     3 <struts>
     4     
     5     <!-- 默认所有的结果页面都存储在WEB-INF/content下 -->
     6     <constant name="struts.convention.result.path" value="/" />
     7     
     8     <!-- 默认继承的包 -->
     9     <constant name="struts.convention.default.parent.package" value="default-package" />
    10     
    11     <!-- 支持json,最近常用easyui,所以对json型返回结果的支持就必不可少了 -->
    12     <package name="default-package" extends="json-default">
    13      <!-- 基于paramsPrepareParamsStack,增加store interceptor保证actionMessage在redirect后不会丢失 -->
    14         <interceptors>
    15             <interceptor-stack name="crudStack">
    16                 <interceptor-ref name="store">
    17                     <param name="operationMode">AUTOMATIC</param>
    18                 </interceptor-ref>
    19                 <interceptor-ref name="paramsPrepareParamsStack" />
    20             </interceptor-stack>
    21         </interceptors>
    22 
    23         <default-interceptor-ref name="crudStack" />
    24     </package>
    25     
    26 </struts>    

      然后就是最重要也是最复杂的applicationContext.xml的配置:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans
     3     xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xmlns:p="http://www.springframework.org/schema/p"
     6     xmlns:context="http://www.springframework.org/schema/context"
     7     xmlns:aop="http://www.springframework.org/schema/aop"     
     8     xmlns:tx="http://www.springframework.org/schema/tx"
     9     
    10     xsi:schemaLocation="
    11         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    12         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    13         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    14         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd        
    15     ">
    16 
    17         <!-- 定义受环境影响易变的变量 -->
    18     <bean
    19     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    20         <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    21         <property name="ignoreResourceNotFound" value="true" />
    22         <property name="locations">
    23             <list>
    24                 <!-- 标准配置 -->
    25                 <value>classpath*:/application.properties</value>
    26             </list>
    27         </property>
    28     </bean>
    29     
    30 
    31     <bean id="dataSource"
    32         class="org.apache.commons.dbcp.BasicDataSource">
    33         <!-- 连接信息 -->
    34         <property name="driverClassName" value="${jdbc.driver}" />
    35         <property name="url" value="${jdbc.url}" />
    36         <property name="username" value="${jdbc.username}" />
    37         <property name="password" value="${jdbc.password}" />
    38 
    39         <!-- 连接池信息 -->
    40         <property name="maxIdle" value="${dbcp.maxIdle}" />
    41         <property name="maxActive" value="${dbcp.maxActive}" />
    42         <property name="defaultAutoCommit" value="false" />
    43         <property name="timeBetweenEvictionRunsMillis" value="3600000" />
    44         <property name="minEvictableIdleTimeMillis" value="3600000" />
    45     </bean>
    46 
    47     </bean>
    48     <bean id="sessionFactory"
    49         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    50         <property name="dataSource"><ref bean="dataSource" /></property>
    51         <property name="hibernateProperties">
    52             <props>
    53                 <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    54                 <prop key="hibernate.show_sql">true</prop>
    55                 <prop key="hibernate.hbm2ddl.auto">update</prop>
    56                 <prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop>
    57             </props>
    58         </property>
    59 
    60          <!-- 配置自动扫描包下的实体,也可使用annotatedClasses属性进行单个实体配置-->
    61         <property name="packagesToScan" value="com.ssh.entity"></property>
    62          
    63     </bean>
    64     
    65     <!-- 配置事务管理器 -->
    66     <bean id="transactionManager"
    67     class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    68         <property name="sessionFactory" ref="sessionFactory"></property>
    69     </bean>
    70     
    71     <!--加载事务管理器   -->
    72     <tx:annotation-driven transaction-manager="transactionManager"/>
    73 
    74     <!-- 使Spring关注Annotation -->
    75     <context:annotation-config />
    76 
    77     <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
    78     <context:component-scan base-package="com.ssh.daoImpl,com.ssh.serviceImpl" />
    79 
    80 </beans>


      还有就application.properties 和 log4j.properties 文件的配置,个人认为比较简单,代码就不贴了。

      

      在开发过程中,我还习惯给dao层建立一个基类BaseHibernateDao.java,主要作用是来提供session对象,方便后面各种使用,代码贴出来:

     1 public class BaseHibernateDao {
     2 
     3     @Autowired
     4     private SessionFactory sf;
     5     
     6     public Session getSession(){
     7         
     8         return sf.getCurrentSession();
     9     }
    10 }

      除此之外,最近还学到了为控制层建立一个基类BaseAction.java,主要作用是来提供ServletContext,HttpServletRequest,HttpServletResponse,HttpSession 对象:

     1 public class BaseAction extends ActionSupport implements ServletContextAware,ServletRequestAware,ServletResponseAware,SessionAware{
     2     
     3     private ServletContext servletContext;
     4     private HttpServletRequest httpServletRequest;
     5     private HttpServletResponse httpServletResponse;
     6     private HttpSession httpSession;
     7     private Map<String,Object> session;
     8     
     9     public void setServletContext(ServletContext servletContext) {
    10         // TODO Auto-generated method stub
    11         this.servletContext = servletContext;
    12     }
    13     
    14     public void setServletRequest(HttpServletRequest httpServletRequest) {
    15         // TODO Auto-generated method stub
    16         this.httpServletRequest = httpServletRequest;
    17     }
    18     
    19     public void setServletResponse(HttpServletResponse httpServletResponse) {
    20         // TODO Auto-generated method stub
    21         this.httpServletResponse = httpServletResponse;
    22     }
    23     
    24     public void setSession(Map<String, Object> session) {
    25         // TODO Auto-generated method stub
    26         this.session = session;
    27     }
    28 }

      代码多了看起来好烦躁, 虽然都是些简单的东西,但确实是这些天反复练习认真总结出来的。不管对自己还是对需要学习这些配置的人,总会有点用处吧。再附上一个容易让我自己混淆的问题:以前老是搞不清楚在service里注入一个dao,到底是注入dao接口还是dao实现类,只能一知半解地试。最近得到一个比较好的解答:Spring的注入基于接口实现,基于接口的方式一是可以使得各个组件松耦合,而且也可以轻松的替代某一组件。所以应该将组件引用改为接口方式。

      

      明后两天准备研究一下Hibernate的主键生成策略,在这个问题上还有一些不能理解的东西,希望深入了解能够找到答案并在这里分享出来。

  • 相关阅读:
    17. Letter Combinations of a Phone Number java solutions
    43. Multiply Strings java solutions
    3. Longest Substring Without Repeating Characters java solutions
    51. N-Queens java solutions
    74. Search a 2D Matrix java solutions
    48. Rotate Image java solutions
    64. Minimum Path Sum java solutions
    leetcode : Container With Most Water
    leetcode : reverse integer
    leetcode : ZigZag Conversion
  • 原文地址:https://www.cnblogs.com/I0000/p/3529149.html
Copyright © 2011-2022 走看看