繁体   English   中英

注解之前的Spring AOP不起作用

[英]Spring AOP before annotation is not working

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

主要:

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类中调用示例方法,但是在执行此方法之前,应该执行我的Aspect方法,但是不能确定我是否需要添加一些配置。

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方法,而无需自己获取它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM