当前位置: 移动技术网 > IT编程>开发语言>Java > 什么是SpringBoot

什么是SpringBoot

2019年05月15日  | 移动技术网IT编程  | 我要评论

随着动态语言的流行(ruby,groovy,scala,node.js),java的开发显得格外的笨重;繁多的配置,低下的开发效率,复杂的部署流程以及第三方技术集成难度大.

在上述环境 下,spring boot应运而生.它使用"习惯优于配置"(项目中存在大量的配置,此外还内置一个习惯性的配置,让你无须手段进行配置)的理念让你的项目快速运行起来.

使用spring boot很容易创建一个独立运行(运行jar,内嵌servlet容器),准生产级别的基于spring框架的项目,使用spring boot你可以不用或者只需要很少的spring配置.

spring boot的优缺点

优点 :

(1) 快速构建项目;

(2) 对主流开发框架的无配置集成;

(3) 项目可独立运行,无须外部依赖 servlet容器;

(4) 提供运行时的应用监控;

(5) 极大地提高了开发,部署效率;

(6) 与云计算的天然集成.

缺点 :

(1) 书籍文档少且不够深入;

(2) 如果你不认同spring框架,这也许是它的缺点,但建议你一定要使用spring框架.

快速入门

设置spring boot的parent

<parent>

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-starter-parent</artifactid>

<version>1.5.2.release</version>

</parent>

说明 : spring boot的项目必须要将parent设置为spring boot的parent, 该parent包含了大量的默认的配置,大大简化了我们的开发.

导入spring boot的web支持

<dependency>

<groupid>org.springframework.book</groupid>

<artifactid>spring-boot-starter-web</artifactid>

</dependency>

添加spring boot的插件

<plugin>

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-maven-plugin</artifactid>

</plugin>

编写第一个spring boot的应用

@controller

@springbootapplication

@configuration

public class helloapplication {

@requestmapping("hello")

@responsebody

public string hello() {

return "hello world!";

}

public static void main(string[] args) {

springapplication.run(helloapplication.class, args);

}

}

代码说明 :

1 : @springbootapplication : spring boot项目的核心注解, 主要目的是开启自动配置;

2 : @configuration : 这是一个配置spring的配置类;

3 : @controller : 标明这是一个springmvc的controller控制器;

4 : main方法 : 在main方法中启动一个应用,既 : 这个应用的入口;

启动应用

在spring boot项目中,启动的方式有两种,一种是直接run java application另外一种是通过spring boot的maven插件运行.

spring boot的核心

入口类和@springbootapplication

spring boot的项目一般都会有*application的入口类,入口类中会有main方法,这是一个标准的java应用程序的入口方法.

@springbootapplication注解是spring boot的核心注解,它其实是一个组合注解;

@target(elementtype.type)

@retention(retentionpolicy.runtime)

@documented

@inherited

@springbootconfiguration

@enableautoconfiguration

@conmpnentscan(excludefilters = {

@filter(type = filtertype.custom, classes = typeexcludefilter.class),

@filter(type = filtertype.custom, classes = autoconfigurationexcludefilter.class)

})

public @interface springbootapplication {}

该注解主要组合了以下注解 :

1 : @springbootconfiguration : 这是spring boot项目的配置注解,这也是一个组合注解 :

@target(elementtype.type)

@retention(retentionpolicy.runtime)

@documented

@configuration

public @interface springbootconfiguration{}

在spring boot项目中推荐使用@springbootconfiguration替代@configuration

2 : @enableautoconfiguration : 启用自动配置,该注解会使spring boot根据项目中依赖的jar包自动配置项目的配置项 :

a) 如 : 我们添加了spring-boot-starter-web的依赖,项目中也就会引入springmvc的依赖,spring boot就会自动配置tomcat和springmvc

spring-boot-starter-web : 1.5.2.release[compile]

spring-boot-starter : 1.5.2.release[compile]

spring-boot-starter-tomcat : 1.5.2.release[compile]

tomcat-embed-core : 8.5.11[compile]

tomcat-embed-el : 8.5.11[compile]

tomcat-embed-websocked : 8.5.11[compile]

tomcat-embed-core : 8.5.11(omitted for conflict with 8.5.11)[compile]

hibernate-vaildator : 5.3.4.final[compile]

jackson-databind : 2.8.7[compile]

spring-web : 4.3.7.release(omitted for confilct with 4.3.7.release)[compile]

spring-webmvc : 4.3.7.release(omitted for confilct with 4.3.7.release)[compile]

3 : @componentscan : 默认扫描@springbootapplication所在类的同级目录以及它的子目录.

关闭自动配置

spring boot会根据项目中的jar包依赖,自动做出配置,spring boot支持的自动配置(非常多);

比如 : 我们不想自动配置redis,想手动配置.

