Spring Auto-Wiring Beans with @Autowired annotation

In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in current Spring container. In most cases, you may need autowired property in a particular bean only.

In Spring, you can use @Autowired annotation to auto wire bean on the setter method, constructor or a field. Moreover, it can autowired property in a particular bean.

Note
The @Autowired annotation is auto wire the bean by matching data type.

See following full example to demonstrate the use of @Autowired.

1. Beans

A customer bean, and declared in bean configuration file. Later, you will use “@Autowired” to auto wire a person bean.


package com.mkyong.common;

public class Customer 
{
	//you want autowired this field.
	private Person person;
	
	private int type;
	private String action;
	
	//getter and setter method
	
}

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address 123" />
		<property name="age" value="28" />
	</bean>
	
</beans>

2. Register AutowiredAnnotationBeanPostProcessor

To enable @Autowired, you have to register ‘AutowiredAnnotationBeanPostProcessor‘, and you can do it in two ways :

1. Include <context:annotation-config />

Add Spring context and <context:annotation-config /> in bean configuration file.


<beans 
	//...
	xmlns:context="http://www.springframework.org/schema/context"
	//...
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	//...

	<context:annotation-config />
	//...
</beans>

Full example,


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:annotation-config />

	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>
	
</beans>
2. Include AutowiredAnnotationBeanPostProcessor

Include ‘AutowiredAnnotationBeanPostProcessor’ directly in bean configuration file.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean 
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
	
	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>
	
</beans>

3. @Autowired Examples

Now, you can autowired bean via @Autowired, and it can be applied on setter method, constructor or a field.

1. @Autowired setter method

package com.mkyong.common;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer 
{
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
	
	@Autowired
	public void setPerson(Person person) {
		this.person = person;
	}
}
2. @Autowired construtor

package com.mkyong.common;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer 
{
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
	
	@Autowired
	public Customer(Person person) {
		this.person = person;
	}
}
3. @Autowired field

package com.mkyong.common;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer 
{
	@Autowired
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
}

The above example will autowired ‘PersonBean’ into Customer’s person property.

Run it


package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
    	  new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});
    	
    	Customer cust = (Customer)context.getBean("CustomerBean");
    	System.out.println(cust);
    	
    }
}

Output


Customer [action=buy, type=1, 
person=Person [address=address 123, age=28, name=mkyong]]

Dependency checking

By default, the @Autowired will perform the dependency checking to make sure the property has been wired properly. When Spring can’t find a matching bean to wire, it will throw an exception. To fix it, you can disable this checking feature by setting the “required” attribute of @Autowired to false.


package com.mkyong.common;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer 
{
	@Autowired(required=false)
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
}

In the above example, if the Spring can’t find a matching bean, it will leave the person property unset.

@Qualifier

The @Qualifier annotation us used to control which bean should be autowire on a field. For example, bean configuration file with two similar person beans.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:annotation-config />

	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean1" class="com.mkyong.common.Person">
		<property name="name" value="mkyong1" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>
	
	<bean id="PersonBean2" class="com.mkyong.common.Person">
		<property name="name" value="mkyong2" />
		<property name="address" value="address 2" />
		<property name="age" value="28" />
	</bean>
	
</beans>

Will Spring know which bean should wire?

To fix it, you can use @Qualifier to auto wire a particular bean, for example,


package com.mkyong.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Customer 
{
	@Autowired
	@Qualifier("PersonBean1")
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
}

It means, bean “PersonBean1” is autowired into the Customer’s person property. Read this full example – Spring Autowiring @Qualifier example

Conclusion

This @Autowired annotation is highly flexible and powerful, and definitely better than “autowire” attribute in bean configuration file.

Download Source Code

