今天再实践 SpringBoot AOP 事物的时候,无论如何注入的aspet都不生效,原来是自己脑瓜子瓦特了,虽然注解该加的都加上了,但是这个对象却是自己new出来的,而spring是不会 去管理你自己new出来的对象的!!!
【要点】
1.pom依赖aspectj
2.aspect注解以及bean要能够让spring扫描到
3.启动程序中要开启代理@EnableAspectJAutoProxy
【SpringBoot启动类】
package com.app.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass=true)
@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackages={"com.app.demo"})
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
【自定义接口】
package com.app.demo;
public interface Calculate
{
public int div(int x,int y);
public int add(int x,int y);
//Java8 接口默认函数
public default int sum(int x,int y)
{
return x+y;
}
}
【实现类,被切入对象】
package com.app.demo;
import org.springframework.stereotype.Component;
@Component
public class CalculateImplement implements Calculate
{
@Override
public int div(int x, int y)
{
return x-y;
}
@Override
public int add(int x, int y)
{
return x+y;
}
}
【程序执行接口,bean初始化时自动执行】
package com.app.demo;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import lombok.Getter;
import lombok.Setter;
@Component
public class AppRun
{
@Setter
@Getter
@Autowired
private CalculateImplement app;
@PostConstruct
private void run()
{
System.out.print("pre construction start");
app.add(3, 4);
}
}
【切面类,自定义通知】
package com.app.demo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AppAspect
{
//切点,进行重用,该类的任意方法
@Pointcut("execution(* CalculateImplement.*(..))")
public void pointcut()
{
//定义切点的print并没有执行
System.out.print("go here");
};
@Before("pointcut() && args(x,y)")
//@After 无论是否发生异常
//@AfterReturning 正常返回之后
//@AfterThrowing
//@Around 环绕通知
public void beforeMethod(int x,int y)
{
System.out.print("go before method"+"X="+x+"Y="+y);
}
@After("pointcut()")
public void afterMethod(JoinPoint point)
{
System.out.print("go after method");
Object[] args = point.getArgs();
//被切入函数名
String name = point.getSignature().getName();
Class clz=point.getSignature().getDeclaringType();
}
}
【pom依赖】
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.app</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.5.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.5.14.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.14.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>hibernate-validator</artifactId>
<groupId>org.hibernate</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>