Announcement Announcement Module
Collapse
No announcement yet.
Spring is throwing incorrect error message!! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring is throwing incorrect error message!!

    Hi,
    I am trying to register a bean that is created outside of the Spring Container scope as follows:

    Code:
    public static void main(String[] args) 
    {
        ClassPathXmlApplicationContext context = 
    new  ClassPathXmlApplicationContext("applicationContext.xml");
       User singletonObject = new User(1, "xxx", null);
       context.getBeanFactory().registerSingleton("myOwnBean", singletonObject);	  
       User user = context.getBean(User.class);
       System.out.println(user);
    		
    }
    If I didn't register any other User type bean in my applicationContext.xml it is working fine.
    Also if I use
    User user = context.getBean("myOwnBean", User.class);
    it is working fine.

    But if i register some User type bean in my applicationContext.xml with someother id, say "testUser", and when i try to get User bean using
    User user = context.getBean(User.class);
    it is throwing following exception.

    Code:
    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myOwnBean' is defined
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:254)
    	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
    	at com.sivalabs.spring.test.Main.main(Main.java:30)
    Ideally it should throw the following exception:
    Code:
    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.sivalabs.spring.chapter1.User] is defined: expected single bean but found 2

    Any idea on this???

  • #2
    Hello

    Code:
    Ideally it should throw the following exception:
    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No unique bean of type [com.sivalabs.spring.chapter1.User] is defined: 
    expected single bean but found 2
    I am agree

    Post your complete code, because the follow is confuse

    But if i register some User type bean in my applicationContext.xml with someother id, say "testUser",
    and when i try to get User bean using User user = context.getBean(User.class);
    it is throwing following exception.
    If in your Main class you have "only" the red part, why appear the myOwnBean term in the error stack trace?

    Code:
    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No bean named 'myOwnBean' is defined

    Comment


    • #3
      Hi,
      The following is my applicationContext.xml

      Code:
      <beans>
      <bean id="user" class="com.sivalabs.spring.chapter1.User">
      		<constructor-arg name="id" value="1"/>
      		<constructor-arg name="name" value="Siva"/>
      	</bean>
      </beans>
      And this is my main method:
      Code:
      public static void main(String[] args) 
      {
      		
      	AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      	User singletonObject = new User(1, "xxx", null);
      	context.getBeanFactory().registerSingleton("myOwnBean", singletonObject);		
      	User user = context.getBean(User.class);
      	System.out.println(user);		
      	}
      In this case I am getting the following error:
      Code:
      Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myOwnBean' is defined
      	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
      	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:254)
      	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
      	at com.sivalabs.spring.test.Main.main(Main.java:29)
      But here what is happening is:
      There is already a bean of type 'User' registered in applicationContext.xml and another User Type bean is registered through registerSingleton(). But when i asked ApplicationContext for a bean of type User.class it should say

      Code:
       No unique bean of type [com.sivalabs.spring.chapter1.User] is defined
      Your thoughts on this?

      Thanks,
      Siva

      Comment

      Working...
      X