Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: $Proxy25 cannot be cast to my class

  1. #1

    Default $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 at 07:46 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    UK
    Posts
    108

    Default

    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.

  3. #3

    Default

    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>

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    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

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Have you actually READ my post.

    Quote 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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    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

  8. #8
    Join Date
    Aug 2004
    Location
    UK
    Posts
    108

    Default

    I think this is the line you are missing:

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

  9. #9

    Default

    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>

  10. #10
    Join Date
    Aug 2004
    Location
    UK
    Posts
    108

    Default

    No probs. Glad you've got it fixed now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •