我們一說(shuō)到spring,可能第一個(gè)想到的是 IOC
(控制反轉(zhuǎn)) 和 AOP
(面向切面編程)。
沒(méi)錯(cuò),它們是spring的基石,得益于它們的優(yōu)秀設(shè)計(jì),使得spring能夠從眾多優(yōu)秀框架中脫穎而出。
除此之外,我們?cè)谑褂胹pring的過(guò)程中,有沒(méi)有發(fā)現(xiàn)它的擴(kuò)展能力非常強(qiáng)
。由于這個(gè)優(yōu)勢(shì)的存在,讓spring擁有強(qiáng)大的包容能力,讓很多第三方應(yīng)用能夠輕松投入spring的懷抱。比如:rocketmq、mybatis、redis等。
今天跟大家一起聊聊,在Spring中最常用的11個(gè)擴(kuò)展點(diǎn)。
1.自定義攔截器
spring mvc攔截器根spring攔截器相比,它里面能夠獲取HttpServletRequest
和HttpServletResponse
等web對(duì)象實(shí)例。
spring mvc攔截器的頂層接口是:HandlerInterceptor
,包含三個(gè)方法:
- preHandle 目標(biāo)方法執(zhí)行前執(zhí)行
- postHandle 目標(biāo)方法執(zhí)行后執(zhí)行
- afterCompletion 請(qǐng)求完成時(shí)執(zhí)行
為了方便我們一般情況會(huì)用HandlerInterceptor
接口的實(shí)現(xiàn)類(lèi)HandlerInterceptorAdapter
類(lèi)。
假如有權(quán)限認(rèn)證、日志、統(tǒng)計(jì)的場(chǎng)景,可以使用該攔截器。
第一步,繼承HandlerInterceptorAdapter
類(lèi)定義攔截器:
public class AuthInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String requestUrl = request.getRequestURI();
if (checkAuth(requestUrl)) {
return true;
}
return false;
}
private boolean checkAuth(String requestUrl) {
System.out.println("===權(quán)限校驗(yàn)===");
return true;
}
}
第二步,將該攔截器注冊(cè)到spring容器:
@Configuration
public class WebAuthConfig extends WebMvcConfigurerAdapter {
@Bean
public AuthInterceptor getAuthInterceptor() {
return new AuthInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthInterceptor());
}
}
第三步,在請(qǐng)求接口時(shí)spring mvc通過(guò)該攔截器,能夠自動(dòng)攔截該接口,并且校驗(yàn)權(quán)限。
2.獲取Spring容器對(duì)象
在我們?nèi)粘i_(kāi)發(fā)中,經(jīng)常需要從Spring容器中獲取Bean,但你知道如何獲取Spring容器對(duì)象嗎?
2.1 BeanFactoryAware接口
@Service
public class PersonService implements BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public void add() {
Person person = (Person) beanFactory.getBean("person");
}
}
實(shí)現(xiàn)BeanFactoryAware
接口,然后重寫(xiě)setBeanFactory
方法,就能從該方法中獲取到spring容器對(duì)象。
2.2 ApplicationContextAware接口
@Service
public class PersonService2 implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void add() {
Person person = (Person) applicationContext.getBean("person");
}
}
實(shí)現(xiàn)ApplicationContextAware
接口,然后重寫(xiě)setApplicationContext
方法,也能從該方法中獲取到spring容器對(duì)象。
2.3 ApplicationListener接口
@Service
public class PersonService3 implements ApplicationListener<ContextRefreshedEvent> {
private ApplicationContext applicationContext;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
applicationContext = event.getApplicationContext();
}
public void add() {
Person person = (Person) applicationContext.getBean("person");
}
}
3.全局異常處理
以前我們?cè)陂_(kāi)發(fā)接口時(shí),如果出現(xiàn)異常,為了給用戶(hù)一個(gè)更友好的提示,例如:
@RequestMapping("/test")
@RestController
public class TestController {
@GetMapping("/add")
public String add() {
int a = 10 / 0;
return "成功";
}
}
如果不做任何處理請(qǐng)求add接口結(jié)果直接報(bào)錯(cuò):
what?用戶(hù)能直接看到錯(cuò)誤信息?
這種交互方式給用戶(hù)的體驗(yàn)非常差,為了解決這個(gè)問(wèn)題,我們通常會(huì)在接口中捕獲異常:
@GetMapping("/add")
public String add() {
String result = "成功";
try {
int a = 10 / 0;
} catch (Exception e) {
result = "數(shù)據(jù)異常";
}
return result;
}
接口改造后,出現(xiàn)異常時(shí)會(huì)提示:“數(shù)據(jù)異?!?,對(duì)用戶(hù)來(lái)說(shuō)更友好。
看起來(lái)挺不錯(cuò)的,但是有問(wèn)題。。。
如果只是一個(gè)接口還好,但是如果項(xiàng)目中有成百上千個(gè)接口,都要加上異常捕獲代碼嗎?
答案是否定的,這時(shí)全局異常處理就派上用場(chǎng)了:RestControllerAdvice
。
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public String handleException(Exception e) {
if (e instanceof ArithmeticException) {
return "數(shù)據(jù)異常";
}
if (e instanceof Exception) {
return "服務(wù)器內(nèi)部異常";
}
retur nnull;
}
}
只需在handleException
方法中處理異常情況,業(yè)務(wù)接口中可以放心使用,不再需要捕獲異常(有人統(tǒng)一處理了)。真是爽歪歪。
-
spring
+關(guān)注
關(guān)注
0文章
340瀏覽量
15064 -
AOP
+關(guān)注
關(guān)注
0文章
41瀏覽量
11254 -
IOC
+關(guān)注
關(guān)注
0文章
28瀏覽量
10368
發(fā)布評(píng)論請(qǐng)先 登錄
Spring框架的設(shè)計(jì)理念
單片機(jī)系統(tǒng)中最常用的三種通信協(xié)議
開(kāi)關(guān)電源中最常用的電阻有哪些呢
單片機(jī)系統(tǒng)中最常用的通信協(xié)議有幾種
EMC問(wèn)題中最常用的手段RC濾波詳細(xì)資料說(shuō)明

聯(lián)網(wǎng)應(yīng)用中最常用的傳感器是什么?
Spring中最常用的11個(gè)擴(kuò)展點(diǎn)
剖析Spring中最常用的擴(kuò)展點(diǎn)(中)

評(píng)論