java添加方面日志引发bean示例化异常

nvbavucw  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(663)

我有一个简单的服务,我想通过springaop做日志记录。我在运行它的时候得到了bean,我已经在控制器中定义了restemplate的bean,如果这里遗漏了什么,请给出建议。
堆栈跟踪:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo1Controller': Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [com/test/demo1/controller/Demo1Controller.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Circular reference involving containing bean 'demo1Controller' - consider declaring the factory method as static for independence from its containing instance. Factory method 'restTemplate' threw exception; nested exception is java.lang.NullPointerException

我的方面类demohttpspect.java

@Aspect
@Component
@Order(1)
public class DemoHttpAspect {

    private final static Logger logger = LoggerFactory.getLogger(DemoHttpAspect.class);

    @After("execution(public * com.test.demo1.controller.*.*(..))")
    public void logAfter() throws Throwable {
        System.out.println(SplunkLogImpl.of("Performance Logging via AspectJ, Into DemoAspect"));
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        HttpAspectCore.controllerEvents(request);
        System.out.println(SplunkLogImpl.of("Performance Logging via AspectJ, Out of CoreAspect"));
    }

}

我的控制器类

package com.test.demo1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class Demo1Controller {

    @Autowired
    RestTemplate restTemplate;

    @Bean
    public RestTemplate restTemplate()
    {
        return new RestTemplate();
    }

    @RequestMapping("/demo1/name")
    public String getMicroserviceName()
    {
          String micro2Response = restTemplate.postForObject("http://localhost:8084/service/sleuth", null, String.class);
          System.out.println("getMicroserviceName");
        return "Demo" + " : " + micro2Response ;
    }

    @GetMapping("/demo1/sleuth")
    public String helloSleuth(@RequestHeader HttpHeaders headers) {
        System.out.println("helloSleuth");
        System.out.println("headers.getClass()"+headers.getClass());
        return "success demo1";
    }
}
svujldwt

svujldwt1#

demo1controller依赖于resttemplate,但它也将其定义为LiteBean。因此,您有一个循环依赖关系—您需要demo1controller来创建resttemplate,但demo1controller需要resttemplate。
您有两种解决方案:
将@bean定义移动到@configuration类
使方法静态化。
在这种情况下,建议使用第一种解决方案。

@Configuration
public class DepsConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@RestController
public class Demo1Controller {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/demo1/name")
    public String getMicroserviceName()
    {
          String micro2Response = restTemplate.postForObject("http://localhost:8084/service/sleuth", null, String.class);
          System.out.println("getMicroserviceName");
        return "Demo" + " : " + micro2Response ;
    }

    @GetMapping("/demo1/sleuth")
    public String helloSleuth(@RequestHeader HttpHeaders headers) {
        System.out.println("helloSleuth");
        System.out.println("headers.getClass()"+headers.getClass());
        return "success demo1";
    }
}

因为您使用的是springboot,所以我假设您使用默认的componentscanning,这意味着depsconfig应该位于定义应用程序的包(或子包)中。
我建议您查看有关ioc如何使用spring以及如何定义bean的文档。以及spring boot自动配置的工作原理。

相关问题