Announcement Announcement Module
Collapse
No announcement yet.
basic wiring question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • basic wiring question

    I have a bean that implements ApplicationContextAware and of course provides its own setApplicationContext() setter per the interface definition.

    I am using this bean in a JUnit context where AUTOWIRE_BY_NAME is set. I am getting an exception from the Spring container because the object type for ApplicationContext on my bean is not understood.

    What do I need to do to avoid this and have Spring ignore this ApplicationContext property when it processes AUTOWIRE_BY_NAME?

    Thanks

  • #2
    It would be really useful to see the TestCase, the full stacktrace and the applicationContext.xml. If you code wrap them in [ code] [ /code] tags, it's soooo much easier to use.

    Comment


    • #3
      example

      Okay here is some sample code to illustrate.

      Sample JUnit class
      Code:
      public class MyTestCase extends AbstractDependencyInjectionSpringContextTests{
              private SomeBeanClass myBean = null;
      	public MyTestCase()
      	{
      		setAutowireMode(AUTOWIRE_BY_NAME);
      	}	
      
              public void testSomething()
              {
                // use the bean somehow
              }
      
              public void setMyBean(SomeBeanClass value)
              {
                 myBean = value;
              }
      	@Override
      	protected String[] getConfigLocations() {
      		return new String[] { "classpath:testApplicationContext.xml" };
      	}
      }
      testApplicationContext.xml
      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
      <beans>
       <bean id="myBean" class="com.SomeBeanClass">
        <!-- boring properties -->
       </bea>
      </beans>
      SomeBeanClass example

      Code:
      public class SomeBeanClass implements ApplicationContextAware
      {
         private ApplicationContext applicationContext = null;
        // property getter/setters and some business logic here
      
      
      	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      		this.applicationContext = applicationContext;
      	}
      }
      Stack trace
      Code:
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'createUserProfileCommand' defined in class path resource [testApplicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
      PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy28] to required type [com.aff.services.RegistrationService] for property 'registrationService'; nested exception is java.lang.IllegalArgumentException: No matching editors or conversion strategy found
      Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
      PropertyAccessException 1:
      org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy28] to required type [com.aff.services.RegistrationService] for property 'registrationService'; nested exception is java.lang.IllegalArgumentException: No matching editors or conversion strategy found
      Caused by: java.lang.IllegalArgumentException: No matching editors or conversion strategy found
      	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:212)
      	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:127)
      	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:774)
      	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:607)
      	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:49)
      	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:74)
      	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:961)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
      	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
      	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:140)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
      	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:273)
      	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:346)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
      	at org.springframework.test.AbstractSingleSpringContextTests.loadContextLocations(AbstractSingleSpringContextTests.java:181)
      	at org.springframework.test.AbstractSingleSpringContextTests.loadContext(AbstractSingleSpringContextTests.java:151)
      	at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:105)
      	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:82)
      	at junit.framework.TestCase.runBare(TestCase.java:128)
      	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
      	at junit.framework.TestResult$1.protect(TestResult.java:110)
      	at junit.framework.TestResult.runProtected(TestResult.java:128)
      	at junit.framework.TestResult.run(TestResult.java:113)
      	at junit.framework.TestCase.run(TestCase.java:120)
      	at junit.framework.TestSuite.runTest(TestSuite.java:228)
      	at junit.framework.TestSuite.run(TestSuite.java:223)
      	at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
      	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
      	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

      Comment


      • #4
        If you create an interface, SomeBeanInterface and get SomeBeanClass to implement that. Then change the property and the setter in the unit test to use SomeBeanInterface instead. The problem should go away.
        http://www.springframework.org/docs/...l#aop-proxying

        Comment

        Working...
        X