Struts2.2.1+Hibernate3.6+spring3.1注解开发学习笔记

    技术2022-05-19  25

    1、开发环境

        JDK1.5.0_11

        Eclipse 3.3.1.1

        Tomcat5.5.32

    Oracle 10g

      JDK的版本不得低于1.5,因为用到了很多1.5版才支持的新特性。Tomcat请不要低于我所用的版本,因为我没在其它的版本上进行测试。这一章就主要来说说jar包的选择。

    2、所有jar包

     

     

    分开讲讲jar包的作用

    3、Struts2的jar包

     

     

    commons-logging、freemarker、ognl、struts2-core、xwork这5个还是struts2的核心包

    commons-fileupload和javassist也要加上不然启动会报错commons-io可以不用加看自己需要。

    struts2也采取注解的方式,所以用到了struts2-convention-plugin-2.1.6.jar这个插件。因为要与spring整合,所以struts2-spring-plugin-2.1.6.jar也必不可少。

    4、Hibernate包

     

     

    这里的javassist跟struts2的jar可能会冲突 所以删掉

    后四个jar是hibernate注解开发用的,上边的是hibernate的lib/required目录下的

    5、Spring的jar包

     

     

    spring包都被分开了,可以只用需要的包

    6、数据库、连接池

     

     

    使用DBCP连接池来管理数据源。用的是oracle数据库ojdbc包是驱动包

    1、所有配置文件列表Jdbc.properties

    Log4j.properties

    applicationContext

    web.xml

    由于struts也使用注解方式进行开发,所有没有struts.xml文件

    2、jdbc.propertiesjdbc.driver=oracle.jdbc.driver.OracleDriver

    jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl

    jdbc.username=scott

    jdbc.password=gg

    hibernate.dialect=org.hibernate.dialect.OracleDialect

    hibernate.show_sql=true

    单独把jdbc参数配置在一个文件里是为了以后更换数据库、数据移植方便

    3、applicationContext<?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:jee="http://www.springframework.org/schema/jee"

        xmlns:tx="http://www.springframework.org/schema/tx"

        xmlns:context="http://www.springframework.org/schema/context"

        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.0.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

        http://www.springframework.org/schema/jee

        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.0.xsd"

        default-autowire="byName" default-lazy-init="true">

        <!-- 支持注解 -->

        <context:annotation-config />

        <!-- 使用annotation 自动注册bean, @Repository,@Service都是注解,前者表示持久层,后者表示业务层. 并保证@Required,@Autowired的属性被注入. 有了它,我们将不用再去写那繁琐的<bean id="" class="" />  -->

        <context:component-scan base-package="cn.dykj.example" />

     

        <!-- PropertyPlaceholderConfigurer这个类就是读取jdbc. properties文件,并将它们设置到这个类的属性中。然后再将下面数据源配置中定义的这些${jdbc.driver}、${jdbc.url}字符串换成属性文件中相同名称的值。${}这种写法,是类里面方法解析用的,网上都说这是叫占位符,我看了源代码的,其实是把它们当成字符串截取前后的特殊字符,再根据里面定义的名称找属性文件中对应的值。所以这个类只能读取properties格式的文件,你如果还有其它需要加入的属性文件,可以在list之间加入,写在value标签里面。 -->

        <bean id="jdbc"     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

            <property name="locations">

                <list>

                    <value>classpath:jdbc.properties</value>

                </list>

            </property>

        </bean>

        <bean id="dataSource"

            class="org.apache.commons.dbcp.BasicDataSource"

            destroy-method="close">

            <property name="driverClassName" value="${jdbc.driver}" />

            <property name="url" value="${jdbc.url}" />

            <property name="username" value="${jdbc.username}" />

            <property name="password" value="${jdbc.password}" />

        </bean>

     

        <bean id="sessionFactory"       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

            <property name="dataSource" ref="dataSource" />

            <property name="hibernateProperties">

                <props>

                    <prop key="hibernate.dialect">

                        ${hibernate.dialect}

                    </prop>

                    <prop key="hibernate.show_sql">

                        ${hibernate.show_sql}

                    </prop>

                </props>

            </property>

    <!-- packagesToScan属性,它是根据value中定义的路径来扫描其下所有的注解实体类。注意此处的value值最后没有.* 如果加上的话,那么服务器启动和程序运行时都不会报错,但是当你的代码需要用到这个类的时候,就会出现异常,提示你找不到实体。 -->

            <property name="packagesToScan" value="cn.dykj.example.pojo" />

        </bean>

        <bean id="transactionManager"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

            <property name="sessionFactory" ref="sessionFactory" />

        </bean>

     

        <!--     这是事务定义,而且是使用注解方式定义事务(@Transactional),proxy-target-class="true"表示采用动态代理类来管理事务,如果是false表示采用接口代理来管理事务(默认值为false)。 -->

            <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

        <!--     上面这个就是使用配置式来定义事务,两种方式的区别主要是,注解式只用写那么一句话,然后在业务类或方法中加入@Transactional这个注解标记,就完成事务声明,不过对于每个业务类都需要在类或方法中加入这些标记。而配置式声明,就是不用加这些标记,只要你的方法名称命名比较统一,就可以像上面这样定义事务规范,然后在aop标签中定义切入点与执行通知就行了。我感觉如果业务逻辑不是太复杂的情况,配置式会比较简单,而且修改起来也方便,这两种方式我都写出来了,至于用哪一种,由你们自己决定。

    <aop:config >

        <aop:pointcut id="transactionPointcut" expression="execution(* cn.dykj.example.service..*Manager.*(..))"/>

        <aop:pointcut id="transactionDao" expression = "execution(* cn.dykj.example.dao..*Dao.*(..))"/>

        <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>

        <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionDao"/>

    </aop:config>

     

         <tx:advice id="txAdvice" transaction-manager="transactionManager">

            <tx:attributes>

                <tx:method name="save*" propagation="REQUIRED" />

                <tx:method name="insert*" propagation="REQUIRED" />

                <tx:method name="add*" propagation="REQUIRED" />

                <tx:method name="update*" propagation="REQUIRED" />

                <tx:method name="delete*" propagation="REQUIRED" />

                <tx:method name="del*" propagation="REQUIRED" />

                <tx:method name="*" read-only="true" />

            </tx:attributes>

        </tx:advice>

        -->

    </beans>

    4、web.xml<?xml version="1.0" encoding="UTF-8"?>

    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

        <display-name>S2S3H3</display-name>

        <!-- 加载spring配置文件 -->

        <context-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:applicationContext.xml</param-value>

        </context-param>

        <!-- 编码过滤器 forceEncoding这个参数,把它设置为true表示不管请求中的编码是什么格式,都将强制采用encoding中设置的编码方式。另外对于响应也将按照encoding指定的编码进行设置。 -->

        <filter>

            <filter-name>encoding</filter-name>

            <filter-class>

                org.springframework.web.filter.CharacterEncodingFilter

            </filter-class>

            <init-param>

                <param-name>encoding</param-name>

                <param-value>UTF-8</param-value>

            </init-param>

            <init-param>

                <param-name>forceEncoding</param-name>

                <param-value>true</param-value>

            </init-param>

        </filter>

        <!-- struts2加载 -->

        <filter>

            <filter-name>struts2</filter-name>

            <filter-class>          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

            </filter-class>

            <!-- 开发时间为true,应用时为false -->

            <init-param>

                <param-name>struts.devMode</param-name>

                <param-value>true</param-value>

            </init-param>

            <!-- struts2自动扫描此包下所有类为action类 ,struts2注解开发必须用 -->

            <init-param>

                <param-name>actionPackages</param-name>

                <param-value>cn.dykj.example.action</param-value>

            </init-param>

        </filter>

        <filter-mapping>

            <filter-name>struts2</filter-name>

            <url-pattern>/*</url-pattern>

        </filter-mapping>

        <!-- 配置spring监听器,读取spring配置文件 -->

        <listener>

            <listener-class>

                org.springframework.web.context.ContextLoaderListener

            </listener-class>

        </listener>

        <welcome-file-list>

            <welcome-file>index.html</welcome-file>

            <welcome-file>index.htm</welcome-file>

            <welcome-file>index.jsp</welcome-file>

        </welcome-file-list>

    </web-app>

     

     


    最新回复(0)