springboot介绍及使⽤详解
Spring Boot是由Pivotal团队提供的全新框架,其设计⽬的是⽤来简化新Spring应⽤的初始搭建以及开发过程。该框架使⽤了特定的⽅式来进⾏配置,从⽽使开发⼈员不再需要定义样板化的配置。通过这种⽅式,Boot致⼒于在蓬勃发展的快速应⽤开发领域(rapid application development)成为领导者。
Spring boot的特点
1. 创建独⽴的Spring应⽤程序
2. 嵌⼊的Tomcat,⽆需部署WAR⽂件
3. 简化Maven配置
4. ⾃动配置Spring
5. 提供⽣产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码⽣成和对XML没有要求配置
Spring boot的优点
spring boot 可以⽀持你快速的开发出 restful 风格的微服务架构
⾃动化确实⽅便,做微服务再合适不过了,单⼀jar包部署和管理都⾮常⽅便。只要系统架构设计合理,⼤型项⽬也能⽤,加上nginx负载均衡,轻松实现横向扩展
spring boot 要解决的问题, 精简配置是⼀⽅⾯, 另外⼀⽅⾯是如何⽅便的让spring⽣态圈和其他⼯具链整合(⽐如redis, email, elasticsearch)
Spring boot的使⽤
1、maven配置⽂件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springboot.sample</groupId>
<artifactId>spring-boot-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-sample</name>springboot框架的作用
<description>Spring Boot Sample Web Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Spring Boot JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application类
@SpringBootApplication
@ServletComponentScan
public class SpringBootApplication extends SpringBootServletInitializer {
private static final Logger logger = Logger(SpringBootSampleApplication.class);
public static void main(String[] args) {
SpringApplication.run(SpringBootSampleApplication.class, args);
}
}
3、配置类
@Repository
@Component
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter implements EnvironmentAware{
private static final Logger logger = Logger(MyWebAppConfigurer.class);
private RelaxedPropertyResolver propertyResolver;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个组成⼀个链
// addPathPatterns ⽤于添加拦截规则
// excludePathPatterns ⽤户排除拦截
registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/**");
registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");
super.addInterceptors(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/res/**").addResourceLocations("classpath:/res/");
// 可以直接使⽤addResourceLocations 指定磁盘绝对路径,同样可以配置多个位置,注意路径写法需要加上file: registry.addResourceHandler("/imgs/**").addResourceLocations("file:D:/imgs/");
super.addResourceHandlers(registry);
}
}
4、添加filter
@WebFilter(filterName="myFilter",urlPatterns="/*")
public class MyFilter implements Filter {
@Override
public void destroy() {
System.out.println("过滤器销毁");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("执⾏过滤操作");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("过滤器初始化");
}
}
5、controller
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/info")
public Map<String, String> getInfo(@RequestParam String name) {
Map<String, String> map = new HashMap<>();
return map;
}
}
6、service
@Service
public class HelloWorldService {
public String getHelloMessage() {
return "Hello world";
}
}
7、全局异常处理
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseBody //在返回⾃定义相应类的情况下必须有,这是@ControllerAdvice注解的规定
public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) {
return resp;
}
}
说明:
@ControllerAdvice是controller的⼀个辅助类,最常⽤的就是作为全局异常处理的切⾯类
@ControllerAdvice可以指定扫描范围
@ControllerAdvice约定了⼏种可⾏的返回值,如果是直接返回model类的话,需要使⽤@ResponseBody进⾏json转换
1、返回String,表⽰跳到某个view
2、返回modelAndView
3、返回model + @ResponseBody
8、添加被其他配置使⽤的bean
@Bean
public PageHelper pageHelper(DataSource dataSource) {
PageHelper pageHelper = new PageHelper();
return pageHelper;
}
9、添加数据加载类
@Component
public class MyStartupRun implements CommandLineRunner { @Override
public void args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执⾏");
}
}
该组件在项⽬启动时会⽴即执⾏
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论