您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“spring boot動態(tài)生成接口怎么實現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“spring boot動態(tài)生成接口怎么實現(xiàn)”吧!
在某些業(yè)務(wù)場景中,我們只需要業(yè)務(wù)代碼中定義相應(yīng)的接口或者相應(yīng)的注解,并不需要實現(xiàn)對應(yīng)的邏輯。
比如 mybatis和feign: 在 mybatis 中,我們只需要定義對應(yīng)的mapper接口;在 feign 中,我們只需要定義對應(yīng)業(yè)務(wù)系統(tǒng)中的接口即可。
那么在這種場景下,具體的業(yè)務(wù)邏輯時怎么執(zhí)行的呢,其實原理都是動態(tài)代理。
我們這里不具體介紹動態(tài)代理,主要看一下它在springboot項目中的實際應(yīng)用,下面我們模仿feign來實現(xiàn)一個調(diào)用三方接口的 httpclient。
package com.mysgk.blogdemo.annotation; public @interface MyHttpClient { }
package com.mysgk.blogdemo.proxy; import org.springframework.beans.factory.FactoryBean; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class RibbonAopProxyFactory<T> implements FactoryBean<T>, InvocationHandler { private Class<T> interfaceClass; public Class<T> getInterfaceClass() { return interfaceClass; } public void setInterfaceClass(Class<T> interfaceClass) { this.interfaceClass = interfaceClass; } @Override public T getObject() throws Exception { return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{interfaceClass}, this); } @Override public Class<?> getObjectType() { return interfaceClass; } @Override public boolean isSingleton() { return true; } /** 真正執(zhí)行的方法 */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return "invoke " + proxy.getClass().getName() + "." + method.getName() + " , do anything .."; } }
package com.mysgk.blogdemo.start; import cn.hutool.core.util.ClassUtil; import cn.hutool.core.util.StrUtil; import com.mysgk.blogdemo.annotation.MyHttpClient; import com.mysgk.blogdemo.proxy.RibbonAopProxyFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import java.util.Set; @Component public class ScanHttpClients implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware { private final Logger logger = LoggerFactory.getLogger(ScanHttpClients.class); private ApplicationContext ctx; public void run(BeanDefinitionRegistry registry) { Set<Class<?>> scanPackage = ClassUtil.scanPackageByAnnotation("com.mysgk", MyHttpClient.class); for (Class<?> cls : scanPackage) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(cls); GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition(); definition.getPropertyValues().add("interfaceClass", definition.getBeanClassName()); definition.setBeanClass(RibbonAopProxyFactory.class); definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE); String beanName = StrUtil.removePreAndLowerFirst(cls.getSimpleName(), 0) + "RibbonClient"; registry.registerBeanDefinition(beanName, definition); } } @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { run(registry); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.ctx = ctx; } }
package com.mysgk.blogdemo.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component @Aspect public class InterceptAnnotation { @Autowired private RestTemplate ribbonLoadBalanced; @Pointcut("@annotation(com.mysgk.blogdemo.annotation.MyHttpClient)") public void execute() { } @Around("execute()") public Object interceptAnnotation(ProceedingJoinPoint joinPoint) throws Throwable { /** * 此處省略 獲取 url, httpMethod, requestEntity, responseType 等參數(shù)的處理過程 */ ResponseEntity<?> exchange = ribbonLoadBalanced.exchange("url", HttpMethod.GET, HttpEntity.EMPTY, Object.class); return exchange.getBody(); } }
package com.mysgk.blogdemo.client; import com.mysgk.blogdemo.annotation.MyHttpClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @MyHttpClient public interface MyHttpClientTest { @PostMapping(value = "test/t1") Object test(String param); }
項目結(jié)構(gòu):
到此,相信大家對“spring boot動態(tài)生成接口怎么實現(xiàn)”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。