1.全注解配置
所需jar包
<!--mybatis的jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--spring上下文容器-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring整合jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!--mybatis整合spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--spring整合JUnit测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--JUnit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--aspect切面-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
service层接口
public interface UserService {
public void add();
public void delete();
public void update();
public void find();
}
service层接口的实现类
@Configuration
@Service("userService")
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("新增用户");
}
public void delete() {
System.out.println("删除用户");
}
public void update() {
System.out.println("更新用户");
}
public void find() {
System.out.println("查询用户");
}
}
spring的配置类
@Configuration
@ComponentScan("com.*")
@EnableAspectJAutoProxy
public class SpringConfig {}
编写注解通知顺序类
@Component("logAdvice")
@Aspect
public class LogAdvice {
//切入点-用于将方法插入
//此方法也可以抽取成父类
@Pointcut("execution(* com.*.service.impl.*.*(..))")
public void pt1(){}
//在插入目标地之前执行
@Before("pt1()")
public void beforeLog(JoinPoint joinPoint){
//获取切入点方法的参数们
Object[] args = joinPoint.getArgs();
System.out.println(Arrays.asList(args));
//获取目标对象的类名
String simpleName = joinPoint.getTarget().getClass().getSimpleName();
String name = joinPoint.getSignature().getName();
System.out.println(simpleName+":"+name+"方法执行后");
}
//在执行目标地的方法后执行此方法
@AfterReturning("pt1()")
public void afterReturningLog(JoinPoint joinPoint){
//获取目标对象的类名
String simpleName = joinPoint.getTarget().getClass().getSimpleName();
String name = joinPoint.getSignature().getName();
System.out.println(simpleName+":"+name+"方法执行后");
}
// 目的地方法正常执行后执行
@After("pt1()")
public void afterLog(JoinPoint joinPoint){
//获取目标对象的类名
String simpleName = joinPoint.getTarget().getClass().getSimpleName();
String name = joinPoint.getSignature().getName();
System.out.println(simpleName+":"+name+"方法执行结束");
}
//目的地方法执行时抛出异常后执行
@AfterThrowing(value = "pt1()",throwing = "e")
public void afterThrowingLog(JoinPoint joinPoint, Throwable e){
//获取目标对象的类名
String simpleName = joinPoint.getTarget().getClass().getSimpleName();
//获取切入点方法名
String methodName = joinPoint.getSignature().getName();
System.out.println(simpleName+"的"+methodName+"方法执行产生异常后,异常为:"+e.getMessage());
}
//环绕--此方法可以代替其余所有方法
//其中代码执行的顺序有自己指定,顺序和try-catch-finally一致
public Object aroundLog(ProceedingJoinPoint pjp){
String simpleName = pjp.getTarget().getClass().getSimpleName();
Signature signature = pjp.getSignature();
String methodName = signature.getName();
try {
// 前置
System.out.println(simpleName+"的"+methodName+"方法执行前");
Object rtValue = pjp.proceed();
//后置
System.out.println(simpleName+"的"+methodName+"方法正常执行后");
return rtValue;
} catch(Throwable throwable){
System.out.println("Exception 异常");
// 异常
System.out.println(simpleName+"的"+methodName+"方法执行产生异常后");
return null;
} finally{
// 最终
System.out.println(simpleName+"的"+methodName+"方法最终执行结束");
}
}
}
2.spring代理
由上述的userServiceImpl举例
public class UserServiceImlp{
public void save(){
System.out.println("水泥墙");
}
}
提供一个方法,用于指定对象的代理对象
class UserServiceCglibProxy{
//如果你的jdk版本高于1.8这里的实现类不用写final
public static UserServiceImpl2 createUserServiceCglibProxy(final UserServiceImpl2 userService){
UserServiceImpl2 userServiceProxy = (UserServiceImpl2) Enhancer.create(userService.getClass(), new MethodInterceptor() {
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
if (method.getName().equals("save")){
//调用原始的父类方法...
Long startTime = 0L;
Date date = new Date();
startTime = date.getTime();
userService.find();
Object rtValue = method.invoke(userService, args);
Long endTime = date.getTime();
System.out.println("阿巴阿巴");
System.out.println(endTime-startTime);
return rtValue;
}
// 其它方法,原封不动的调用
return method.invoke(userService, args);
}
});
return userServiceProxy;
}
}
测试代码
public static void main(String[] args) {
UserServiceImpl2 userServiceImpl = new UserServiceImpl2();
UserServiceImpl2 userService = UserServiceCglibProxy.createUserServiceCglibProxy(userServiceImpl);
userService.save();
}
切入点表达式
execution(*
execution(*
execution(public
*
execution(public
int
execution(public
void
execution(public
void com..*.*(..))
execution(public
void com..service
execution(public
void com.itheima.service
execution(public
void com.itheima.service.User
execution(public
void com.itheima.service .*
execution(public
void com.itheima.service.UserService
execution(public
User com.itheima.service.UserService.find
execution(public
User com.itheima.service.UserService .*
execution(public
User com.itheima.service.UserService.findById
execution(public
User com.itheima.service.UserService.findById int
execution(public
User com.itheima.service.UserService.findById int,int
execution(public
User com.itheima.service.UserService.findById int
execution(public
User com.itheima.service.UserService.findById int
execution(public
User com.itheima.service.UserService.findById
execution(List
com.itheima.service .* findAll