web项目进展5天,架构由组长搭建,有数据库层,持久化层,服务层,表现层。mvc控制,spring管理bean,hibernate做持久化和反转映射工作。
dao 层是从组长写的泛型类中通过spring注入方式生成,之后结合spring的事务管理功能,生成service层中的增加,删除的服务功能。
spring配置文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 配置 Hibernate 的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 配置dao -->
<bean id="orderDao" class="com.hb.petstore.dao.BasicDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
<constructor-arg>
<value>com.hb.petstore.vo.Order</value>
</constructor-arg>
</bean>
<bean id="userDao" class="com.hb.petstore.dao.BasicDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
<constructor-arg>
<value>com.hb.petstore.vo.User</value>
</constructor-arg>
</bean>
<bean id="productDao" class="com.hb.petstore.dao.BasicDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
<constructor-arg>
<value>com.hb.petstore.vo.Product</value>
</constructor-arg>
</bean>
<bean id="productTypeDao"
class="com.hb.petstore.dao.BasicDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
<constructor-arg>
<value>com.hb.petstore.vo.ProductType</value>
</constructor-arg>
</bean>
<bean id="pageQueryDao"
class="com.hb.petstore.dao.PageQueryDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 配置 service -->
<bean id="orderService"
class="com.hb.petstore.service.impl.OrderServiceImpl">
<property name="orderDao">
<ref bean="orderDao" />
</property>
</bean>
<bean id="userService"
class="com.hb.petstore.service.impl.UserServiceImpl">
<property name="userDao">
<ref bean="userDao" />
</property>
</bean>
<bean id="productService"
class="com.hb.petstore.service.impl.ProductServiceImpl">
<property name="productDao">
<ref local="productDao" />
</property>
<property name="proTypeDao">
<ref local="productTypeDao" />
</property>
<property name="pageQueryDao">
<ref local="pageQueryDao" />
</property>
</bean>
<bean id="cartService"
class="com.hb.petstore.service.impl.CartServiceImpl">
</bean>
<!-- 配置 service里用到的HibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置struts中的action -->
<bean name="/order"
class="com.hb.petstore.struts.action.OrderAction">
<property name="orderService">
<ref local="orderServiceTransaction"/>
</property>
<property name="userService">
<ref local="userServiceTransaction"/>
</property>
</bean>
<bean name="/findUser"
class="com.hb.petstore.struts.action.FindUserAction">
<property name="userService">
<ref local="userServiceTransaction" />
</property>
</bean>
<bean name="/addUser"
class="com.hb.petstore.struts.action.AddUserAction">
<property name="userService">
<ref local="userServiceTransaction" />
</property>
</bean>
<bean name="/addProduct" class="com.hb.petstore.struts.action.AddProductAction">
<property name="productService">
<ref bean="proServiceTransaction"/>
</property>
</bean>
<bean name="/cart"
class="com.hb.petstore.struts.action.CartAction">
</bean>
<!-- 配置spring事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- spring 事务代理的service 配置-->
<bean id="orderServiceTransaction"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target">
<ref bean="orderService"/>
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userServiceTransaction"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref bean="userService" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="proServiceTransaction"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref bean="productService" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
以订单表为例:
先注入 数据到泛型实现 BasicDaoImpl 中生成oraderDaoImpl ,但命名上的问题,这次命名成了orderDao。
注入生成Dao后,同样注册生成OrderService。为了让其真正的成为一个service层,在Service中,要配置Spring对它的代理,就是增加事务的功能,需要定义各种方法,以及用到的insert等。这些是与hibernate的整合配置。
同时配置action,就是与struts整合的配置。用spring来控制先前Structs生产的action,当然在structs的配置文件中,设置type="org.springframework.web.struts.DelegatingActionProxy" 使他把控制权给spring,配置spring的action,并添加入有事务功能的orderservice,即注册他的属性。
对spring自身,需要配置sessionfactory,以及hinbernate模板等。
昨天晚上写了一个hibernate测试类,运行后一大堆的问题和错误,经过组长的指点,到中午才解决。这个艰难的解决过程,再下一篇中好好提。