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

简介: 许多Spring Boot开发人员总是使用@Configuration,@EnableAutoConfiguration和@ComponentScan来标注它们的主类。

许多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);
    }

}
相关文章
|
前端开发 Java Linux
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
1851 0
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
|
Java 开发者 微服务
深入解析@SpringBootApplication注解:简化Spring Boot应用的配置
在现代的Java开发中,Spring Boot框架成为了构建微服务和快速开发应用的首选。Spring Boot的成功部分归功于其简化的配置和约定大于配置的理念。而`@SpringBootApplication`注解则是Spring Boot应用的入口,负责自动配置和启动Spring Boot应用。本文将深入探讨`@SpringBootApplication`注解的作用、用法,以及在Spring Boot应用中的应用场景。
1133 1
|
6月前
|
XML Java 数据格式
从零开始构建Spring Boot应用:逐步解析@SpringBootApplication
从零开始构建Spring Boot应用:逐步解析@SpringBootApplication
77 0
|
6月前
|
Java 容器 Spring
SpringBoot2 | @SpringBootApplication注解 自动化配置流程源码分析(三)
SpringBoot2 | @SpringBootApplication注解 自动化配置流程源码分析(三)
62 0
|
Java 开发工具 Spring
【Spring Boot 源码学习】@SpringBootApplication 注解
本篇介绍 @SpringBootApplication 注解的相关内容
157 1
【Spring Boot 源码学习】@SpringBootApplication 注解
|
Java API 容器
@SpringBootApplication注解SpringBoot深度理解(课时八)
@SpringBootApplication注解SpringBoot深度理解(课时八)
74 0
|
Java API Spring
SpringBoot中“@SpringBootApplication“自动配置原理《第七课》
SpringBoot中“@SpringBootApplication“自动配置原理《第七课》
146 0
|
存储 安全 Java
SpringBoot整合SpringSecurity完整教程
SpringBoot整合SpringSecurity完整教程
SpringBoot整合SpringSecurity完整教程
|
监控 网络协议 关系型数据库
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(二)
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(二)
303 0
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(二)
|
NoSQL Java 应用服务中间件
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(一)
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(一)
504 0
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(一)
下一篇
无影云桌面