spring3.2与Hibernate集成配置问题

00风涯00 发布于 2014/01/17 15:43
阅读 321
收藏 0

我想用全注解的方式配置项目

我的配置如下:

1.

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

     <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
        <property name="url" value="jdbc:sqlserver://192.168.100.33:1433;DatabaseName=ll"></property>
        <property name="username"><value>***</value></property>
        <property name="password"><value>***</value></property>
    </bean>  
        
   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />
         <property name="packagesToScan">  
            <list>  
                <value>com.test.*</value>  
            </list>  
        </property>     
        <property name="hibernateProperties">  
            <props>  
                   <prop key="hibernate.connection.autocommit">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">ture</prop>
                <prop key="hibernate.format_sql">ture</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>  
        </property>
    </bean>
    
    <!-- 指定使用cglib -->  
    <aop:aspectj-autoproxy proxy-target-class="true" />   
      
    <!-- 配置注解实现管理事务(cglib:proxy-target-class="true") -->  
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />   
    
     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
              <tx:method name="find*" read-only="true" propagation="REQUIRED"/>
              <tx:method name="update*" propagation="REQUIRED"/>
              <tx:method name="del*" propagation="REQUIRED"/>
              <tx:method name="add*" propagation="REQUIRED"/>
              <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
    <aop:config expose-proxy="true">
        <aop:pointcut expression="execution(* com.test.service.*.*(..))" id="pointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

2.

<!-- 开启注解 -->
    <mvc:annotation-driven/>
    
    <!-- 开启静态资源访问 -->
    <mvc:default-servlet-handler/>

<!--扫描crotroller包-->

    <context:component-scan base-package="com.test.controller">


    </context:component-scan>
    
    <!-- 默认视图解析器,指定逻辑视图转换为物理视图时的前缀和后缀。 -->
    <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

这时会出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.service.RoleService com.test.controller.RoleController.roleservice; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.service.RoleService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
	org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
	org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:634)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:591)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:648)
	org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:511)
	org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:452)
	org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
	javax.servlet.GenericServlet.init(GenericServlet.java:160)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
	org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
	org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	java.lang.Thread.run(Unknown Source)
提示:注入service失败
如果我把红色字体的部分修改为扫描全部包,如:

<context:component-scan base-package="com.test">

或者再加上:

<context:component-scan base-package="com.test.controller">
    <!-- 扫描符合@Service @Repository的类 -->
    <context:include-filter type="regex" expression="com.test.service.*"/>
    <context:include-filter type="regex" expression="com.test.dao.*"/>
    </context:component-scan>

这时就会报:No Session found for current thread
这个错!!!!

@Transactional这个也加了,还是不行

哪位大神懂,帮小弟分析下,万分感谢!!!


加载中
0
0
00风涯00
呃.....没人回答?
返回顶部
顶部