Skip to content

Commit

Permalink
fix(#139): To ensure compatibility with Spring proxy, utilize JDK pro…
Browse files Browse the repository at this point in the history
…xy instead of Byte Buddy when creating the mapper proxy.
  • Loading branch information
GangCheng committed Jan 20, 2024
1 parent 9ec5863 commit 0bb852e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pro.chenggang.project.reactive.mybatis.support.r2dbc.spring.application.mapper.repository;

import org.springframework.stereotype.Repository;
import reactor.core.publisher.Mono;

/**
* @author Gang Cheng
* @version 1.0.0
* @since 1.0.0
*/
@Repository
public interface SimpleQueryRepository {

Mono<Long> countAllDept();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pro.chenggang.project.reactive.mybatis.support.r2dbc.spring.test.mapper.repository;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import pro.chenggang.project.reactive.mybatis.support.r2dbc.spring.application.MybatisR2dbcApplication;
import pro.chenggang.project.reactive.mybatis.support.r2dbc.spring.application.mapper.repository.SimpleQueryRepository;
import pro.chenggang.project.reactive.mybatis.support.r2dbc.spring.test.MybatisR2dbcApplicationTests;
import reactor.test.StepVerifier;

/**
* @author Gang Cheng
* @version 1.0.0
* @since 1.0.0
*/
@SpringBootTest(classes = MybatisR2dbcApplication.class)
public class SimpleQueryRepositoryTests extends MybatisR2dbcApplicationTests {

@Autowired
SimpleQueryRepository simpleQueryRepository;

@Test
void countAll() {
simpleQueryRepository.countAllDept()
.as(StepVerifier::create)
.expectNext(4L)
.verifyComplete();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="pro.chenggang.project.reactive.mybatis.support.r2dbc.spring.application.mapper.repository.SimpleQueryRepository">

<select id="countAllDept" resultType="java.lang.Long">
SELECT COUNT(*) FROM dept
</select>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package pro.chenggang.project.reactive.mybatis.support.r2dbc.binding;

import pro.chenggang.project.reactive.mybatis.support.r2dbc.ReactiveSqlSession;
import pro.chenggang.project.reactive.mybatis.support.r2dbc.support.ProxyInstanceFactory;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -68,10 +68,8 @@ public Map<Method, MapperProxy.MapperMethodInvoker> getMethodCache() {
* @return the t
*/
protected T newInstance(MapperProxy<T> mapperProxy) {
return ProxyInstanceFactory.newInstanceOfInterfaces(
mapperInterface,
() -> mapperProxy
);
// #139 To ensure compatibility with Spring proxy, using JDK proxy instead of Byte Buddy when creating the mapper proxy.
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import pro.chenggang.project.reactive.mybatis.support.r2dbc.ReactiveSqlSession;
import pro.chenggang.project.reactive.mybatis.support.r2dbc.builder.R2dbcMapperAnnotationBuilder;
Expand Down Expand Up @@ -64,15 +63,6 @@ void testWithBindingInterface() {
assertNotNull(bindingInterface);
}

@Test
void testWithBindingClass() {
Assertions.assertThrowsExactly(IllegalStateException.class, () -> {
MapperProxyFactory<BindingClass> interfaceMapperProxyFactory = new MapperProxyFactory<>(BindingClass.class);
assertEquals(interfaceMapperProxyFactory.getMapperInterface(), BindingClass.class);
BindingClass bindingClass = interfaceMapperProxyFactory.newInstance(mockReactiveSqlSession);
});
}

@Test
void testOkInterfaceMethod() {
MapperProxyFactory<OkInterface> interfaceMapperProxyFactory = new MapperProxyFactory<>(
Expand Down

0 comments on commit 0bb852e

Please sign in to comment.