1. 程式人生 > >Spring中的@ControllerAdvice註解的使用

Spring中的@ControllerAdvice註解的使用

今天在CodeReview的時候,看到李明同學的程式碼中用了ControllerAdvice這個註解,覺得挺有用的,就蒐集了一下他的用法;

@ControllerAdvice,是Spring3.2引入的新註解,意為增強型控制器;

@ControllerAdvice註解內部使用@ExceptionHandler、@InitBinder、@ModelAttribute註解的方法應用到所有的 @RequestMapping註解的方法。

網上的一個例子,我搬過來:

    @ControllerAdvice  
    public class ControllerAdviceTest {  
      
        @ModelAttribute  
        public User newUser() {  
            System.out.println("============應用到所有@RequestMapping註解方法,在其執行之前把返回值放入Model");  
            return new User();  
        }  
      
        @InitBinder  
        public void initBinder(WebDataBinder binder) {  
            System.out.println("============應用到所有@RequestMapping註解方法,在其執行之前初始化資料繫結器");  
        }  
      
        @ExceptionHandler(UnauthenticatedException.class)  
        @ResponseStatus(HttpStatus.UNAUTHORIZED)  
        public String processUnauthenticatedException(NativeWebRequest request, UnauthenticatedException e) {  
            System.out.println("===========應用到所有@RequestMapping註解的方法,在其丟擲UnauthenticatedException異常時執行");  
            return "viewName"; //返回一個邏輯檢視名  
        }  
    }  


相關推薦

spring 使用註解

contex repos 使用註解 qualifier 說明 cast pri web .post 1、要在applicationContext.xml中配置掃描哪個包下的註解 <!-- 指定掃描cn.itcast.bean報下的所有類中的註解. 註意:掃

Spring@Autowired註解、@Resource註解的區別

不用 它的 查找 允許 type 規範 找到 註入 以及 Spring不但支持自己定義的@Autowired註解,還支持幾個由JSR-250規範定義的註解,它們分別是@Resource、@PostConstruct以及@PreDestroy。   @Resource的作

Spring@Component註解,@Controller註解詳解(網摘)

tin ava 代碼 autowired reat control type imp 訪問   在使用Spring的過程中,為了避免大量使用Bean註入的Xml配置文件,我們會采用Spring提供的自動掃描註入的方式, 只需要添加幾行自動註入的的配置,便可以完成Servic

Spring使用註解時啟用<context:component-scan/>

方式 log one tar ati out 文件 ext tail 在spring中使用註解方式時需要在spring配置文件中配置組件掃描器:http://blog.csdn.net/j080624/article/details/56277315 <context

spring註解大全和應用說明

來源:http://t.cn/RFBJugV @Controller @RestController: @Service @Autowired @RequestMapping @RequestParam @

SpringBoot,Spring 常用註解@RequestMapping/@GetMapping/@PostMapping /@PutMapping/@DeleteMapping介紹

1、@Controller @Controller 用來響應頁面,表示當前的類為控制器。 2、@RestController @RestController 是@ResponseBody和@Controller的結合 表明當前類是控制器且返回的是一

Spring 註解彙總

@Controller @RestController: @Service @Autowired @RequestMapping @RequestParam @ModelAttribute @Cacheable @CacheEvict @Resource @PostConstruct @PreDes

Spring原始碼分析(十五)Spring常用註解使用以及原始碼分析

從Java5.0開始,Java開始支援註解。Spring做為Java生態中的領軍框架,從2.5版本後也開始支援註解。相比起之前使用xml來配置Spring框架,使用註解提供了更多的控制Spring框架的方式。 現在越來越多的專案也都在使用註解做相關的配置,但Spring的註解非常多,相信

Spring@Configuration註解使用

    在Spring中使用@Configuration註解載入JavaConfig配置-zifangsky的部落格備份-51CTO部落格   http://blog.51cto.com/983836259/1889610   如

spring註解配置

步驟:1.為主配置檔案引入新的名稱空間(引入約束) 2.開啟使用註解代替配置檔案在spring配置檔案applicationContext中配置 <!-- 指定掃描com.lsz.spring包下的所有類中註解--> <context:component-scan base-pac

Spring@Import註解的作用和使用

@Import用來匯入@Configuration註解的配置類、宣告@Bean註解的bean方法、匯入ImportSelector的實現類或匯入ImportBeanDefinitionRegistra

Spring 註解與分層思想

在Spring框架中最常見的幾個註解 @Controller, @Service, @Component, @Repository 其中@Component是一種通用名稱,泛指任意可以通過Spring來管理的元件,@Controller, @Service, @Rep

Spring主要註解解析

Spring在實現註解開發過程中,或使用到多種註解,方便我們的開發,接下來我們就逐一講解一下,Spring中主要註解的實現原理。 1:@Autowired:我們可以理解為是by type,根據型別來匹配注入的實現類,也可以放入在方法,建構函式上面。 2:@Qual

Spring註解@Service @Component @Controller @Repository區別

@Service用於標註業務層元件,  在 serviceImpl 上註解 @Controller用於標註控制層元件(如struts中的action),  在handler/ mycontroler上註解 @Repository用於標註資料訪問元件,即DAO元件,  在d

Spring -13 -Spring 常用註解總結

[email protected] 建立類物件,相當於配置<bean/> [email protected] 與@Component 功能相同.   2.1都寫在ServiceImpl 類上. [email protected] 與@Component 功

Spring常用註解

[email protected]註解 @Value 為屬性注入值(該註解貼在屬性上) 支援如下方式的注入: (1)注入普通字元 @Value(“Michael Jackson”)String name;(2)注入作業系統屬性 @Value(“#{system

spring註解關鍵字

1、@controller 控制器(注入服務) 2、@service 服務(注入dao) 3、@repository dao(實現dao訪問) 4、@component (把普通pojo例項化到spring容器中,相當於配置檔案中的<bean id="" class=

使用spring註解@Autowired,獲取到的物件為null

最近工作中遇到一個關於@AutoWired註解使用的細節問題,以前沒有注意到,現在特意記錄一下。 問題描述:在我的一個工具類中,使用@Autowired需要注入一個dao物件,然後工具類的方法中會呼叫該dao物件中的方法,在執行時發現報空指標異常,dao物件居然是null。 相關程式碼如下:

Spring@Async註解實現“方法”的非同步呼叫

簡單介紹: Spring為任務排程與非同步方法執行提供了註解支援。通過在方法上設定@Async註解,可使得方法被非同步呼叫。也就是說呼叫者會在呼叫時立即返回,而被呼叫方法的實際執行是交給Spring的TaskExecutor來完成。 開啟@Async註解: <task:annotation

Spring @Value 註解的使用方法

為了簡化讀取properties檔案中的配置值,spring支援@value註解的方式來獲取,這種方式大大簡化了專案配置,提高業務中的靈活性。 一、兩種使用方法 1、@Value(“#{configProperties[‘key’]}”) 2、@Value(“${key}”) 二、配置 2.1 @Valu