敏捷开发必备框架之SpringBoot

一、SpringBoot简介

SpringBoot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置,简单来说,springboo提供了一堆依赖打包,并且已经按照习惯解决了依赖问题

SpringBoot默认使用tomcat作为服务器,使用logback提供日志记录

SpringBoot提供了一系列的依赖包,所以需要构建工具的支持,maven或者gradle

二、springboot启动器

spring-boot-starter(这是springboot的可信启动器,包含了自动配置,日志和YAML)

一个简单的sprngboot示例:

1、pom.xml配置

springboot父工程依赖

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifcatId>

<version>1.3.1.RELEASE</version>

<relativePath/>

</parent>

2、springboot web启动器

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

二、SpringBoot的注解

@SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解

@ComponentScan:使得SpringBoot扫描到Configuration类并将其加入到程序上下文

@Configuration:等同于spring的xml配置文件,使得java代码可以检查类型安全

@EnableAutoConfiguration:自动配置

@ComponentScan:组件扫描,可以自动发现和装配一些Bean

@Component:可以配合CommandLineRunner使用,在程序启动后执行一些基础任务

@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。

@Autowired自动导入。

@PathVariable获取参数

@JsonBackReference解决嵌套外链问题。

@RepositoryRestResourcepublic配合spring-boot-starter-data-rest使用。

@EnableAutoConfiguration:Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。

@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。

这个注释告诉SpringBoot“猜”你将如何想配置Spring,基于你已经添加jar依赖项。如果spring-boot-starter-web已经添加Tomcat和Spring MVC,这个注释自动将假设您正在开发一个web应用程序并添加相应的spring设置。

自动配置被设计用来和“Starters”一起更好的工作,但这两个概念并不直接相关。您可以自由挑选starter依赖项以外的jar包,springboot仍将尽力自动配置您的应用程序。

@Bean:相当于XML中的bean,放在方法的上面,而不是类,意思是产生一个bean,并交给spring管理。

三、SpringBoot的启动

import    org.springframework.boot.SpringApplication;

import   org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootAppliaction

public class SpringBootSampleApplication{

public static void mian(String[] args){

SpringApplication.run(SpringBootSampleApplication.class,args);

}

}

四、springboot支持jsp

springboot默认支持freeMarker页面,jsp技术过于陈旧(效率低下),如果springboot输出jsp页面,则需要:

1、配置application.properties

#页面默认前缀目录(不可改动)

spring.mvc.view.prefix=/WEB-INF/jsp

#响应页面默认后缀

spring.mvc.view.suffix=.jsp

2、加入依赖

<dependency> 
<groupId>org.apache.tomcat.embed</groupId> 
<artifactId>tomcat-embed-jasper</artifactId> 
<scope>provided</scope> 

</dependency> 

<dependency>
 <groupId>javax.servlet</groupId> 
<artifactId>jstl</artifactId> 

</dependency>

五、springboot支持freemarker

1、配置application.properties

spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false

spring.freemarker.expose-spring-macro-helpers=false

2、引入依赖

<!-- 引入freeMarker的依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

六、Springboot/servlet注册

springboot有两种方式添加Servlet/Filter/Listener

1、代码注册通过ServletRegistrationBean、FilterRegistrationBean()和ServletListenerRegistrationBean获得

@Bean

public ServletRegistrationBean servletRegistrationBean(){

return new ServletRegistrationBeab(new MyServlet(),"/xs/*");

}

2、在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码

eg:

@SpringBootApplication

@ServletComponentScan

public class SpringBootApplication{

        public static void main(String[]c args){

        SpringBootApplication.run(SpringBootSampleApplication.calss,args);

        }

}

七、SpringBoot拦截器

1、创建我们自己拦截器类,并且实现HandlerInterceptor接口

2、创建一个Java类继承WebMvcConfigurerAdapter,并且重写addInterceptors方法

3、实例化我们自定义的拦截器,然后将对象手动添加到拦截器链中(在addInterceptors方法中添加)

八、SpringBoot静态资源处理

1、springboot的默认资源映射

其中默认配置的 /**映射到/static(或者/public 、/resource、/META-INF/resource)

其中默认配置的/webjars/** 映射到classpath:/META-INF/resources/webjars/

上面的static、public、resources等目录都在classpath:下面(如:“src/main/resources/static”)

2、自定义资源映射

继承WebMvcConfigurerAdapter并且重写方法addResourceHandlers

registry.addResourceHandler("/image/**").addResourceLocations("filter:H:/image")

registry.addResourceHandler("/image1/**").addResourceLocations("classpath:/img1")

3、通过配置文件映射

使用spring.mvc.static-path-pattern,如修改为/image/**、

使用spring.resources.static-location可以重新定义pattern所指向的路径,支持classpath和file

注意:spring.mvc.static-path-pattern只可以定义一个,目前不支持多个都好分割的方式


#默认值为  /**

spring.mvc.static-path-pattern=/image/**

#默认值为 classpath:/META-INF/resources/  , classpath:/resources/ , classpath:/static/ , classpath:/public/

spring.resources.stat-locations=classpath:/image/


springboot 启动加载数据,在项目启动的时候加载一些数据或者做一些事情的需求,springBoot提供的方法是通过实现接口CommandLineRunner来实现

九、SpringBoot   JDBC

1、属性配置文件(application.properties)

spring.datasource.url=mysql://localhost:3306/consult

spring.datasource.username=myConsult

spring.datasource.password=123456

spring.datasource.driver-class-name=org.git.m.m.mysql.Driver

2、pom.xml配置maven依赖

<!--MySql依赖-->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>


<!--springboot JDBC-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifcatId>spring-boot-starter-jdbc</artifcatId>

</dependency>

十、SpringBoot整合MyBatis

1、pom.xml配置maven依赖

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifcatId>mybatis-spring-boot-starter</artifcatId>

<version>1.0.0</vsersion>

</dependency>

2、一定要在启动的地方加上@MapperScan(“com.zhongmei.tang.dao”)

3、配置文件中加上配置

mybatis.typeAliasesPackage=com.zhongmei.tang.bean

mybatis.mapperLOcation-classpath:com/zhongmei/tang/xml/*Mapper

猜你喜欢

转载自blog.csdn.net/tangyin_007/article/details/79973700