【发布时间】:2017-10-12 13:09:20
【问题描述】:
我正在尝试在 spring boot 项目中使用 spring aop。我不知道为什么我没有在主课的连接点得到建议。 AOP 在服务类中运行良好。代码如下。
SpringBootWithAOP.java
package com.example.demo;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.demo.services.SayHelloService;
@SpringBootApplication
public class SpringBootWithAOP {
@Autowired
SayHelloService service;
String message;
static String name;
public static void main(String[] args) {
name="George";
SpringApplication.run(SpringBootWithAOP.class, args);
}
@PostConstruct
public void init() {
service.message(name);
}
}
SayHelloService.java
package com.example.demo.services;
import org.springframework.stereotype.Service;
@Service
public class SayHelloService {
public String message(String name) {
System.out.println("Hello Dear User - " + name );
return "Hello Dear User - " + name ;
}
}
MasterLoggerAspect.java
package com.example.demo.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MasterLoggerAspect {
@Before("execution(* com.example.demo.services.*.*(..) )")
public void doForEveryServicesMethod(JoinPoint jp){
System.out.println("hurray! In class: " + jp.getSignature().getDeclaringTypeName() + " - before method: " + jp.getSignature().getName());
}
//this advice is not getting weaved
@Before("execution(* com.example.demo.*.*(..)) " )
public void doForEveryMainClassMethod(JoinPoint jp){
System.out.println("hurray! In class: " + jp.getClass() + " - before method: " + jp.getSignature().getName());
}
}
pom.xml 在依赖项中有 spring-boot-starter-aop。
有人能指出问题出在哪里吗?
输出 -
hurray! In class: com.example.demo.services.SayHelloService - before method: message
Hello Dear User - George
【问题讨论】:
标签: spring spring-boot aop aspectj spring-aop