In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to implement the spring boot dynamic generation interface". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to implement the spring boot dynamic generation interface"!
In some business scenarios, we only need to define the corresponding interfaces or annotations in the business code, and we do not need to implement the corresponding logic.
For example, mybatis and feign: in mybatis, we only need to define the corresponding mapper interface; in feign, we only need to define the interface in the corresponding business system.
So in this scenario, how to implement the specific business logic, in fact, the principle is dynamic proxy.
We will not specifically introduce the dynamic proxy here, but mainly take a look at its practical application in the springboot project. Let's imitate feign to implement a httpclient that calls the three-party interface.
1: define annotations package com.mysgk.blogdemo.annotation;public @ interface MyHttpClient {} 2: create dynamic proxy class 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 implements FactoryBean, InvocationHandler {private Class interfaceClass; public Class getInterfaceClass () {return interfaceClass } public void setInterfaceClass (Class 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;} / * * method actually executed * / @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {return "invoke" + proxy.getClass (). GetName () + "." + method.getName () + ", do anything." }} III: inject spring container 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;@Componentpublic class ScanHttpClients implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {private final Logger logger = LoggerFactory.getLogger (ScanHttpClients.class); private ApplicationContext ctx Public void run (BeanDefinitionRegistry registry) {Set exchange = ribbonLoadBalanced.exchange ("url", HttpMethod.GET, HttpEntity.EMPTY, Object.class); return exchange.getBody ();}} V: create a new test class 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 MyHttpClientpublic interface MyHttpClientTest {@ PostMapping (value = "test/t1") Object test (String param);}
Project structure:
At this point, I believe you have a deeper understanding of "how to implement the spring boot dynamic generation interface". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.