专栏首页一个会写诗的程序员的博客《Springboot极简教程》使用@SpringBootApplication annotation

《Springboot极简教程》使用@SpringBootApplication annotation

许多Spring Boot开发人员总是使用@Configuration,@EnableAutoConfiguration和@ComponentScan来标注它们的主类。 由于这些注释经常一起使用(特别是如果您遵循上述最佳实践),Spring Boot提供了一个方便的@SpringBootApplication替代方法。

@SpringBootApplication注释相当于同时使用@Configuration,@EnableAutoConfiguration和@ComponentScan及其默认属性(重复注解,SpringBoot是怎么处理的?):

package com.example.myproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication 
/* same as */ 
//@Configuration 
//@EnableAutoConfiguration 
//@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

NOTE

@SpringBootApplication alias 关联了注解: @EnableAutoConfiguration和@ComponentScan的属性。

源码:

/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Indicates a {@link Configuration configuration} class that declares one or more
 * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
 * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
 * annotation that is equivalent to declaring {@code @Configuration},
 * {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
 *
 * @author Phillip Webb
 * @since 1.2.0
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {

    /**
     * Exclude specific auto-configuration classes such that they will never be applied.
     * @return the classes to exclude
     */
    Class<?>[] exclude() default {};

}

所以说,你也可以直接自己定于注解,来封装一些常用的注解。

package com.restfiddle;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by jack on 2017/3/23.
 *
 * @author jack
 * @date 2017/03/23
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface RestBoot {
    Class<?>[] exclude() default {};
}

代码中可以这样用:

/*
 * Copyright 2014 Ranjan Kumar
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.restfiddle;

import org.springframework.boot.SpringApplication;

/**
 * 应用入口类 main函数
 */

//@Configuration
//@EnableAutoConfiguration
//@ComponentScan({"com.restfiddle"})
//@SpringBootApplication
@RestBoot
public class RestFiddleApplication {

    public RestFiddleApplication() {
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(RestFiddleApplication.class, args);
    }

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Spring Boot 自定義 HttpMessageConverter 解決 String 類型返回JSON對象問題

    HttpMessageConverter 接口介绍 org.springframework.http.converter.HttpMessageConvert...

    一个会写诗的程序员
  • 《Spring Boot极简教程》第7章 Spring Boot集成模板引擎

    其实,没有任何一个模板引擎(jsp,velocity,thymeleaf,freemarker,etc)可以完全实现MVC绝对的分层,只有“自由度”上的界定罢了...

    一个会写诗的程序员
  • JPA 执行update/delete query 需要加上事务

    Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Executing...

    一个会写诗的程序员
  • Android注解学习(2)

        最近考试周没什么时间写,回归正题。前面的一次简单的讲了关于注解的的基础部分,这一次分析xutils注解封装的源码(奉上github源码)。     补充...

    用户1148881
  • Android注解学习(2)

        最近考试周没什么时间写,回归正题。前面的一次简单的讲了关于注解的的基础部分,这一次分析xutils注解封装的源码(奉上github源码)。     补充...

    用户1148881
  • SrpingCloud ---github上面如何使用webHook实现配置文件修改 ,客户端自动刷新

    增加了spring-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,可以监控程序在运行时...

    爱明依
  • java之异步线程Executor

    说起异步,下意识就想到同步,这两个概念每个人都有着不同的理解,在这我们只是提下这关键词,具体的理解,可自行斟酌。

    码农王同学
  • 一步步完成Maven+SpringMVC+SpringFox+Swagger整合示例

    本文给出一个整合Maven+SpringMVC+SpringFOX+Swagger的示例,并且一步步给出完成步骤。

    孟君
  • Spring Boot入门系列(九)如何实现异步执行任务,极简版!

    今天主要讲解Spring Boot中的另外一个任务:异步任务。所谓异步任务,其实就是异步执行程序,有些时候遇到一些耗时的的任务,如果一直卡等待,肯定会影响其他程...

    架构师精进
  • uNetXST:将多个车载摄像头转换为鸟瞰图语义分割图像

    项目地址:https://github.com/ika-rwth-aachen/Cam2BEV

    3D视觉工坊

扫码关注云+社区

领取腾讯云代金券