注释之前的Spring AOP不起作用

时间:2017-05-23 17:30:22

标签: java spring spring-boot spring-aop

我正在尝试使用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>

Application.properties

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

主要:

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

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

}

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

1 个答案:

答案 0 :(得分:5)

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

上面的代码是问题,准确地说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();
}

这将通过应用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方法而不必亲自获取它。