Spring全局注解使用

注解:
@ControllerAdvice(看成spring mvc提供的一个特殊的拦截器)
Spring3.2开始提供的新注解,控制器增强(AOP),最主要的应用是做统一的异常处理

@ControllerAdvice是一个@Component,用于定义@ExceptionHandler(最主要用途),@InitBinder和@ModelAttribute方法,适用于所有使用@RequestMapping方法(拦截)。

引申:@interface 元注解
@Target(ElementType.TYPE) --该注解应用到什么地方
@Retention(RetentionPolicy.RUNTIME)–什么时候应用

@ExceptionHandler–为所有controller封装统一异常处理代码
@ModelAttribute–为所有controller设置全局变量
@InitBinder–用于为所有controller设置某个类型的数据转换器

工具的Controller

import com.zzzcontr.demo111.testDemo333;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class controllers {
    /**
     * 异常捕捉
     * @param ex 
     * @return
     */
    @ExceptionHandler
    public Map exceptionHandler(Exception ex ){
        Map map = new HashMap();
        map.put("code","101");
        map.put("Exception",ex.getMessage());
        System.out.println("异常为:"+ex.getMessage());
        return map;
    }

    /**
     * 时间格式转换
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder){
        System.out.println("进入InitBinder方法了");
        SimpleDateFormat data  = new SimpleDateFormat("yyyy-MM-dd");
        data.setLenient(true);
        binder.registerCustomEditor(Date.class,new CustomDateEditor(data,false));
    }
    /**
     * 全局增加变量
     */
    @ModelAttribute
    public void addModel(Model model){
        System.out.println("进入全局增加变量");
        model.addAttribute("allName","张三");
    }
}

测试类Controller

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.Date;


@RestController
public class controllerTest {
    /**
     * 自定义异常
     */
    @RequestMapping("/testzero")
    public String zeroExcpetion(){
        System.out.println("进入计算了");
        Integer i = 9/0;
        return i.toString();
    }
    /**
     * 时间格式
     * @param data
     * @return
     */
    @RequestMapping("testInitBinder")
    public Date dataAll(Date data){
        System.out.println("进入init时间类时间为:"+data);
        return data;
    }

    /**
     * 后去全局变1
     * @param modelMap
     * @return
     */
    @RequestMapping("/modelOUT")
    public Object testModel(ModelMap modelMap){
        Object allName = modelMap.get("allName");
        System.out.println("获取到的全局变量为:"+allName);
        return allName;
    }

    /**
     * 获取全局变量2
     * @param getvalue
     * @return
     */
    @RequestMapping("/modelOneTo")
    public Object testModel2(@ModelAttribute("allName") String getvalue){
        System.out.println(getvalue);
        return getvalue;
    }
}

版权声明:本文为qq_14902731原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。