有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在SpringBoot中连接类和对象

使用不同类的SpringBoot Java应用程序。我无法注入依赖项并将一个类的对象初始化/访问到另一个类。已经看过spring文档并使用了注释(@component、@Autowired等),仍然存在问题

以下是课程

主类()

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);
    }

}

控制器类

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类

   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;
}
}
 

现在,尽管我已经准备好了所有注释,但我不明白为什么在控制器类中使用rt对象无法访问getToken()方法。请建议


共 (1) 个答案

  1. # 1 楼答案

    好的,我们按顺序走

    首先,所有注释@Service@Controller@Repository都是来自@Component的规范,因此不需要在HighChartsController中指定@Component@Controller

    实际上,如果您检查注释@Controller定义是什么,您会发现:

    @Component
    public @interface Controller {
      ...
    }
    

    第二,我真的不知道你不能访问getToken()方法是什么意思,但正如你所写的,你似乎试图以static方法访问该方法

    您正在注入对象,因此使用对象的方法,比如在普通Java中:rt.getToken()。唯一的区别是RequestToken对象在调用它的那一刻就已经初始化了

    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";
        }
    
    }