Skip to content

How to wire classes and objects in SpringBoot

Have SpringBoot Java app with different classes. I am not able to inject the dependencies and initialize/access the object of one class into another . Have seen the spring doc and used the annotations (@component,@Autowired etc. ), still there is an issue.

following are the classes.

Main Class ()

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class CostmanagementApplication {

    public static void main(String[] args) {
        SpringApplication.run(CostmanagementApplication.class, args);
    }

}

Controller class

package com.test;

import javax.swing.text.rtf.RTFEditorKit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;


@Component
@Controller
public class HighChartsController {
    
    @Autowired
    private RequestToken rt;
    
    @GetMapping("/costdata")
    public static String customerForm(Model model) {

    //here not able to access the getToken() method 
                
        model.addAttribute("costdata", new CostDataModel());        
        return "costdata";
    }

}

RequestToken Class

   package com.test;
    
    import java.io.IOException;
    import java.net.URI;
    import java.net.URLEncoder;
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpRequest.BodyPublishers;
    import java.net.http.HttpResponse;
    import java.net.http.HttpResponse.BodyHandlers;
    import java.nio.charset.StandardCharsets;
    import java.util.Base64;
    import java.util.HashMap;
    import java.util.stream.Collectors;
    
    import org.json.JSONObject;
    import org.springframework.stereotype.Component;
    
    @Component
    public class RequestToken {
        
    
        public String getToken() throws IOException, InterruptedException {
            // TODO Auto-generated method stub
            
            // code to get the token 
        return token;
}
}
 

now eventhough , I have all annotation in place , not getting why the getToken() method is not accessible in controller class using rt object. please suggest

Answer

Okay, let’s go in order.

First of all, all the annotations @Service, @Controller and @Repository are specifications from @Component, so you don’t need to specify @Component and @Controller in your HighChartsController.

Actually, if you check what the annotation @Controller definition is, you’ll find this:

@Component
public @interface Controller {
  ...
}

Secondly, I don’t really know what do you mean with that you aren’t able to access the getToken() method, but as you wrote it seems you tried to access to that method as an static method.

You’re injecting the object, so you use the methods of the objects like in plain Java: rt.getToken(). The only difference is that the RequestToken object will be already initialized at the moment you call it.

package com.test;

import javax.swing.text.rtf.RTFEditorKit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HighChartsController {
    
    @Autowired
    private RequestToken rt;
    
    @GetMapping("/costdata")
    public static String customerForm(Model model) {

        String token = rt.getToken(); 

        ...
                
        model.addAttribute("costdata", new CostDataModel());        
        return "costdata";
    }

}