SpringBoot 核心

Spring Boot 的核心注解

Spring Boot的项目一般都会有注解*Application标注的入口类,入口类中会有一个main 方法,main方法是一个标准的Java应用程序的入口方法,可以直接启动。

@SpringBootApplication注解是Spring Boot的核心注解,用此注解标注的入口类是应用的 启动类,通常会在启动类的main方法中通过SpringApplication.run(App.class,args)来启动 Spring Boot应用项目。

@SpringBootApplication其实是一个组合注解,源代码如下。

 * Copyright 2012-2016 the original author or authors.

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.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AliasFor;

/**
 * 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
 * @author Stephane Nicoll
 * @since 1.2.0
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class))
public @interface SpringBootApplication {

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

	/**
	 * Exclude specific auto-configuration class names such that they will never be
	 * applied.
	 * @return the class names to exclude
	 * @since 1.3.0
	 */
	String[] excludeName() default {};

	/**
	 * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
	 * for a type-safe alternative to String-based package names.
	 * @return base packages to scan
	 * @since 1.3.0
	 */
	@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
	String[] scanBasePackages() default {};

	/**
	 * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
	 * scan for annotated components. The package of each class specified will be scanned.
	 * <p>
	 * Consider creating a special no-op marker class or interface in each package that
	 * serves no purpose other than being referenced by this attribute.
	 * @return base packages to scan
	 * @since 1.3.0
	 */
	@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
	Class<?>[] scanBasePackageClasses() default {};

}

@SpringBootApplication注解主要组合了以下注解。

(1)@SpringBootConfiguration这是Spring Boot项目的配置注解,也是一个组合注解, 源代码如下。

 * Copyright 2012-2016 the original author or authors.

package org.springframework.boot;

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

import org.springframework.context.annotation.Configuration;

/**
 * Indicates that a class provides Spring Boot application
 * {@link Configuration @Configuration}. Can be used as an alternative to the Spring's
 * standard {@code @Configuration} annotation so that configuration can be found
 * automatically (for example in tests).
 * <p>
 * Application should only ever include <em>one</em> {@code @SpringBootConfiguration} and
 * most idiomatic Spring Boot applications will inherit it from
 * {@code @SpringBootApplication}.
 *
 * @author Phillip Webb
 * @since 1.4.0
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

}

在Spring Boot项目中推荐使用@SpringBootConfiguration 来替代 @Configuration注解。

(2)@EnableAutoConfiguration 启动自动配置,该注解会让SpringBoot 根据当前项目依赖的jar包自动配置项目的相关配置项。‘

例如,当在Spring Boot项目的pom.xml文件中配置了如下spring-boot-starter-web依赖。

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

项目就会自动添加Tomcat和Spring MVC的依赖,同时Spring Boot会对Tomcat和Spring MVC进行配置项的自动配置。打开pom.xml,选择Dependency Hierarchy页面查看。
spring-boot-starter-web自动配置图
显示的是当前项目中Spring Boot的所有依赖,如果在项目中添加spring-boot-starter- data-jpa依赖,Spring Boot就会自动进行spring-boot-starter-data-jpa依赖的相关配置,可 以自行添加依赖,然后进行观察。

(3)@ComponentScan 扫描配置,Spring Boot 默认会扫描@SpringBootApplication 所在类的同级包以及它的子包。所以建议将@SpringBootApplication修饰的入口类放置在项目包下 (Group Id+Artifact Id),这样做的好处是,可以保证Spring Boot项目自动扫描到项目所有的包。


  • 邮箱:ithelei@sina.cn
  • GoodLuck
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值