Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the dynamic Generation Interface of spring boot

Shulou Source: shulou.com Published: 2022-06-02 09:45:36 10月14日 Update

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!

Tags: Interface dynamic business generation agent content scenario actual method annotation logic project learning practical deeper tripartite code interest principle parameter Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux MariaDB Docker Shulou Information