qq空间做宣传网站,网站开发亿码酷流量,设置网站的默认文档,网站维护费用怎么收SpringBoot多数据源之AOP#xff08;四#xff09; 1.自定义注解2.切面类3.使用注解 1.自定义注解
这里定义了一个注解WR#xff0c;作用在方法和类上#xff0c;同时注解中value的默认值为W Target({ElementType.METHOD, ElementType.TYPE})
Retention(RetentionPolicy.R… SpringBoot多数据源之AOP四 1.自定义注解2.切面类3.使用注解 1.自定义注解
这里定义了一个注解WR作用在方法和类上同时注解中value的默认值为W Target({ElementType.METHOD, ElementType.TYPE})
Retention(RetentionPolicy.RUNTIME)
public interface WR {String value() default W;
}
2.切面类
导入相关的AOP依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependency定义一个切面类DynamicDataSourceAspect.java
Component
Aspect
public class DynamicDataSourceAspect {Before(within(com.rql.controller.*) annotation(wr))public void before(JoinPoint joinPoint, WR wr) {String namewr.value();DynamicDataSource.name.set(name);System.out.println(name);}
}
3.使用注解
这里在方法上添加了注解
RestController
RequestMapping(user)
public class UserController {Autowiredprivate UserDao userDao;GetMapping(/a)WR(R)public ListUser selectUsers(){return userDao.findAll();}PostMapping(/b)WR(W)public void insertUser(RequestBody User user){userDao.inserUser(user);}}