Announcement Announcement Module
Collapse
No announcement yet.
$Proxy25 cannot be cast to my class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • $Proxy25 cannot be cast to my class

    Hello, I am getting this exception while running a Test (I am trying to configure aop in spring):

    Code:
       java.lang.ClassCastException: $Proxy25 cannot be cast to path.UserDao
        	at com.path.TestCreateOntologyDB.testGenerateGlobalAnnotation(TestCreateOntologyDB.java:49)
        	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        	at java.lang.reflect.Method.invoke(Method.java:597)
        	at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
        	at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
        	at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
        	at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
        	at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
        	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
        	at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
        	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
        	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
        	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
        	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
        	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
        	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
        	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    UserDao.java

    Code:
    public class UserDao extends AbstractHibernateDAOSupport {
        
        	public UserDao() {
        		super();
        	}
        
        	/**
        	 * Insert a new User into the database.
        	 * 
        	 * @param user
        	 */
        	public void store(User user) throws DataAccessLayerException {
        		super.save(user);
        	}
        
        	/**
        	 * Delete a User from the database.
        	 * 
        	 * @param user
        	 */
        	public void delete(User user) throws DataAccessLayerException {
        		super.delete(user);
        	}
        
        	/**
        	 * Updates the state of a detached User.
        	 * 
        	 * @param user
        	 */
        	public void update(User user) throws DataAccessLayerException {
        		super.update(user);
        	}
        
        	public User findByID(String id) throws DataAccessLayerException {
        		return (User) this.find(User.class, id);
        
        	}
        	
        	/**
        	 * Finds all Users in the database.
        	 * 
        	 * @return
        	 */
        	public List findAll() throws DataAccessLayerException {
        		return super.findAll(User.class);
        	}


    Spirng configuration files: applicationContext-dao.xml

    HTML Code:
    <bean id="userDao" class="path.UserDao">
        		<property name="sessionFactory">
        			<ref bean="sessionFactory" />
        		</property>
        	</bean>
    TestCreateOntologyDB.java

    Code:
      ....  
        ApplicationContext ctx ;
        	
        	public TestCreateOntologyDB() {
        		ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        	}
        	
        	@Test
        	public void testGenerateGlobalAnnotation(){
        		UserDao userDao = (UserDao)ctx.getBean("userDao");
        
        ...

    And I haven't set up any UserDao additional propperty in any other configuration file. What could be the error?? Any help would be appreciated. Thanks in advance
    Last edited by mujer_esponja; Apr 18th, 2011, 07:46 AM.

  • #2
    You probably need to configure your transactions in you test context file.

    Something like.

    Code:
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    It looks like you are missing the proxy-target-class attribute.

    Comment


    • #3
      If I am not wrong, I think I have already this configuration in another file: applicationContext-service.xml

      Code:
      <?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: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/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">
      
      	<bean id="transactionManager"
      		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      		<property name="sessionFactory" ref="sessionFactory" />
      	</bean>
      
      	<aop:aspectj-autoproxy />
      	<tx:advice id="txAdvice" transaction-manager="transactionManager">
      		<tx:attributes>
      			<tx:method name="*" propagation="REQUIRED" />
      		</tx:attributes>
      	</tx:advice>
      
      	<aop:config>
      		<aop:pointcut id="serviceOperation"
      			expression="execution(* path.**.*(..)) || execution(* path.**.*(..)) "  />
      	-->
      		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
      	</aop:config>
      
      	<bean id="ontologyDao" class="path.OntologyDao">
      	
      		<property name="sessionFactory" ref="sessionFactory" />
       
      </beans>

      Comment


      • #4
        I suggest the forum search, as this question has been answered numerous times before. As the previous poster stated enable class proxies or create proper interfaces and program to those.

        Comment


        • #5
          Thanks, but before writting here my question I tried to find something in different forums which could help me, but without success...

          I have realized that probably, the error is here:

          <aop:config>
          <aopointcut id="serviceOperation"
          expression="execution(* path.model.**.*(..)) || execution(* path.dao.**.*(..)) " />
          <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />

          </aop:config>
          maybe in the expression, but I can't solve it, because this point is not very clear for me, even reading documentations about that

          Comment


          • #6
            Have you actually READ my post.

            Originally posted by mdeinum
            As the previous poster stated enable class proxies or create proper interfaces and program to those.
            How to do that is documented in the reference guide. I also suggest a read on how proxies work and the difference between JDK Dynamic Proxies and class proxies.

            Comment


            • #7
              Following your link, and also this example, I created a package in: path.services with the classes that will be working with aop, and also another package: path.services.impl, with the appropiate interfaces for them.

              Then, I change the configurations file, writting on it:

              <aop:config>
              <aopointcut id="serviceOperation"
              expression="execution(* path.services.**.*(..))" />

              <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />

              </aop:config>
              The error is still there. I am new in aop, and in spring configuration for this, consider that, please.

              Thanks your answer

              Comment


              • #8
                I think this is the line you are missing:

                Code:
                <aop:aspectj-autoproxy proxy-target-class="true"/>

                Comment


                • #9
                  Yes, that was it!!
                  Thank you very much!!

                  I just had
                  <aop:aspectj-autoproxy/>
                  PS. I had also to add this dependency to my pom.xml

                  <dependency>
                  <groupId>cglib</groupId>
                  <artifactId>cglib</artifactId>
                  <version>2.2</version>
                  </dependency>

                  Comment


                  • #10
                    No probs. Glad you've got it fixed now.

                    Comment


                    • #11
                      I don't know if I better open a new thread, but I prepared all this aop configuration just to avoid an exception I was having:

                      failed to lazily initialize a collection of role: path.MediaObject.annotations, no session or session was closed
                      Leave Spring the work of open and close ddbb connections
                      This is an Object which contains a list, adn this exception comes when I am trying to list it.
                      Thanks

                      Comment


                      • #12
                        Looks like a question that would be better suited to the Data forum.

                        http://forum.springsource.org/forumdisplay.php?f=27

                        Comment


                        • #13
                          Thanks again, I already did it here, explaining the whole aop configuration I did, because maybe I miss something


                          <beans xmlns="http://www.springframework.org/schema/beans"
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          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/schem...-beans-3.0.xsd
                          http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd
                          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

                          <bean id="transactionManager"
                          class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
                          <property name="sessionFactory" ref="sessionFactory" />
                          </bean>


                          <aop:aspectj-autoproxy proxy-target-class="true"/>

                          <tx:advice id="txAdvice" transaction-manager="transactionManager">
                          <tx:attributes>
                          <tx:method name="*" propagation="REQUIRED" />
                          </tx:attributes>
                          </tx:advice>




                          <aop:config>
                          <aopointcut id="serviceOperation"
                          expression="execution(* path.services.**.*(..))" />
                          <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
                          </aop:config>

                          <bean id="ontologyDao" class="path.services.OntologyDao">

                          <property name="sessionFactory" ref="sessionFactory" />
                          </bean>
                          Now the explanation:
                          path.services is the package where the implementations are
                          path.services.localImpl is the package where the interfaces are


                          Thanks in advance

                          Comment

                          Working...
                          X