SpringBoot中實(shí)現(xiàn)代理方式
SpringBoot實(shí)現(xiàn)代理
功能
定義一個功能,使指定的方法執(zhí)行前后輸出日志信息。
1. 定義一個注解,添加的方法上具有該功能,或者添加到類上,類下的所有方法都具有該功能
@Target( {ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAround {
}2. 實(shí)現(xiàn)PointCut接口,找出標(biāo)注注解的方法或類
// 這里是直接繼承了一個便捷基類,實(shí)現(xiàn) matches 方法
public class LogAroundPointcut extends StaticMethodMatcherPointcut {
@Override
public boolean matches(Method method, Class<?> targetClass) {
return AnnotatedElementUtils.hasAnnotation(targetClass, LogAround.class) ||
AnnotatedElementUtils.hasAnnotation(method, LogAround.class);
}
}3. 實(shí)現(xiàn) Advice,定義增強(qiáng)行為
// 這里繼承了 MethodInterceptor,方法攔截。前后打印日志
public class LogAroundInterceptor implements MethodInterceptor {
@Nullable
@Override
public Object invoke(@Nonnull MethodInvocation invocation) throws Throwable {
System.out.println("before method invoke log....");
Method method = invocation.getMethod();
Object[] args = invocation.getArguments();
Object target = invocation.getThis();
Object invoke = method.invoke(target, args);
System.out.println("after method invoke log....");
return invoke;
}
}4. 定義一個 Advisor ,把這兩個組合起來,并添加到 Spring 中
@Component
public class LogAroundPointcutAdvisor extends AbstractPointcutAdvisor {
@Override
public Pointcut getPointcut() {
return new LogAroundPointcut();
}
@Override
public Advice getAdvice() {
return new LogAroundInterceptor();
}
@Override
public boolean isPerInstance() {
return false;
}
}5. 測試bean
@Component
public class AopDemo {
@LogAround
public static int sum(int i1, int i2) {
return i1 + i2;
}
}6. 測試結(jié)果
前后有打印日志,代理成功

SpringBoot實(shí)現(xiàn)反向代理
背景:在前后端分離的項(xiàng)目中,有一天后端項(xiàng)目因?yàn)槟承┰虿荒鼙┞对诠W(wǎng)地址,此時為了修改最少,利用反向代理技術(shù)進(jìn)行實(shí)現(xiàn)。
這種不是解決方案,曲線救國。
1. 引入依賴
? <dependency> ?? ? <groupId>org.mitre.dsmiley.httpproxy</groupId> ? ?? ? <artifactId>smiley-http-proxy-servlet</artifactId> ? ??? ? <version>1.12</version> </dependency>
2. 配置文件
將本地項(xiàng)目的 /baidu/* 下的請求轉(zhuǎn)發(fā)至https://baidu.com
/baidu/* 前面一定要加 /
proxy: ? baidu: ? ? url: /baidu/* ? ? target_url: https://baidu.com
3. 配置代理
package athena.gateway.app.banshi;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SolrProxyServletConfiguration {
? ? @Value("${proxy.baidu.url}")
? ? private String url;
? ? @Value("${proxy.baidu.target_url}")
? ? private String targetUrl;
? ? @Bean
? ? public ServletRegistrationBean servletRegistrationBean() {
? ? ? ? ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), url);
? ? ? ? servletRegistrationBean.setName("百度");
? ? ? ? servletRegistrationBean.addInitParameter("targetUri", targetUrl);
? ? ? ? servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, String.valueOf(true));
? ? ? ? return servletRegistrationBean;
? ? }
}總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot基于自定義注解實(shí)現(xiàn)切面編程
這篇文章主要介紹了SpringBoot基于自定義注解實(shí)現(xiàn)切面編程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
詳解Java使用super和this來重載構(gòu)造方法
這篇文章主要介紹了詳解Java使用super和this來重載構(gòu)造方法的相關(guān)資料,這里提供實(shí)例來幫助大家理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
Java實(shí)現(xiàn)將Word和PDF轉(zhuǎn)成一張垂直拼接長圖的工具類
這篇文章主要為大家詳細(xì)介紹了如何使用Java編寫一個將Word和PDF轉(zhuǎn)成一張垂直拼接長圖的工具類,支持doc和docx,有需要的小伙伴可以了解下2025-10-10
SpringBoot+layui實(shí)現(xiàn)文件上傳功能
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。這篇文章主要介紹了SpringBoot+layui實(shí)現(xiàn)文件上傳,需要的朋友可以參考下2018-09-09

