【发布时间】: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