diff --git a/docs/spring/csa0b2c5fa-8129-11ef-ac27-acde48001122.java b/docs/spring/csa0b2c5fa-8129-11ef-ac27-acde48001122.java new file mode 100644 index 00000000..b23373cd --- /dev/null +++ b/docs/spring/csa0b2c5fa-8129-11ef-ac27-acde48001122.java @@ -0,0 +1,73 @@ +/** + * Copyright 2009-2019 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 + *

+ * http://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 org.apache.ibatis.executor.loader; + +import net.sf.cglib.proxy.Factory; +import org.apache.ibatis.domain.blog.Author; +import org.apache.ibatis.executor.ExecutorException; +import org.apache.ibatis.executor.loader.cglib.CglibProxyFactory; +import org.apache.ibatis.reflection.factory.DefaultObjectFactory; +import org.apache.ibatis.session.Configuration; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; + +import static org.junit.jupiter.api.Assertions.*; + +class CglibProxyTest extends SerializableProxyTest { + + @BeforeAll + static void createProxyFactory() { + proxyFactory = new CglibProxyFactory(); + } + + @Test + void shouldCreateAProxyForAPartiallyLoadedBean() throws Exception { + ResultLoaderMap loader = new ResultLoaderMap(); + loader.addLoader("id", null, null); + Object proxy = proxyFactory.createProxy(author, loader, new Configuration(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>()); + Author author2 = (Author) deserialize(serialize((Serializable) proxy)); + assertTrue(author2 instanceof Factory); + } + + @Test + void shouldFailCallingAnUnloadedProperty() { + // yes, it must go in uppercase + HashMap unloadedProperties = new HashMap<>(); + unloadedProperties.put("ID", null); + Author author2 = (Author) ((CglibProxyFactory) proxyFactory).createDeserializationProxy(author, unloadedProperties, new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>()); + Assertions.assertThrows(ExecutorException.class, author2::getId); + } + + @Test + void shouldLetCallALoadedProperty() { + Author author2 = (Author) ((CglibProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>()); + assertEquals(999, author2.getId()); + } + + @Test + void shouldSerizalizeADeserlizaliedProxy() throws Exception { + Object proxy = ((CglibProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>()); + Author author2 = (Author) deserialize(serialize((Serializable) proxy)); + assertEquals(author, author2); + assertNotEquals(author.getClass(), author2.getClass()); + } + +} diff --git a/docs/spring/csa0e3513e-8129-11ef-ac27-acde48001122.java b/docs/spring/csa0e3513e-8129-11ef-ac27-acde48001122.java new file mode 100644 index 00000000..6d44c0a7 --- /dev/null +++ b/docs/spring/csa0e3513e-8129-11ef-ac27-acde48001122.java @@ -0,0 +1,13 @@ +package com.github.huifer.ctrpluginexample.repo; + +import com.github.huifer.ctrpluginexample.entity.AppEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AppRepo extends CrudRepository { + + // 现在我拥有一个 CrudRepository的实现类,我想要获取这个实现类的Class 我应该怎么操作 + +} diff --git a/docs/spring/csa112aab0-8129-11ef-ac27-acde48001122.java b/docs/spring/csa112aab0-8129-11ef-ac27-acde48001122.java new file mode 100644 index 00000000..3e5b48f7 --- /dev/null +++ b/docs/spring/csa112aab0-8129-11ef-ac27-acde48001122.java @@ -0,0 +1,40 @@ +/** + * Copyright 2009-2015 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 + *

+ * http://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 org.apache.ibatis.exceptions; + +import org.apache.ibatis.executor.ErrorContext; + +/** + * 异常工厂 + * @author Clinton Begin + */ +public class ExceptionFactory { + + private ExceptionFactory() { + // Prevent Instantiation + } + + /** + * 异常 + * @param message 异常消息 + * @param e 异常 + * @return + */ + public static RuntimeException wrapException(String message, Exception e) { + return new PersistenceException(ErrorContext.instance().message(message).cause(e).toString(), e); + } + +}