@springbootapplication(exclude = {redisautoconfiguration.class})

public class helloapplication {

}

如果不想看到banner,可以将其关闭 :

public static void main(stirng[] args) {

springapplication app = new springapplication(helloapplication.class, args);

app.setbannermode(banner.mode.off); //关闭banner

app.run-正在西部数码(www.west.cn)进行交易(args);

}

spring boot项目使用一个全局的配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到resources下。

1、 修改tomcat的端口为8088(server.port=8088)

2、 修改进入dispatcherservlet的规则为:*.html(server.servlet.path=*.html)

xml配置文件

spring boot提倡零配置,既无xml配置,但是在实际项目中,可能有一些特殊要求你 必须使用 xml配置,这时我们可以通过spring提供的@importresource来加载xml配置,

例如 :

@importresource({"classpath:some-context.xml","classpath:another-context.xml"})

日志

spring boot对各种日志框架都支持,通过配置修改默认的日志的配置 :

#设置日志级别

logging.level.org.springframework=debug

格式 :

logging.level.*= # log levels serverity. for instance 'logging.level.org.springframework=debug'

spring boot的自动配置的原理

spring boot在进行springapplication对象实例化时会加载meta-inf/spring.factories文件,将该配置文件中的配置载入到spring容器.

进入规则为 /

如果进入springmvc的规则为/时,spring boot的默认静态资源的路径为:

spring.resources.static-locations=classpath:/meta-inf/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

进入规则为*.xxx 或者 不指定静态文件路径时

将静态资源放置到webapp下的static目录中即可通过地址访问:

自定义springmvc的配置

有些时候我们需要自已配置springmvc而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承webmvcconfigureradapter然后重写父类中的方法进行扩展。

在spring boot中推荐使用@transaction注解来申明事务.

首先需要导入依赖 :

<dependency>

<groupid>org.springframework.boot</groupid>

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

</dependency>

当引入jdbc依赖之后,spring boot会自动默认分别注入datasourcetransactionmanager或jpatransactionmanager,所以我们不需要任何额外配置就可以用@transactional

注解进行事务使用.

例如 :

在service中添加@transactional注解 :

发布到独立的tomcat中运行

在开发阶段我们推荐使用内嵌的tomcat进行开发,因为这样会方便很多,但是到生成环境,我们在独立的tomcat容器中运行,因为我们需要对tomcat做额外的优化,这时我们

需要将工程打成war包进行发布到外部的tomcat里面.

工程的打包方式为war

<parent>

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-starter-parent</artifactid>

<version>1.5.2.release</version>

<parent>

<groupid>com.taotao.cart</groupid>

<artifactid>taotao-cart-springboot</artifactid>

<version>1.0.0-snapshot</versio>

<packageing>war</packageing>

将spring-boot-starter-tomcat的范围设置为provided

设置为provided是打包时会将该包排除,因为要放到独立的tomcat中运行,是不需要的.

<dependency>

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-starter-tomcat</artifactid>

<scope>provided</scope>

</dependency>

修改代码,设置启动配置

需要集成springbootservletinitializer,然后重写configure,将spring boot的入口类设置进去.

需要启动类继承需要集成springbootservletinitializer

@configuration

@propertysource(value = {"classpath:jdbc.properties", "classpath:env.properties", "classpath:httpclient.properties"})

@componentscan(basepackages = "con.taotao")

@importresource(value = "classpath:dubbo/dubbo-consumer.xml") // dubbo的配置文件,将dubbo整合到spring容器中

@springbootapplication

public class taobaoapplication extends springbootservletinitializer {

@override

protected springapplicationbuilder configure(springapplicationbuilder builder) {

// 设置启动类,用于独立tomcat运行的入口

return builder.sources(taotaoapplication.class);

}

}

maven命令 : clean package // 把项目打包成war包.

把打包成war的项目放到tomcat的webapps的root里面,并解压.

 

在配置mybatis时使用java配置

@configuration

@autoconfiureafter(mybatisconfig.class) // 保证在mybatisconfig实例化之后再实例化该类

public class mapperscannerconfig {

// mapper接口的扫描器

@bean

public mapperscannerconfigurer mapperscannerconfigurer() {

mapperscannerconfigurer mapperscannerconfigurer = new mapperscannerconfigurer();

mapperscannerconfigurer.setbasepackage("com.taotao.cart.mapper");

return mapperscannerconfigurer;

}

}

全局捕获异常

1. 新建一个class,这里取名为globaldefaultexceptionhandler;

2. 在class上添加注解,@controlleradvice;

3. 在class中添加一个方法;

4. 在方法上添加@exceptionhandler拦截相应的异常信息;

5. 如果返回的是view -- 方法的返回值是modelandview;

6. 如果返回的是string或者是json数据,那么需要在方法上添加@responsebody注解

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网