异常统一处理
在class注解上 @ControllerAdvice
在方法上注解上 @ExceptionHandler(value = Exception.class)
/*
* 返回json数据或者String数据:
* 那么需要在方法上加上注解:@ResponseBody
* 添加return即可。
*/
/*
* 返回视图:
* 定义一个ModelAndView即可,
* 然后return;
* 定义视图文件(比如:error.html,error.ftl,error.jsp);
*
*/
package com.osvue.win.app.spr;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class GlobalExceptionHoder {
@ResponseBody
@ExceptionHandler(value = Exception.class)
public String defaultErrorHandler(HttpServletRequest req, Exception e) {
System.out.println("global exception control ...");
e.printStackTrace();
return "error";
}
}
log4j
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private Logger logger = LoggerFactory.getLogger(this.getClass());
优雅注入
@EnableConfigurationProperties({CustomProperties.class})
@ConfigurationProperties(prefix = "custom.datasource.ds1")
public class CustomProperties {
private String url;
private String driverClassName;
private String username;
private String password;
}
// 在启动类中加入:
@EnableConfigurationProperties({BlogProperties.class})