Spring Dependency Injection Autowiring Null

29,798

Solution 1

Using Bridge wb = new Bridge() does not work with dependency injection. Your restTemplate is not injected, because wb in not managed by Spring.

You have to make your Bridge a Spring bean itself, e.g. by annotation:

@Service
public class Bridge {
    // ...
}

or by bean declaration:

<bean id="bridge" class="Bridge"/>

Solution 2

Just to add further to Jeha's correct answer.

Currently, by doing

Bridge wb = new Bridge();

Means that, that object instance is not "Spring Managed" - I.e. Spring does not know anything about it. So how can it inject a dependency it knows nothing about.

So as Jeha said. Add the @Service annotation or specify it in your application context xml config file (Or if you are using Spring 3 you @Configuration object)

Then when the Spring context starts up, there will be a Singleton (default behavior) instance of the Bridge.class in the BeanFactory. Either inject that into your other Spring-Managed objects, or pull it out manually e.g.

Bridge wb = (Bridge) applicationContext.getBean("bridge"); // Name comes from the default of the class

Now it will have the dependencies wired in.

Solution 3

If you want to use new operator and still all dependency injected, then rather than making this a spring component (by annotating this with @Service), make it a @Configurable class.

This way even object is instantiated by new operator dependencies will be injected.

Few configuration is also required. A detailed explanation and sample project is here.

http://spring-framework-interoperability.blogspot.in/2012/07/spring-managed-components.html

Share:
29,798
kamaci
Author by

kamaci

Updated on November 13, 2020

Comments

  • kamaci
    kamaci over 3 years

    I was able to use RestTemplate and autowire it. However I want to move my rest template related part of code into another class as follows:

    public class Bridge {
    
        private final String BASE_URL = "http://localhost:8080/u";
    
        @Autowired
        RestTemplate restTemplate;
    
        public void addW() {
           Map<String, String> x = new HashMap<String, String>();
           W c = restTemplate.getForObject(BASE_URL + "/device/yeni", W.class, x);
           System.out.println("Here!");
        }
    }
    

    And at another class I call it:

    ...
    Bridge wb = new Bridge();
    wb.addW();
    ...
    

    I am new to Spring and Dependency Injection terms. My restTemplate variable is null and throws an exception. What can I do it how to solve it(I don't know is it related to I use new keyword)?

    • Tomasz Nurkiewicz
      Tomasz Nurkiewicz over 12 years
      This problem is so common for Spring newbies that I added a reference to this question to spring tag wiki. I am 100% sure that it has been answered multiple times so far, but I couldn't find any legitimate question. If any of you find one, please mark this question as duplicate and update wiki.
  • kamaci
    kamaci over 12 years
    thanks for your comment. How can I design my implementation do you have suggestion?
  • kamaci
    kamaci over 12 years
    I used Service annotation and autowired Bridge class at another class. What is the difference between Service and Configuration?
  • James 'Cookie' Cook
    James 'Cookie' Cook over 12 years
    @Configuration allows to programmtically specify your beans. It isn't a Stereotype like the @Service/@Repositry/@Component/@Controller are. Plus you will need <context:component-scan base-package="com.yourpackages"/> <context:annotation-config/> - in your application context
  • Andrew Barber
    Andrew Barber over 11 years
    Any chance you are affiliated with the site you are linking to?
  • Lalit Jha
    Lalit Jha over 11 years
    @Andrew This blog is publicly available and is written by me only, I dont think this requires any affiliation. I did not get your concern.
  • Andrew Barber
    Andrew Barber over 11 years
    It is required that you disclose every time you link to your own website.
  • Lalit Jha
    Lalit Jha over 11 years
    @Andrew What actions I need to take?
  • Andrew Barber
    Andrew Barber over 11 years
    Be sure to read the FAQ on Self-Promotion carefully.
  • Lalit Jha
    Lalit Jha over 11 years
    Yes, terms does not stops from pointing to affiliate resource and it is much about promoting products not for links. This question needs much configuration, enough that can not be described in a comment, so I pointed to a link where it is done. What's negetive in that...and this was the most appropriate answer.
  • user2932397
    user2932397 over 10 years
    the faq says it applies for both promoting products and websites. It also says clearly, "However, you must disclose your affiliation in your answers."