EnableAutoConfiguration

Spring Boot 的核心注解SpringBootApplication引起的探究

星期二, 十二月 3rd, 2019 | JAVA-and-J2EE, spring-boot | 没有评论

Spring Boot 的核心注解SpringBootApplication引起的探究

1.Spring Boot 的核心注解是哪个?

先上一个工程的启动配置项看下:

项目比较老混杂了spring boot,dubbo,jersey注册等

	package com.pomelolee.cms;
 
	import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
	import com.pomelolee.boot.dubbo.annotation.EnableDubboProvider;
	import org.glassfish.jersey.server.ResourceConfig;
	import org.glassfish.jersey.servlet.ServletProperties;
	import org.springframework.beans.factory.config.BeanDefinition;
	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	import org.springframework.boot.builder.SpringApplicationBuilder;
	import org.springframework.boot.web.support.SpringBootServletInitializer;
	import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
	import org.springframework.cloud.netflix.feign.EnableFeignClients;
	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
	import org.springframework.context.annotation.ImportResource;
	import org.springframework.util.ClassUtils;
 
	import java.util.HashSet;
	import java.util.Set;
 
	@EnableFeignClients
	@SpringBootApplication
	@EnableApolloConfig
	@EnableDiscoveryClient
	@EnableDubboProvider
	@ImportResource("classpath*:/META-INF/spring/spring-job.xml")
	public class Application extends SpringBootServletInitializer {
	  public static void main(String[] args) {
	    SpringApplication.run(Application.class, args);
	  }
 
	  @Override
	  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
	    return application.sources(Application.class);
	  }
 
	  @Bean
	  public ResourceConfig resourceConfig() {
	    ResourceConfig resourceConfig = new ResourceConfig();
	    ClassPathScanningCandidateComponentProvider scanner =
	        new ClassPathScanningCandidateComponentProvider(true);
	    Set<beandefinition> beanDefinitionSet =
	        scanner.findCandidateComponents("com.pomelolee.cms.service");
	    beanDefinitionSet.addAll(scanner.findCandidateComponents("com.pomelolee.cms.facade"));
	    beanDefinitionSet.addAll(scanner.findCandidateComponents("com.pomelolee.cms.config"));
	    Set<Class<?>> clazzSet = new HashSet<Class<?>>();
	    for (BeanDefinition beanDefinition : beanDefinitionSet) {
	      clazzSet.add(
	          ClassUtils.resolveClassName(
	              beanDefinition.getBeanClassName(), resourceConfig.getClassLoader()));
	    }
	    resourceConfig.registerClasses(clazzSet);
	    resourceConfig.property(ServletProperties.FILTER_FORWARD_ON_404, true);
	    return resourceConfig;
	  }
	}
 </beandefinition>

@SpringBootApplication 注解,就是 Spring Boot 的核心注解。

› Continue reading

Tags: ,

Search

文章分类

Links

Meta