Java Spring AOP-方面在没有xml配置的情况下无法工作
我的意图是在服务中运行获取消息方法之前的方面。我不想使用xml配置,所以我(希望)添加了必要的注释。但是,当我运行我的应用程序时,aspect不起作用,什么也没有发生。你能解释一下为什么吗 服务Java Spring AOP-方面在没有xml配置的情况下无法工作,java,spring-boot,spring-aop,Java,Spring Boot,Spring Aop,我的意图是在服务中运行获取消息方法之前的方面。我不想使用xml配置,所以我(希望)添加了必要的注释。但是,当我运行我的应用程序时,aspect不起作用,什么也没有发生。你能解释一下为什么吗 服务 public interface Service { void getMessage(); } 服务实施 import org.springframework.stereotype.Component; @Service public class ServiceImpl implements S
public interface Service {
void getMessage();
}
服务实施
import org.springframework.stereotype.Component;
@Service
public class ServiceImpl implements Service {
public void getMessage() {
System.out.println("Hello world");
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass=true)
@ComponentScan("com.example")
public class AopApplication {
public static void main(String[] args) {
final ConfigurableApplicationContext run = SpringApplication.run(AopApplication.class, args);
final Service bean = run.getBean(ServiceImpl.class);
bean.getMessage();
}
}
方面
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(* com.example.aop.Service.getMessage())")
public void logBefore(JoinPoint joinPoint) {
System.out.println("AOP is working!!!");
}
}
跑步
import org.springframework.stereotype.Component;
@Service
public class ServiceImpl implements Service {
public void getMessage() {
System.out.println("Hello world");
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass=true)
@ComponentScan("com.example")
public class AopApplication {
public static void main(String[] args) {
final ConfigurableApplicationContext run = SpringApplication.run(AopApplication.class, args);
final Service bean = run.getBean(ServiceImpl.class);
bean.getMessage();
}
}
输出
只有
Hello world
我认为切入点表达式语法应该是这样的:
@Before("execution(void com.aop.service.Service+.getMessage(..))")
+
用于将切入点应用于子类型(您也可以用*
替换void
。可能需要将@Component
注释添加到LoggingAspect
类。尝试getBean(Service.class)
相反。仍然不起作用…是简单的polimorphism。可能我使用了错误的注释?方面不应该是@Before(“execution(*com.example.aop.serviceinpl.getMessage())”
?请看这里您的确切意思?我有相同的@Before值和自己的路径。。。