62 comments on “Spring Auto-Wiring Beans with @Autowired annotation

  1. It would have been nice if you also showed the PersonBean class

    Reply
  2. can you please explain which type of DI is best and why among the constructor based,setter based and field based injection.

    Reply
  3. I am facing one problem that might interest you and you might like to have a post on it. My requirement is to Autowire an instance that requires constructor arguments to be passed. But it keeps on giving me error that default constructor not found for the bean.

    Something like this…
    // bean to be autowired
    class A {
    private int a;
    public A(int a) {
    }
    }

    // main class
    class B {
    @Autowired
    private A a;
    }

    Although I have declared in spring context a bean where constructor argument is specified for bean A

    Reply
  4. xml mapping for annotations is compulsory?.
    is there any way that i wire my bean without any xml mapping.

    Reply
  5. Perhaps you may put a note that @Autowired will only work for the object where it’s initialized as bean in spring. I have a class and one of the property/setter is annotated with @Autowired, if I instantiate the class by using new Abc() the @Autowired won’t work, I have to declare before I can see the result. Also annotating the class with @Component also does the same magic, I guess basically they’re the same thing.

    This could be intuitive for certain people but at least I’ve spent not less than 30 minutes just trying to figure out what has gone wrong. Hope it helps!

    Reply
  6. Aw, this was an incredibly nice post. Taking a few minutes and
    actual effort to create a really good article_ but what can I say_
    I procrastinate a lot and never ssem to get anything done.

    Reply
  7. I was curious if you ever thought of changing the layout of your
    site? Its very well written; I love what youve ggot to say.
    Buut maqybe you could a little more in thhe way of contdnt so people
    could connect with it better. Youve got an awful lot of
    text for only havving one or 2 images. Maybe
    you could space iit outt better?

    Reply
  8. We are a group of volunteers and opening a new
    scheme in our community. Your website provided us with valuable information to work on.
    You’ve done a formidable job and our entire community will be thankful to you.

    Reply
  9. Undeniably consider that that you stated. Your favourite reason appeared to bbe
    on the net the easiest factor to kesp inn mind of.
    I say to you, I certainly get annoyed while folks think about concerns
    that they just do not recognize about. You managed to
    hit the nail upon the top as neztly as defined out the whole thing with no need side effect , othher folks could tale a signal.
    Wiill likely be baxk to get more. Thhank you

    Reply
  10. I simply couldn’t go away your website before suggesting that I really enjoyed the usual information a person supply in your visitors?
    Is going to be back frequently in order to
    check out new posts

    Reply
  11. Howdy! I just wish to give an enormous thumbs up for the good data you will have here on this post.
    I will likely be coming back to your weblog for more soon.

    Reply
  12. I every time spent my half an hour to read this blog’s articles everyday along with a cup of coffee.

    Reply
  13. By starting an affiliate program, Motor Club Of America is tapping
    into all the exposure and advertising that affiliates create without spending a dime on outdated and ineffective advertising strategies.
    95 every month after the two months are up for that membership, you will immediately get 200%
    commission via $80 for that one person. That is why, when a person’s living pattern is suddenly broken for no apparent reason, it can be the sign of an affair.

    Reply
  14. I used to be suggested this blog through my cousin. I’m now not certain whether this post is written by way of him as no one else know such special about my difficulty. You’re incredible!
    Thank you!

    Reply
  15. Howdy! Would you mind if I share your blog with my twitter group?
    There’s a lot of people that I think would really enjoy your content. Please let me know. Many thanks

    Reply
  16. The most unique aspect of a virtual private server is that
    it provides with the flexibility to add and change modules for
    installing your own software, this includes the features and functionality of a dedicated server without the cost of
    a dedicated server. Unlike shared hosting, VPS guarantees the dedicated
    resources with full control to webmasters which makes it extremely suitable for medium to large websites.
    Web hosting services is one of the Denver IT support services which
    has became an essential requirement for you if you want to have a sure
    success into your company.

    Reply
  17. Many people aims to optimize their website and gain recognition by the Search Engine.
    These links are considered as spamming and are ignored by the search engine spiders.
    Higher page rank internet sites this sort of as Twitter, Facebook
    or even blog network web sites make it possible for you to develop
    a cost-free profile on their web-site. The more relevant the
    website to yours will look good for you and Google will
    like it to. If the social sites think you are only bookmarking your own site’s links, they could ban you. Link building by the use of content writing services is an important step in making a website functional for two important reasons.

    Reply
  18. I’ve learn a few just right stuff here. Definitely worth bookmarking for revisiting. I wonder how a lot effort you set to create this type of great informative site.

    Reply
  19. This is the right blog for everyone who would like to understand this topic.
    You understand so much its almost tough to argue with you (not that I really would want to_HaHa).
    You certainly put a brand new spin on a subject that has been written about
    for a long time. Wonderful stuff, just excellent!

    Reply
  20. To have more idea regarding these cars it is best to dial the toll free customer care numbers.
    Based in California, this is reportedly the only
    tractor-trailer limo ever built. People will tell you afterward about things that happened and show you pictures
    of scenes you’ll have absolutely no recollection of.

    Reply
  21. Way cool! Some very valid points! I appreciate you writing this article and
    the rest of the website is also very good.

    Reply
  22. Hi this is somewhat of off topic but I was wanting to know if blogs use
    WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

    Reply
  23. Not a great tutorial. You are not explaining what autowiring does.

    Reply
  24. I am trying implement a sample project with struts2 and spring. I have followed your example and it worked fine. But in every action I need hardcode spring.xml configuration file like below. How can i get rid of the following statement?

    ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {“SpringBeans.xml”});

    Customer cust = (Customer)context.getBean(“CustomerBean”);
    System.out.println(cust);

    I want to write something like below

    @Autowired
    Customer customer;

    does the above declaration works?if yes how spring will look for xml configuration file? will that check in classpath?

    Reply
  25. You might want to add a brief context as to WHY one would want to Autowire in the first place!

    Reply
  26. Wow, marvelous blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your site is magnificent, as
    well as the content!

    Reply
  27. Unquestionably believe that which you said. Your favorite
    justification seemed to be on the internet the simplest thing to be aware of.
    I say to you, I definitely get irked while people think about
    worries that they just do not know about. You managed to hit the nail upon the top
    as well as defined out the whole thing without having side-effects
    , people could take a signal. Will probably be back to get more.
    Thanks

    Reply
  28. I rarely leave a response, but i did some searching and wound up here
    Spring Auto-Wiring Beans with @Autowired annotation.
    And I do have 2 questions for you if it’s allright. Is it only me or does it give the impression like some of the comments come across like left by brain dead individuals? 😛 And, if you are posting on additional online sites, I’d like to follow anything fresh you have to post.
    Would you list of all of your community sites like your Facebook page,
    twitter feed, or linkedin profile?

    Reply
  29. Thanks designed for sharing such a fastidious thought, article
    is nice, thats why i have read it completely

    Reply
  30. Attractive component of content. I just stumbled upon your site and in accession capital to claim that I acquire actually loved account
    your weblog posts. Any way I’ll be subscribing on your augment or even I success you get entry to consistently fast.

    Reply
  31. I am getting the following error while running the above springs program, can any one help on that

    22 Dec, 2012 2:10:37 PM org.springframework.context.support.AbstractApplicationContext refresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e0cf70: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1e0cf70]; startup date [Sat Dec 22 14:10:37 IST 2012]; root of context hierarchy
    22 Dec, 2012 2:10:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [SpringBeans.xml]
    22 Dec, 2012 2:10:37 PM org.springframework.context.support.AbstractApplicationContext refresh
    INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1e0cf70]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1d04653
    22 Dec, 2012 2:10:37 PM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
    INFO: Bean ‘org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor’ is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    22 Dec, 2012 2:10:37 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d04653: defining beans [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,customer,person]; root of factory hierarchy
    22 Dec, 2012 2:10:37 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d04653: defining beans [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,customer,person]; root of factory hierarchy
    Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘customer’ defined in class path resource [SpringBeans.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.findMethod(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Method;
    Caused by: java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.findMethod(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Method;
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineRequiredStatus(AutowiredAnnotationBeanPostProcessor.java:407)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.buildAutowiringMetadata(AutowiredAnnotationBeanPostProcessor.java:340)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.findAutowiringMetadata(AutowiredAnnotationBeanPostProcessor.java:317)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:823)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:423)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:279)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:360)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:91)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:75)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
    at autowire12.App.main(App.java:16)

    Reply
  32. Hi,thanks for your tutorial, I typed some demo codes and ran it , then I found it that the means of the bean autowiring in property is first “byName”, if no matching bean exits, “byType” will work. right?

    Reply
  33. The application runs in JBoss 6.0.0 Final, but not in JBoss AS 7.1.1.

    We get a timeout without any error message. Only the following timeout is displayed:


    11:36:07,972 INFO [org.jboss.as.server] (DeploymentScanner-threads – 2)
    JBAS015870: Deploy of deployment “AS7.ear” wasrolled back with failure message Operation cancelled

    11:36:07,972 ERROR [org.jboss.as.server.deployment.scanner]
    (DeploymentScanner-threads – 1) JBAS015052: Did not receive a response to the deployment operation within the allowed timeout period [60 seconds]. Check the server configuration file and the server logs to find more about the status of the deployment.

    Reply
  34. I have used @Autowired without configuring AutowiredAnnotationBeanPostProcessor or context:annotation-config and it works.

    How is that possible. My spring version is 2.5.6

    Thanks for this simple tutorial

    Reply
  35. I have used @Autowired without configuring AutowiredAnnotationBeanPostProcessor or and it works.

    How is that possible. My spring version is 2.5.6

    Thanks for this simple tutorial

    Reply
    1. You might have used ,
      if you’re using this annotation, then without AutowiredAnnotationBeanPostProcessor, code will be worked.

      Reply
  36. I have got everything working without having AutowiredAnnotationBeanPostProcessor or

    How was that possible?

    Reply
  37. I have a question. You have used:

    	ApplicationContext context = 
        	  new ClassPathXmlApplicationContext(new String[] {"com/mkyong/common 
                                                  /SpringBeans.xml"});
     
        	Customer cust = (Customer)context.getBean("CustomerBean&quot");
    

    To start you soring context off. Is there no way to autowire the first bean “CustomerBean”?

    Thanks

    Reply
    1. Sorry caused you confused, above example is for demonstration purpose only. Sure you can autowire the “customerBean”, the concept is same with “personBean”.

      Reply
  38. Hi great Spring by example type post. I had to place the bean configuration file into the package level that the Person and Customer class existed in order to get it to work:

      
        public static void main( String[] args )
        {
        	ApplicationContext context = 
        	  new ClassPathXmlApplicationContext(new String[] {"com/mkyong/common 
                                                  /SpringBeans.xml"});
     
        	Customer cust = (Customer)context.getBean("CustomerBean");
        	System.out.println(cust);
     
        }
    
    

    I have seen other Spring examples where the author places the bean configuration xml file on the project level? Is there a convention for this? Did I forget something in order to have the bean configuration file on the project level?

    Geo

    Reply
    1. Forgot one other thing. If you have a constructor with the Autowired bean in the argument you need to make sure to place the @Qualifier annotation in the argument:

        
      	@Autowired
      	public void setPerson(@Qualifier(&quot;PersonBean2&quot;)Person person) {
      		this.person = person;
      	}
      
      

      And also the same for any setter you may have in the class:

        
      	@Autowired
      	public void setPerson(@Qualifier(&quot;PersonBean2&quot;)Person person) {
      		this.person = person;
      	}
      
      

      Hope that helps,

      Geo

      Reply
      1. Geo I think your first code example should be:

        
        @Autowired
        public Person(@Qualifier("PersonBean2") Person person) {
            this.person = person;
        }
        
        
        Reply
        1. I am getting the following error while running the above springs program, can any one help on that

          22 Dec, 2012 2:10:37 PM org.springframework.context.support.AbstractApplicationContext refresh
          INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e0cf70: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1e0cf70]; startup date [Sat Dec 22 14:10:37 IST 2012]; root of context hierarchy
          22 Dec, 2012 2:10:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
          INFO: Loading XML bean definitions from class path resource [SpringBeans.xml]
          22 Dec, 2012 2:10:37 PM org.springframework.context.support.AbstractApplicationContext refresh
          INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1e0cf70]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1d04653
          22 Dec, 2012 2:10:37 PM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
          INFO: Bean ‘org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor’ is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
          22 Dec, 2012 2:10:37 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
          INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d04653: defining beans [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,customer,person]; root of factory hierarchy
          22 Dec, 2012 2:10:37 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
          INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d04653: defining beans [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,customer,person]; root of factory hierarchy
          Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘customer’ defined in class path resource [SpringBeans.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.findMethod(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Method;
          Caused by: java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.findMethod(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Method;
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineRequiredStatus(AutowiredAnnotationBeanPostProcessor.java:407)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.buildAutowiringMetadata(AutowiredAnnotationBeanPostProcessor.java:340)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.findAutowiringMetadata(AutowiredAnnotationBeanPostProcessor.java:317)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:823)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:423)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:279)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:360)
          at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:91)
          at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:75)
          at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
          at autowire12.App.main(App.java:16)

          Reply
    2. I am getting the following error while running the above springs program, can any one help on that

      22 Dec, 2012 2:10:37 PM org.springframework.context.support.AbstractApplicationContext refresh
      INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e0cf70: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1e0cf70]; startup date [Sat Dec 22 14:10:37 IST 2012]; root of context hierarchy
      22 Dec, 2012 2:10:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
      INFO: Loading XML bean definitions from class path resource [SpringBeans.xml]
      22 Dec, 2012 2:10:37 PM org.springframework.context.support.AbstractApplicationContext refresh
      INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1e0cf70]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1d04653
      22 Dec, 2012 2:10:37 PM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
      INFO: Bean ‘org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor’ is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
      22 Dec, 2012 2:10:37 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
      INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d04653: defining beans [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,customer,person]; root of factory hierarchy
      22 Dec, 2012 2:10:37 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
      INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d04653: defining beans [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,customer,person]; root of factory hierarchy
      Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘customer’ defined in class path resource [SpringBeans.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.findMethod(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Method;
      Caused by: java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.findMethod(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Method;
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineRequiredStatus(AutowiredAnnotationBeanPostProcessor.java:407)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.buildAutowiringMetadata(AutowiredAnnotationBeanPostProcessor.java:340)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.findAutowiringMetadata(AutowiredAnnotationBeanPostProcessor.java:317)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:823)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:423)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:279)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:360)
      at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:91)
      at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:75)
      at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
      at autowire12.App.main(App.java:16)

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *