【发布时间】: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 类调用示例方法,但在执行此方法之前,我的方面方法应该被执行,但它不起作用我不确定我是否需要添加一些配置。
【问题讨论】:
-
正常工作。您正在自己创建一个新实例,您应该使用应用程序上下文创建的实例。
-
@M.Deinum 你能帮忙回答这个问题吗transaction-management-does-not-working-when-using-configuration-and-aspectj-in,请
标签: java spring spring-boot spring-aop