Springboot之@SpringBootApplication

@SpringBootApplication

public static void main(String[] args) {
        SpringApplication.run(FreedoWebApplication.class, args);
    }
import java.lang.annotation.Annotation;
import org.springframework.context.annotation.ComponentScan;

public interface SpringBootApplication
    extends Annotation
{

    public abstract Class[] exclude();

    public abstract String[] excludeName();

    public abstract String[] scanBasePackages();

    public abstract Class[] scanBasePackageClasses();
}

之前用户使用的是3个注解注解他们的main类。分别是@Configuration,@EnableAutoConfiguration,@ComponentScan。

由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@SpringBootApplication。

@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。

@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。

@ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller。

注意:一般情况下@SpringBootApplication的类说明springboot扫描其类所在包以及其子包下的所有包;

springboot使用maven项目管理有时候会找不到启动类的错误:1、SpringBootApplication扫描所在类的包下必须将所有的contrller或者其他类放在其包或者其子包下;

                            2、清理项目clean重新编译。

关注公众号,我们就从陌生变成了相识,从此我们就成为了朋友,共同学习共同进步!

来都来了,抬起贵手点个赞再走呗!

猜你喜欢

转载自www.cnblogs.com/wwwcf1982603555/p/10102423.html