Cause of problem:
Previously, a single official account was used to develop the WeChat official account. Recently, multiple official accounts need to be configured, but the code written before is not changed on a large scale, so I want to add the default configuration information when obtaining the configuration information of the WeChat official account.
The sdk used to develop the WeChat official account is
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>4.4.0</version> </dependency>
For details, you can check the relevant demo. The writing method in the demo supports multiple official accounts: weixin-java-mp-demo-springboot: WeChat official account Java backend demo based on Spring Boot and WxJava, which supports multiple official accounts
As mentioned earlier, because it was a single official account at the beginning, it was slightly modified. As a result, the use of multiple official accounts now needs to be assigned a default value. Otherwise, the configuration information of the official account will report an error, or both the front and back ends need to be changed, which is time-consuming and laborious.
solution:
Finally, it was decided to use the Spring AOP aspect with annotations to solve the assignment problem.
annotation:
import java.lang.annotation.*;
/**
* 用于给类的属性wxService赋予默认值的注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WxService {
}
AOP aspect:
import java.lang.reflect.Field;
/**
* 对被@WxService修饰的方法所属对象进行属性赋值
*/
@Aspect
@Component
@Slf4j
public class WxServiceAspect {
@Around(value = "@annotation(WxService)")
public Object AfterThrowing(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//log.info("默认赋值第一个公众号信息");
//获取被代理的对象
Object target = proceedingJoinPoint.getTarget();
//获取被代理对象的类型
Class aClass = target.getClass();
//通过反射获取属性数组
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
//设置允许访问
field.setAccessible(true);
//判断属性数组中是否是类型WxMpService的属性
if (field.get(target) instanceof WxMpService){
//获取代理对象的属性值
WxMpServiceImpl o = (WxMpServiceImpl)field.get(target);
//将该属性值进行赋值
WxMpService wxMpService =o;
//如果wxService获取不到配置信息默认添加第一条配置的信息
if (wxMpService.getWxMpConfigStorage()==null) {
wxMpService.switchover(WxConfigIndexEnum.LIE_R_BABY_SERVICE.getValue());
}
//将值放入属性中
field.set(target,wxMpService);
}
}
return proceedingJoinPoint.proceed();
}
}
Finally, add the @WxService annotation to the method that needs to assign a default value to the attribute of the WxMpService type.
Originally, I wanted to add the annotation to the class and assign it when the Controller class was created, so that I don’t have to worry about whether there are missing methods that are not annotated.
However, if you declare the annotation as @Target({ElementType.METHOD,ElementType.TYPE}) and add it to the class, an error will be reported. Due to time problems, no solution has been thought of yet.
If you have a better solution and are willing to share it, please leave a message, thank you.