为了让mongodbRepository的方法和自己实现的方法,使用同一个类调用。
注解:

标记自定义方法

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Documented
@QueryAnnotation
public @interface SelfMongoMethod {

}

标记实现类

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface MongoImpl {
    /**
     * 实现类
     * @return
     */
    Class<?> value() default Object.class;



}
@Repository
public interface UserRepository extends MongoRepository<User, String>,UserExpandRepository  {

    /**
     * 根据邮箱查询用户和权限
     *
     * @param email
     * @return
     */
    Optional<User> findFirstByEmail(String email);

    /**
     * 根据登陆名查询用户和权限
     *
     * @param login
     * @return
     */
    Optional<User> findFirstByLogin(String login);

    /**
     * 根据登陆名查询
     * @param login
     * @return
     */
    @Query(value = "{login::login}",fields = "{roles:0,appIds:0}")
    Optional<User> findOneByLogin(@Param("login") String login);

    /**
     * 根据邮箱查询
     *
     * @param email
     * @return
     */
    @Query(value = "{email::email}",fields = "{roles:0,appIds:0}")
    Optional<User> findOneByEmail(@Param("email")String email);
}
@MongoImpl(UserRepositoryImpl.class)
public interface UserExpandRepository{
    /**
     //     * @Query("{}")
     //     * 略过mongodb方法名称校验
     //     *
     //     * @param username
     //     * @param age
     //     * @return
     //     */
    @SelfMongoMethod
    List<User> findMy(@Param("username")String username, @Param("age")int age);

}
@Service
public class UserRepositoryImpl implements UserExpandRepository{

    @Autowired
    private MongoTemplate mongoTemplate;
//
    @Override
    public List<User> findMy(@Param("username")String username, @Param("age")int age){
        return mongoTemplate.findAll(User.class);
    }

}
@Aspect
@Component
public class MongoImplAop {
    private static final Logger logger = LoggerFactory.getLogger(MongoImplAop.class);


    @Autowired
    private SpringContextsUtil springContextsUtil;


    /**
     * 定义一个切点
     */
    @Pointcut(value = "@annotation(com.iflytek.cloudbaseserver.repository.SelfMongoMethod)")
    public void annotationPointCut() {
    }

    //声明一个advice,并使用@pointCut定义的切点
    @Around("annotationPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MongoImpl clazz = AnnotationUtils.findAnnotation(((MethodSignature) point.getSignature()).getMethod().getDeclaringClass(), MongoImpl.class);
        String impClassName = clazz.value().toString();

        //获取注解
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();

        //目标类、方法
        String className = method.getDeclaringClass().getName();
        String name = method.getName();
        method.getParameterAnnotations();

        SelfMongoMethod mongoJs = method.getAnnotation(SelfMongoMethod.class);
        System.out.println(method.getName());
        Method implMethod = ReflectionUtils.findMethod(clazz.value(), method.getName(), method.getParameterTypes());
        implMethod.setAccessible(true);
        return ReflectionUtils.invokeMethod(implMethod, springContextsUtil.getBean(clazz.value()), point.getArgs());
    }
}

调用

@Autowired
    private UserRepository userRepository;

   

    @Test
    public void test(){
      List<User> userList =  userRepository.findMy("admin",20);
        System.out.println(GsonUtil.toJson(userList));
    }
```java