最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring?boot?動(dòng)態(tài)生成接口實(shí)現(xiàn)類的場(chǎng)景分析

 更新時(shí)間:2021年11月29日 11:44:35   作者:mysgk  
本文不具體介紹動(dòng)態(tài)代理,主要看一下它在springboot項(xiàng)目中的實(shí)際應(yīng)用,下面我們模仿feign來(lái)實(shí)現(xiàn)一個(gè)調(diào)用三方接口的?httpclient,感謝的朋友跟隨小編一起看看吧

在某些業(yè)務(wù)場(chǎng)景中,我們只需要業(yè)務(wù)代碼中定義相應(yīng)的接口或者相應(yīng)的注解,并不需要實(shí)現(xiàn)對(duì)應(yīng)的邏輯。

比如 mybatis和feign: 在 mybatis 中,我們只需要定義對(duì)應(yīng)的mapper接口;在 feign 中,我們只需要定義對(duì)應(yīng)業(yè)務(wù)系統(tǒng)中的接口即可。

那么在這種場(chǎng)景下,具體的業(yè)務(wù)邏輯時(shí)怎么執(zhí)行的呢,其實(shí)原理都是動(dòng)態(tài)代理。

我們這里不具體介紹動(dòng)態(tài)代理,主要看一下它在springboot項(xiàng)目中的實(shí)際應(yīng)用,下面我們模仿feign來(lái)實(shí)現(xiàn)一個(gè)調(diào)用三方接口的 httpclient。

一: 定義注解

package com.mysgk.blogdemo.annotation;

public @interface MyHttpClient {
}

二: 建立動(dòng)態(tài)代理類

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 ..";
	}
}

三: 注入spring容器

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ù)的處理過(guò)程
		 */
		ResponseEntity<?> exchange = ribbonLoadBalanced.exchange("url", HttpMethod.GET, HttpEntity.EMPTY, Object.class);
		return exchange.getBody();
	}

}

五: 新建測(cè)試類

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);

}

項(xiàng)目結(jié)構(gòu):

到此這篇關(guān)于spring boot 動(dòng)態(tài)生成接口實(shí)現(xiàn)類的文章就介紹到這了,更多相關(guān)spring boot 接口實(shí)現(xiàn)類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring IOC創(chuàng)建對(duì)象的兩種方式

    Spring IOC創(chuàng)建對(duì)象的兩種方式

    這篇文章主要給大家介紹了關(guān)于Spring IOC創(chuàng)建對(duì)象的兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Spring?cloud?Hystrix注解初始化源碼過(guò)程解讀

    Spring?cloud?Hystrix注解初始化源碼過(guò)程解讀

    這篇文章主要為大家介紹了Hystrix初始化部分,我們從源碼的角度分析一下@EnableCircuitBreaker以及@HystrixCommand注解的初始化過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2023-12-12
  • Mybatis中傳遞多個(gè)參數(shù)的4種方法總結(jié)

    Mybatis中傳遞多個(gè)參數(shù)的4種方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于Mybatis中傳遞多個(gè)參數(shù)的4種方法,并且介紹了關(guān)于使用Mapper接口時(shí)參數(shù)傳遞方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • 基于@Valid和@Validated驗(yàn)證List集合的踩坑記錄

    基于@Valid和@Validated驗(yàn)證List集合的踩坑記錄

    這篇文章主要介紹了基于@Valid和@Validated驗(yàn)證List集合的踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java棧和基礎(chǔ)隊(duì)列的實(shí)現(xiàn)詳解

    Java棧和基礎(chǔ)隊(duì)列的實(shí)現(xiàn)詳解

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)中的棧與隊(duì)列,在Java的時(shí)候,對(duì)于棧與隊(duì)列的應(yīng)用需要熟練的掌握,這樣才能夠確保Java學(xué)習(xí)時(shí)候能夠有扎實(shí)的基礎(chǔ)能力。本文小編就來(lái)詳細(xì)說(shuō)說(shuō)Java中的棧與隊(duì)列,需要的朋友可以參考一下
    2022-02-02
  • 如何解決HttpServletRequest.getInputStream()多次讀取問(wèn)題

    如何解決HttpServletRequest.getInputStream()多次讀取問(wèn)題

    這篇文章主要介紹了如何解決HttpServletRequest.getInputStream()多次讀取問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Spring中的監(jiān)聽(tīng)器SpringApplicationRunListener詳解

    Spring中的監(jiān)聽(tīng)器SpringApplicationRunListener詳解

    這篇文章主要介紹了Spring中的監(jiān)聽(tīng)器SpringApplicationRunListener詳解,命名我們就可以知道它是一個(gè)監(jiān)聽(tīng)者,分析springboot啟動(dòng)流程我們會(huì)發(fā)現(xiàn),它其實(shí)是用來(lái)在整個(gè)啟動(dòng)流程中接收不同執(zhí)行點(diǎn)事件通知的監(jiān)聽(tīng)者,需要的朋友可以參考下
    2023-11-11
  • Java?Map初始化并賦值的幾種簡(jiǎn)短寫法

    Java?Map初始化并賦值的幾種簡(jiǎn)短寫法

    在Java中初始化一個(gè)Map并賦值可以通過(guò)多種方式完成,下面這篇文章主要介紹了Java?Map初始化并賦值的幾種簡(jiǎn)短寫法,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2025-03-03
  • SpringBootAdmin+actuator實(shí)現(xiàn)服務(wù)監(jiān)控

    SpringBootAdmin+actuator實(shí)現(xiàn)服務(wù)監(jiān)控

    這篇文章主要為大家詳細(xì)介紹了SpringBootAdmin+actuator實(shí)現(xiàn)服務(wù)監(jiān)控,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • java 數(shù)值類型分秒時(shí)間格式化的實(shí)例代碼

    java 數(shù)值類型分秒時(shí)間格式化的實(shí)例代碼

    這篇文章主要介紹了java 數(shù)值類型分秒時(shí)間格式化的實(shí)例代碼的相關(guān)資料,將秒或分鐘的值轉(zhuǎn)換為xx天xx小時(shí)xx分鐘xx秒 如果 “xx” 為0 自動(dòng)缺省,需要的朋友可以參考下
    2017-07-07

最新評(píng)論

蓬溪县| 万年县| 泽普县| 永兴县| 蒙城县| 通河县| 德庆县| 石河子市| 文登市| 中卫市| 金湖县| 济南市| 孝感市| 庆阳市| 平陆县| 碌曲县| 洛浦县| 荣昌县| 治多县| 万山特区| 奉化市| 陵水| 丹棱县| 南漳县| 岢岚县| 江孜县| 隆昌县| 隆化县| 无棣县| 汤原县| 彭阳县| 平顶山市| 怀来县| 沈丘县| 裕民县| 安阳县| 锦屏县| 华容县| 丰台区| 湖北省| 西安市|