Spring Autowire弹簧引导类

Spring Autowire弹簧引导类,spring,spring-boot,Spring,Spring Boot,我有一些代码试图自动连接一个包含Spring引导类的Bean import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import org.springframework.web.filter.GenericFilterBean

我有一些代码试图自动连接一个包含Spring引导类的Bean

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class JWTAuthenticationFilter extends GenericFilterBean{

    @Autowired
    RequestPublicKey requestPublicKey;

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
                throws IOException, ServletException {

            System.out.println(requestPublicKey.getPublicKey());
            filterChain.doFilter(request, response);

        }
}
RequestPublicKey
的初始代码如下所示:

@Component
public class RequestPublicKey {

    public String getPublicKey(){
        RestTemplate restTemplate = new RestTemplate();
        String publicCreds = restTemplate.getForObject("http://localhost:8081/get-public-key?kid=bf3b7429-261e-48c5-8409-79e1c9f203de", String.class);
        return publicCreds;
    }
}
当我尝试自动连线
RequestPublicKey
时,它返回
null
。我想这是因为我用
restemplate restemplate=new restemplate()
实例化了
restemplate
。为了解决这个问题,我尝试像这样自动连接
RestTemplate

@Component
public class RequestPublicKey {

    @Autowired
    private RestTemplate restTemplate;

    public String getPublicKey(){
        String publicCreds = restTemplate.getForObject("http://localhost:8081/get-public-key?kid=bf3b7429-261e-48c5-8409-79e1c9f203de", String.class);
        return publicCreds;
    }
} 
但后来我发现
无法自动连线。未找到RestTemplate类型的bean


是否可以自动连接Spring引导类?或者我应该只是实例化
RequestPublicKey
RequestPublicKey RequestPublicKey=new RequestPublicKey()

如评论中所述,Spring不会自动为您提供RestTemplate。所以你要么自己设置,比如

@Configuration
public class MyConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
或者使用由Spring(boot)提供的RestTemplateBuilder,这样可以让Spring将其注入到代码中

@Component
public class RequestPublicKey {

    private final RestTemplate restTemplate;

    @Autowired
    public RequestPublicKey(RestTemplateBuilder restTemplateBuilder) {
         this.restTemplate = restTemplateBuilder.build();
    }
    ...
这有一些好处,比如Spring为您的设置了HttpMessageConverters,您可以使用RestTemplateCustomizer。有关更多信息,请参阅中的


希望这能帮上忙。

我现在可以用了。我有两个关于自动布线的问题。第一个问题是需要一些额外的设置才能从
GenericFilterBean
自动连线。我找到了一个方法来设置它和。第二个问题是如何自动连接
restemplate
。我和他一起去的。谢谢你的帮助。

你在配置中定义了
restemplate
bean了吗?我想你有not@Mr.Arjun. 没有,我还没有在配置中定义它。你有没有这样的例子?抱歉,我刚刚开始学习Spring引导和Spring。您可以编辑问题并添加配置代码吗?不需要将
RestTemplate
制作成Springbean。但您的意思是,如果您自动连线
RequestPublicKey
,则它为空。事实不应如此。你能展示一下自动连接了
RequestPublicKey
的类吗?尝试在RequestPublicKey上添加构造函数谢谢,我会试试的。我现在意识到,我的问题出现在我尝试从
JWTAuthenticationFilter
自动连线
RequestPublicKey
时。我试着直接在
JWTAuthenticationFilter
中运行
restemplate
,结果成功了。