Java三大框架搭建

嗯,分享一下Java的三大框架(struts、spring、Hibernate) 的搭建,雖然很簡單,但是我覺得可能還是有很多新手需要,希望可以留個參考吧!
環境什麼的肯定大家都搭建好了吧,哈哈(>^ω^<)

先給大家梳理一下順序↓↓↓↓↓
1.導入各個框架的jar包,加入支持–簡稱導包
2.創建spring 的配置文件(創建完了就寫裏面的內容吧)–簡稱春天的配置╭(′▽`)╯
3.寫struts 的配置文件–簡稱視圖與服務的連接點⊙﹏⊙‖∣好像太長了…………
4.最後的最後 配置web.xml 文件
直接看代碼:
這是applicationContext.xml 放在src目錄下

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
    ">

<!-- 定義數據源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"  />
        <property name="username" value="test" />
        <property name="password" value="456" />
        <property name="defaultAutoCommit" value="false" /><!-- 自動提交事務屬性爲true -->
    </bean>

<!-- 定義SessionFactory Bean -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" /> <!-- 注入定義好的數據源 -->
        <property name="hibernateProperties" ><!-- 添加hibernate配置參數 -->
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
                <!-- <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop> -->
            </props>
        </property>
        <property name="mappingDirectoryLocations" ><!-- 添加對象關係映射文件 -->
            <list>
                <value>classpath:cn/jbit/test/entity/</value>
            </list>
        </property>
    </bean>

    <!-- 定義dao -->
    <bean id="accountDao" class="cn.jbit.test.dao.impl.AccountDaoImpl" >
         <!-- autowire="byName" -->
         <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 定義service -->
    <bean id="accountService" class="cn.jbit.test.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"  />
    </bean>

    <!-- 定義事務管理器 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" /><!-- 注入sessionFactory -->
    </bean>

    <!-- 定義事務增強,指定事務管理器 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="submit*" propagation="REQUIRED" />
            <tx:method name="register*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>

            <tx:method name="find*" read-only="true" /><!-- 事務只讀true,能提高事務處理性能 -->
            <tx:method name="search*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="do*" propagation="REQUIRED"/>
            <!-- <tx:method name="*" propagation="REQUIRED" read-only="true"/> -->
        </tx:attributes>
    </tx:advice>

    <aop:config>
    <!-- 定義切面 -->
        <aop:pointcut expression="execution(* cn.jbit.test.dao..*.*(..))" id="daoMethod"/>
        <!-- 把切入點和事務增強關聯 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod"/>
    </aop:config>
</beans>

下面是struts.xml 的文件,也是放在src目錄下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--  -->
<constant name="struts.ui.theme" value="simple" />
<package name="p1" namespace="/" extends="struts-default">


    <!-- 定義全局結果 -->
    <!-- <global-results>
        <result name="login" type="redirect">/login.jsp</result>
    </global-results> -->

  <action name="get" class="cn.jbit.test.action.AccountAction" method="get">
      <result name="success" >/get.jsp</result>
  </action>
  <action name="register" class="cn.jbit.test.action.UserAction" method="register">
    <result name="success">/registerSuccess.jsp</result>
    <result name="input">/register.jsp</result>
  </action>
</package>
</struts>    

4.這是web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsSpringHib</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>

  <!-- 告訴spring的監聽器spring主配置文件的位置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>  
  <!-- 配置一個監聽器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 配置OpenSessionInViewFilter過濾器 -->
  <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern><!-- *.action -->
  </filter-mapping>

    <!-- 配置struts核心控制器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><!-- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter -->
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


</web-app>

最後呢,我覺得吧,三大框架最難的的地方就是怎麼整合了,至於什麼jsp 頁面啊,Action 控制器啊,或許還有什麼Service層,還有DAO層啊,這些在applicationContext.xml 文件中已經充分表現出來了,需要的自己寫就OK了。
spring 的配置文件也是可以拆開寫的,隨着需要項目規模的增加,需要交給spring 容器管理的類肯定不少,對不對,所以spring 的配置文件肯定很臃腫 對不對,這樣不利於代碼維護啊,所以需要拆開,至於怎麼拆,,,,,你猜猜●▽●,不要打我,說笑的,下次更加精彩

其實我想直接傳源代碼的,這樣就可以省事兒了,但是不知道怎麼傳,誰知道滴?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章