本帖最后由 linshow3 于 2012-5-7 23:07 编辑
* 这个问题可能与SPRING事务有关。
异常信息:::
Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'mgrManager' must be of type [service.impl.MgrManagerImpl], but was actually of type [$Proxy1]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:349)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079)
at tools.TestDao.main(TestDao.java:38)*我只要把SPRING的事务配置删除掉就可以正常运行了。但是却无法使用事务了,有没有什么解决办法。这是我在网上找到的同类异常情况。Beannamed 'txAdvice' must be of type [org.aopalliance.aop.Advice], but was actually of type [org.springframework.transaction.interceptor.TransactionInterceptor]但是网上的这个异常是jar包冲突,org.aopalliance.aop.Advice这个类是框架提供的,
我这个情况就是service.impl.MgrManagerImpl。这个类是我自己写的,应该不会有什么jar包冲突吧。 xml部分配置
<bean id="managerTemplate" abstract="true" lazy-init="true"
                p:appDao-ref="appDao"
                p:attendDao-ref="attendDao"
                p:typeDao-ref="attendTypeDao"
                p:checkDao-ref="checkDao"
                p:empDao-ref="employeeDao"
                p:mgrDao-ref="managerDao"
                payDao-ref="payDao"/>        <!-- 定义两个业务逻辑组件,继承业务逻辑组件的模板 -->
        <bean id="empManager"
                class="service.impl.EmpManagerImpl"
                parent="managerTemplate"/>
                
        <bean id="mgrManager"
                class="service.impl.MgrManagerImpl"
                parent="managerTemplate"/>
<bean id="transactionManager" class=
                "org.springframework.orm.hibernate3.HibernateTransactionManager"
                p:sessionFactory-ref="sessionFactory"/>        <!-- 配置事务增强处理Bean,指定事务管理器 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
                <!-- 用于配置详细的事务语义 -->
                <tx:attributes>
                        <!-- 所有以'get'开头的方法是read-only的 -->
                        <tx:method name="get*" read-only="true"/>
                        <!-- 其他方法使用默认的事务设置 -->
                        <tx:method name="*"/>
                </tx:attributes>
        </tx:advice>
        <aop:config>
                <!-- 配置一个切入点,匹配empManager和mgrManager
                        两个Bean的所有方法的执行 -->
                <aopointcut id="servicePointcut"
                        expression="bean(empManager)||bean(mgrManager)"/>
                <!-- 指定在servicePointcut切入点应用txAdvice事务增强处理 -->
                <aop:advisor advice-ref="txAdvice" 
                        pointcut-ref="servicePointcut"/>
        </aop:config>测试程序:::public class TestDao {        /**
         * @param args
         */
        public static void main(String[] args) {
                ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","daoContext.xml"});
                ApplicationDao app=ctx.getBean("appDao", ApplicationDaoHibernate.class);
                AttendDao att=ctx.getBean("attendDao",AttendDaoHibernate.class);
                AttendTypeDao attType=ctx.getBean("attendTypeDao",AttendTypeDaoHibernate.class);
                CheckBackDao che=ctx.getBean("checkDao",CheckBackDaoHibernate.class);
                EmployeeDao emp=ctx.getBean("employeeDao",EmployeeDaoHibernate.class);
                ManagerDao mgr=ctx.getBean("managerDao",ManagerDaoHibernate.class);
                PaymentDao pay=ctx.getBean("payDao",PaymentDaoHibernate.class);                MgrManager mss=ctx.getBean("mgrManager",MgrManagerImpl.class);  ///异常信息提示出问题的地方                //EmpManager empmgr=(EmpManagerImpl)ctx.getBean("empManager";
                
                /*下面这些是一开始写的,出了异常之后就先注释了。
                EmpManager empmgr=ctx.getBean("empManager",EmpManagerImpl.class);
        
                /*
                Manager manager=new Manager();
                manager.setDept("sss部";
                mgr.save(manager);
                
                Employee employee=emp.get(1);
                List<PaymentBean> payment=empmgr.empSalary("mysql";
                Iterator it=payment.iterator();
        
                while(it.hasNext())
                {
                        PaymentBean paym=(PaymentBean)it.next();
                        System.out.println(paym.getAmount()+paym.getPayMonth());
                }
                System.out.print("===================================";
                for(Object obj : payment)
                {
                        PaymentBean paym=(PaymentBean)obj;
                        System.out.println("月份: "+paym.getAmount()+"工资:"+paym.getPayMonth());
                }
                */
                //emp.delete(employee);
                
                System.out.print("程序执行完成===================================";
                        }}补充一点:
因为之前有个Unable to get the default Bean Validation factory 异常
所以我在配置文件
加了这句。javax.persistence.validation.mode=none我上面这个异常是出现在hibernate和srping整合的时候的,如果再整合struts ,spring容器由listener创建的话。就不会出现异常。

解决方案 »

  1.   

    楼主,我也遇到了同样问问题,不知道是配置文件不对,还是jar包冲突或者jar包之间的版本不匹配
    不知道楼主解决了没???
      

  2.   

    把“MgrManager mss=ctx.getBean("mgrManager",MgrManagerImpl.class);”这行改成“MgrManager mss=(MgrManager)ctx.getBean("mgrManager");”试试~~
      

  3.   

    Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'mgrManager' must be of type [service.impl.MgrManagerImpl], but was actually of type [$Proxy1]我觉得先要查明为什么会有代理类型,是Spring在某种条件下的自动行为吗?
      

  4.   

    这个问题应该很简单的原因造成的,好久没有玩过ssh这样类似的框架了,哎,现在没机会接触了,要是两年前,我应该能给你一个很准确的回答,哎学什么忘什么,果然害人害己啊!不过你需要修改配置文件是肯定的,或者你这个类 根本就没有交给spring管理,却要spring给你提供实例。这就出问题了。
      

  5.   

    aopalliance-alpha1.jar 包冲突了,去掉就行了。