【问题标题】:Spring Boot @Aspect J loggingSpring Boot @Aspect J 日志记录
【发布时间】:2016-04-07 02:00:57
【问题描述】:

我使用 REST 控制器创建了测试 Spring MVC 应用程序。我想为我的一些方法应用方面,但是当这个方法被调用时,什么也没有发生,我找不到原因。 这是我的配置和应用程序类:

@SpringBootApplication(scanBasePackages = "org.test")
@EnableAspectJAutoProxy
public class TestaopApplication {

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

这是我的方面类:

@Aspect
@Component
public class Logging {

private static final Logger logger = LoggerFactory.getLogger(GreetingController.class);
@Pointcut("execution(* org.test.restspring.model.Greeting.getCreatedDate(..))")
private void getDate(){}

@Before("getDate()")
public void beforeGettingDate(){
    logger.info("Date is asked");
}

@After("getDate()")
public void afterGettingDate(){
    logger.info("Date is received");
}

}

这是我的简单 bean:

@Component
public class Greeting {

private long id;
private String content;
private Date created;

public Greeting() { }
public Greeting(long id, String content) {
    this.id = id;
    this.content = content;
    this.created = Calendar.getInstance().getTime();
}

public long getId() {
    return id;
}

public String getContent() {
    return content;
}

public String getCreatedDate(){
    return created.toString();
}
 }

这是我的控制器:

@RestController
public class GreetingController {

private static final String template = "Hello, %s!";
private static final Logger logger = LoggerFactory.getLogger(GreetingController.class);
private final AtomicLong counter = new AtomicLong();

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    Greeting response = new Greeting(counter.incrementAndGet(),
            String.format(template, name));
    logger.info(response.getCreatedDate());
    return response;
}


}

请帮我解决这个问题。

【问题讨论】:

    标签: java spring-mvc spring-boot aop


    【解决方案1】:

    通过显式构造函数调用 (new Greeting(...)) 创建的 Bean 不受 Spring 管理。这就是你的方面没有被应用的原因。

    您可以尝试使用原型范围。这样,Spring 会为每个请求创建新的问候 bean 实例。

    对于豆类:

    @Component
    @Scope("prototype")
    public class Greeting {
    

    和你的控制器:

    @RestController
    public class GreetingController {
    
    private static final String template = "Hello, %s!";
    private static final Logger logger = LoggerFactory.getLogger(GreetingController.class);
    private final AtomicLong counter = new AtomicLong();
    
    @Autowired
    private Greeting greeting;
    
    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        greeting.setId(counter.incrementAndGet());
        greeting.setContent(String.format(template, name))
        logger.info(response.getCreatedDate());
        return response;
    }
    
    
    
    }
    

    【讨论】:

    • 我出错了,但是当我在@Pointcut 中将方法替换为不存在时(该方面不适用于方法),我没有错误。这是错误::无法写入 HTTP 消息:org.springframework.http.converter.HttpMessageNotWritableException:无法写入内容:没有找到类 org.springframework.aop.TrueClassFilter 的序列化程序,也没有发现创建 BeanSerializer 的属性(为了避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS) )
    • 我在 Greeting 类中有 getter 和 setter。