16

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)?

kamaci
  • 72,915
  • 69
  • 228
  • 366
  • This problem is so common for Spring newbies that I added a reference to this question to [*spring*](http://stackoverflow.com/tags/spring/info) 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. – Tomasz Nurkiewicz Nov 11 '11 at 08:28

3 Answers3

10

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"/>
jeha
  • 10,562
  • 5
  • 50
  • 69
6

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.

  • I used `Service` annotation and autowired Bridge class at another class. What is the difference between `Service` and `Configuration`? – kamaci Nov 11 '11 at 08:22
  • @Configuration allows to programmtically specify your beans. It isn't a Stereotype like the @Service/@Repositry/@Component/@Controller are. Plus you will need - in your application context – James 'Cookie' Cook Nov 11 '11 at 08:30
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

Lalit Jha
  • 2,143
  • 1
  • 12
  • 6
  • 1
    Any chance you are affiliated with the site you are linking to? – Andrew Barber Oct 12 '12 at 08:19
  • @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. – Lalit Jha Oct 24 '12 at 09:46
  • 1
    It is *required* that you disclose every time you link to your own website. – Andrew Barber Oct 24 '12 at 12:52
  • @Andrew What actions I need to take? – Lalit Jha Nov 08 '12 at 17:03
  • Be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. – Andrew Barber Nov 08 '12 at 17:06
  • 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. – Lalit Jha Nov 16 '12 at 12:13
  • the faq says it applies for both promoting products and websites. It also says clearly, "However, you must disclose your affiliation in your answers." – user2932397 Nov 25 '13 at 20:45