(转)Spring Boot(二) & lombok
(二期)5、springboot框架集成与lombok
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- mybatis plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/homework
username: root
password: admin
@ExceptionHandler(Exception.class)
public String exceptionHandler(Exception e) {
log.error("---------------->捕获到局部异常", e);
return "index";
}
步骤:
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
log.error("------------------>捕捉到全局异常", e);
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName("error");
return mav;
}
@ExceptionHandler(value = MyException.class)
@ResponseBody
public R jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
//TODO 错误日志处理
return R.fail(e.getMessage(), "some data");
}
}
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.0.36</version>
<scope>provided</scope>
</dependency>
@SpringBootApplication
public class SpringBootFrameApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootFrameApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意这里要指向原先用main方法执行的Application启动类
return builder.sources(SpringBootFrameApplication.class);
}
}
IDEA
eclipse
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
http://codepub.cn/2015/07/30/Lombok-development-guidelines/
public abstract class AbstractProcessor implements Processor{
#通过ProcessingEnvironment来获取编译阶段的一些环境信息
public synchronized void init(ProcessingEnvironment var1){}
#实现具体逻辑的地方,也就是对AST进行处理的地方
public abstract boolean process(Set<? extends TypeElement> var1, RoundEnvironment var2);
}
https://juejin.im/entry/5a390ba76fb9a0451e3fed7c
http://blog.didispace.com/java-lombok-how-to-use/