• 什么是SpringBoot


    随着动态语言的流行(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注解

  • 相关阅读:
    Python 面向对象(类与对象)
    修改Centos文 7件夹所在组及权限(不同用户共用一个文件夹)
    CGPoint,CGSize,CGRect
    关于投资币圈的思考和微信群问答干货整理
    数字货币合约交易基础知识
    比特币成长大事记
    健康医疗笔记(四)癌症知识
    ​阿德勒心理学《被讨厌的勇气》一切烦恼皆源于人际关系
    免费学术论文网站Sci-Hub
    数字货币交易所开发笔记3-撮合引擎开发
  • 原文地址:https://www.cnblogs.com/haizai/p/10864883.html
Copyright © 2020-2023  润新知