Spring2.5 MVC + Hibernate3 关于扫描注解的问题

使用Spring2.5 + Hibernate3 进行改造项目的时候出现的问题,已经很长时间了都没有解决,希望大家帮忙,告诉问题所在.
问题是这样的:
1.我希望使用spring的MVC,不想像spring+struts那样有纵多的XML配置文件,开发起来配这配那很是繁琐.
2.以前用struts开发的,将Action改造成Controller,使用 @Controller 标注
3.Service层使用了@Service 标注, @Trasnactional标注
4.DAO层,由于使用泛型,从Controller到Service到DAO都使用泛型,DAO层被简化,只有一个基类DAO,Hibernate 这块 仍然是读取hbm.xml文件
5.事物交给spring 管理

问题是关于 组建扫描 和 事物标注 产生的问题, 在applicationcontext.xml中到底需要不需要扫描? 需要扫描哪些包
在spring-servlet.xml中肯定需要扫描 扫描什么包呢?

我曾今修改了很多方式,在applicationcontext.xml中,我使用<tx:annotation-driven transaction-manager="transactionManager"/> 后,使用 <context:component-scan base-package="com.dk">中间使用表达式过滤了包含controller的包</context:component-scan>
目的就是扫描除了Controller以外的所有标注类,在spring-servlet中仅仅扫描 Controller包,这时候就出现了问题,发现实例化同时拥有@service和Transactional标注的Service,好像有问题,我在这个Service类里的构造函数里打印的hashCode 和 Controller引用了这个类的地方打印hashCode发现并不相同,并且出现了 一对多关系的类不能加载子表数据,因为我在hibernate配置文件lazy=true,但绝对不是因为没opensessioninview,而是service的实例问题.

还有就是 我在applicationcontext.xml 把扫描去掉,再 spring-servlet中,扫描所有标注,这个时候就不会出现不能加载子表数据问题,而是事物没有起作用.

麻烦大家伙看看

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

<!-- 读配置文件 -->
<context:property-placeholder location="classpath:jdbc/mysql.properties"/>

<!-- 数据源配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>

<!-- Session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 指定映射文件夹位置 -->
<property name="mappingDirectoryLocations" value="classpath:mapping"/>
<!-- 指定hibernate参数 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>

<!-- Spring事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>


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

<!-- 注解扫描范围 -->
<context:component-scan base-package="com.dk"/>

<!-- 启用注解映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>



帮帮忙看看,或者有相同项目经历的,小弟谢谢大哥们.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值