注释之前的Spring AOP不起作用

KSK*_*KSK 1 java spring spring-aop spring-boot

我正在尝试使用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>
Run Code Online (Sandbox Code Playgroud)

Application.properties

server.port=6500
spring.aop.proxy-target-class=true
Run Code Online (Sandbox Code Playgroud)

主要:

包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();

    }
}
Run Code Online (Sandbox Code Playgroud)

样品服务:

package com.techno.theater.services;

import org.springframework.stereotype.Service;

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

Aspect类

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");
    }

}
Run Code Online (Sandbox Code Playgroud)

这里我从DigitalTheaterApplication类调用sample方法但是在执行这个方法之前我应该​​执行aspect方法但是它不起作用我不确定是否需要添加一些配置.

M. *_*num 5

public static void main(String[] args) {
    SpringApplication.run(DigitalTheaterApplication.class, args);
    new SampleService().sample();
}
Run Code Online (Sandbox Code Playgroud)

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

相反,你应该做的是SampleService从中检索ApplicationContext.

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

这将通过应用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();
}
Run Code Online (Sandbox Code Playgroud)

像这样的东西也会调用sampleSpring托管实例上的方法,而不必自己搞定.