程序里有一段,需要后置切面.
结果不生效,
结论: 要交给spring管理.
1.启动类,或者配置文件里添加注解
@Slf4j
@SpringBootApplication
public class TestSpringBootApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(TestSpringBootApplication.class);
application.run(args);
}
}
package top.tsyz.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy //这个放到配置类.或者 启动类.都行
public class AspectConfig {
// 这里可以不需要其他配置,只需确保被扫描到即可
}
package top.tsyz.controller;
import top.tsyz.pojo.User;
import top.tsyz.service.IGoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/test")
public class TestController {
@Resource
private IGoService goService;
@PostMapping("/commit")
public String commitTransmit(@RequestBody User user) {
return aspectCommit(user);
}
public String aspectCommit(User user) {
List<Object> list = goService.aspectCommit(user);
String s = (String) list.get(0);
return s;
}
}
package top.tsyz.service;
import top.tsyz.pojo.User;
import java.util.List;
public interface IGoService {
//已经不用了.放在这里是为了对比,仅仅改动了 String --> List<Object>
// String commit(User user);
//现在是调用这个.
List<Object> aspectCommit(User user);
}
package top.tsyz.service.impl;
import com.google.common.collect.Lists;
import top.tsyz.service.IGoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import top.tsyz.pojo.User;
@Slf4j
@Service
public class GoServiceImpl implements IGoService {
//原来是这个.但是不满足我的要求,已经不再被调用,
//这里本想着 套一层,结果发现还是无法被切面.
/* @Override
public String commit(User user) {
List<Object> list = aspectCommit(user);
return (String) list.get(0);
} */
//为了多返回几个值
@Override
public List<Object> aspectCommit(User user) {
System.out.println("打印::::" + user);
// 从直接返回,到现在的多加了几个参数,给切面用.
ArrayList<Object> list = Lists.newArrayList();
list.add(user.toString());//原来正常返回参数
list.add(selectUU(2));//我需要的参数,
list.add(selectUU(3));//我需要的参数,
return list;
}
User selectUU(int i) {
User user = new User();
user.setName("姓名+" + i);
user.setAge(1 + i);
return user;
}
}
package top.tsyz.aspect;
import top.tsyz.pojo.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.List;
@Aspect
@Component
public class TestAspect {
@AfterReturning(value = "execution(* top.tsyz.service.impl.GoServiceImpl.aspectCommit(..))", returning = "result")
//JoinPoint 是类的相关信息. 具体百度.我这里没使用.
//result 是返回值的相关信息
public void aspectCommit(JoinPoint joinPoint, Object result) {
System.out.println("aspectCommit 执行后置通知");
// 在这里可以对方法返回的结果进行处理
if (result instanceof List) {
List<Object> users = (List<Object>) result;
//这里就可以获取多个参数.给我的切面用了.
System.out.println(users.get(0));//这是原来正常返回的
User user2 = (User) users.get(1);//这是我需要的参数
User user3 = (User) users.get(2);//这是我需要的参数
System.out.println(user2);
System.out.println(user3);
}
}
}
- 我曾经尝试过,在GoServiceImpl 类里直接 直接新建个方法,(private List aspectCommit),结果无法跳转过去. 改成了public
- 在GoServiceImpl 类中,(public List aspectCommit) 还是不行,测试,没又切到(这时候开始查问题)
- 没有在GoServiceImpl 类 commit 方法直接改,因为会影响正常业务返回值.所以又套了一层.
- 然后在想,我要切的在service.impl类里,那我直接反着写到controller 层,在service的改,仅仅是返回值从String–> List 了
- 测试了下.被调正常.能切面就行了.