【问题标题】:Spring AOP before annotation is not working注释之前的Spring AOP不起作用
【发布时间】:2017-05-23 17:30:21
【问题描述】:

我正在尝试使用 Spring Boot 实现 AOP 概念。但在注释不起作用之前。 这是我的代码,

POM.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

应用程序属性

server.port=6500
spring.aop.proxy-target-class=true

主要:

包 com.techno.theater;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.techno.theater.services.SampleService;

@SpringBootApplication
public class DigitalTheaterApplication {

     private static  Logger logger=LoggerFactory.getLogger(DigitalTheaterApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(DigitalTheaterApplication.class, args);
        new SampleService().sample();

    }
}

示例服务:

package com.techno.theater.services;

import org.springframework.stereotype.Service;

@Service
public class SampleService {
    public void sample(){
        System.out.println("Sample method inovking");
    }
}

方面类

package com.techno.theater.aop;

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

@Aspect
@Component
public class AspectService {

    @Before("execution(* com.techno.theater.services.SampleService.sample())")
    public void beforeSampleMethod() {
        System.out.println("Sample method aspect");
    }

}

这里我从 DigitalTheaterApplication 类调用示例方法,但在执行此方法之前,我的方面方法应该被执行,但它不起作用我不确定我是否需要添加一些配置。

【问题讨论】:

标签: java spring spring-boot spring-aop


【解决方案1】:
public static void main(String[] args) {
    SpringApplication.run(DigitalTheaterApplication.class, args);
    new SampleService().sample();
}

上面的代码是问题所在,确切地说,new SampleService().sample(); 是您代码中的缺陷。您正在 Spring 范围之外创建一个新实例,因此它不会暴露给 AOP。

您应该做的是从ApplicationContext 中检索SampleService

public static void main(String[] args) {
    ApplicationContext ctx = SpringApplication.run(DigitalTheaterApplication.class, args);
    ctx.getBean(SampleService.class).sample();
}

这将创建并代理应用 AOP 的 Spring 实例。

另一种不搞乱ApplicationContext 的方法是创建一个CommandLineRunner,它将在启动期间执行。

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

@Bean
public CommandLineRunner tester(SampleService service) {
    return args -> service.sample();
}

类似的东西也会在 Spring 托管实例上调用 sample 方法,而无需自己获取。

【讨论】:

  • 嗨,Deinum,现在我有了一张生动的照片。谢谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 2015-10-26
相关资源
最近更新 更多