如何在 Spring Boot 中连接 类 和对象

How to wire classes and objects in SpringBoot

拥有具有不同 classes 的 SpringBoot Java 应用程序。我无法将依赖项和 initialize/access 一个 class 的对象注入另一个 .看过spring文档,也使用了注解(@component,@Autowired等),还是有问题

以下是 classes.

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

}

控制器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;
}
}
 

现在尽管如此,我已经完成了所有注释,但不明白为什么 getToken() 方法无法在控制器 class 中使用 rt 对象访问。请推荐

好,我们按顺序走

首先,所有注释@Service@Controller@Repository都是@Component的规范,所以你不需要指定@Component@Controller 在你的 HighChartsController.

实际上,如果您检查注释 @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";
    }

}