欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring3+Struts2+SpringJDBC注解实现 博客分类: spring spring 

程序员文章站 2024-03-22 10:11:28
...
   个人有点侧重于性能,于是就想着把hibernate替换为JDBC,但是J2EE的jdbc还要额外的控制事物,就换成了spring容器封装好的jdbc,有不对的地方还请大家指出来,小弟在此谢过了。
   这里有两个配置文件web.xml和ApplicationContext.xml,至于Struts2的配置文件很简单就没有拿出来,还是按照常规的MVC模式。我写的大致是dao,service,action,entity。

这是ApplicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " default-autowire="byName" default-lazy-init="true">

<!-- 隐式地向 Spring容器注册:AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor这 4 个BeanPostProcessor注解处理器。-->
<context:annotation-config/>

<!-- 扫描包下的类,含子包 -->
<context:component-scan base-package="com"/>

<!-- 基于命名空间的配置加载properties配置文件,Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉。写多个请参照下面写法-->
<context:property-placeholder location="classpath*:*.properties"/>

<!-- 使用apache自己的dbcp数据源产品 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
    <property name="url" value="${url}"></property>
    <property name="username" value="${username}"></property>
    <property name="password" value="${password}"></property>
    <property name="maxActive" value="${maxActive}"></property>
    <property name="maxIdle" value="${maxIdle}"></property>
    <property name="minIdle" value="${minIdle}"></property>  
    <property name="initialSize" value="${initialSize}"></property>
</bean>

<!-- 如果数据访问层使用jdbc来实现,请添加spring封装的jdbcTemplate模板,使用依赖注入特性注入数据源 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 如果数据访问层使用hibernate实现,请添加如下以用于读取hibernate配置文件作为数据源-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<!-- 如果使用了@Transactional注解,请添加如下的JDBC事务管理器声明-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 如果使用了AOP,请使用如下事物管理器声明 -->

<tx:advice id="……" transaction-manager="……"> 
    <tx:attributes> 
            <tx:method name="*" 
                               propagation="REQUIRED" 
                               isolation="DEFAULT" 
                               timeout="-1" 
                               read-only="true" 
                               no-rollback-for=""  
                               rollback-for="java.lang.Exception"/> 
        </tx:attributes> 
    </tx:advice> 
      
    <tx:advice id="txAdvice" transaction-manager="txManager"> 
    <tx:attributes> 
               <tx:method name="save*" propagation="REQUIRED" /> 
               <tx:method name="add*" propagation="REQUIRED" /> 
               <tx:method name="create*" propagation="REQUIRED" /> 
               <tx:method name="insert*" propagation="REQUIRED" /> 
               <tx:method name="update*" propagation="REQUIRED" /> 
               <tx:method name="merge*" propagation="REQUIRED" /> 
               <tx:method name="del*" propagation="REQUIRED" /> 
               <tx:method name="remove*" propagation="REQUIRED" /> 
               <tx:method name="put*" propagation="REQUIRED" /> 
               <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> 
               <tx:method name="count*" propagation="SUPPORTS" read-only="true" /> 
               <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> 
               <tx:method name="list*" propagation="SUPPORTS" read-only="true" /> 
               <tx:method name="*" propagation="SUPPORTS" read-only="true" /> 
               <tx:method name="batchSaveOrUpdate" propagation="REQUIRES_NEW" /> 
           </tx:attributes> 
    </tx:advice> 
    <aop:config> 
           <aop:pointcut id="txPointcut" expression="execution(* cn.javass..service.*.*(..))" /> 
           <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> 
    </aop:config> 


<!-- 这是@Transactional注解的驱动,是基于注解的方式使用事务配置声明-->
<tx:annotation-driven transaction-manager="txManager"/>


</beans>


web.xml部署描述符参数配置

<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>
 
  <filter>
<filter-name>encodingFilter</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>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>


在Base或者Common层的类添加不好归类的泛指组件注解:@Component
在Dao层的实现类加数据访问层注解:@Repository
在Service层的实现类加业务层注解:@Service,@Transactional
在Controller层的Action上加控制器注解:@Controller
如果是用注解而非AOP事物的情况下则使用注解:@Transactional
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
注意这里:当在Service层标注@Transactional这个注解时默认所有的方法都开启事物,这样在性能上会打折扣同时数据库压力会越来越大,请在查询方法上添加,该注解是对查询不启用事物。



这是注解的Spring3+Struts2.1+SpringJDBC,
相关标签: spring