问答详情
源自:2-2 使用AOP处理请求(中)

没任何日志的出现

package liang.test.aspect;


import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public aspect HttpCliet {

   private final static Logger logger= LoggerFactory.getLogger(HttpCliet.class);
   @Pointcut("execution(public * liang.test.controller.GirlController.*(..))")
   public void log(){
   }

   @Before("log()")
   public void logb(){
       logger.info("befor");
   }
   @After("log()")
   public void log1(){
       logger.info("After");

   }

}

 

package liang.test.controller;

import liang.test.properties.Girl;
import liang.test.repository.GirlsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/girl")
public class GirlController {

   @Autowired
   private Girl girl;
   @Autowired
   private GirlsRepository girlsRepository;

   @PostMapping("/insert")
   public Girl insert(@Valid Girl girl, BindingResult bindingResult){
       if (bindingResult.hasErrors()){
           System.out.println(bindingResult.getFieldError().getDefaultMessage());
           return null;
       }
       girl.setAge(girl.getAge());
       girl.setName(girl.getName());
       return girlsRepository.save(girl);
   }

}

提问者:qq_人不犯er枉少年_03735473 2018-04-04 17:35

个回答

  • Ashwood_Edward
    2018-04-07 16:53:22
    已采纳

    @Aspect
    @Component
    public aspect HttpCliet {

    问题在这里: 类的定义

    解决: public class HttpClient{...}

    望采纳

  • qq_人不犯er枉少年_03735473
    2018-04-07 18:48:00

    com.example.demo.aspectorg.slf4j.Loggerorg.slf4j.LoggerFactoryGirlAspect {
    
        Logger =LoggerFactory.(GirlAspect.)()
        (){
    
        }
    
        (= )
        (){
    
            .info()}
    
        (= )
        (){
            .info()}
    }