Spring Boot AOP @before不工作

时间:2017-01-27 12:31:40

标签: spring spring-boot

我写了这段代码: 它看起来很完美但不起作​​用 当我运行代码它运行正常 我点击了(/)的URL,只调用了问候方法

AOP方法未被调用

这是我的主要课程

DemoApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aop.AOPSample;
com.services.TaskService;

@SpringBootApplication
@ComponentScan({"com.aop","com.services"})
@RestController
public class DemoApplication {

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



@RequestMapping("/")
public void greetings(){
    new TaskService().startService();
    System.out.println("In Greetings");
}
}

AOPSample.java
package com.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AOPSample {
@Before("execution(public void com.services.TaskService.startService())")
public void beforeSampleCreation() {
    System.out.println("Hello world");
}

}

TaskService.java

package com.services;

public class TaskService {
public void startService(){

}
public void endService(){

}
}

输出:    在问候

0 个答案:

没有答案