在https://start.spring.io/上创建一个springboot工程
生成的代码中的启动方式咱们暂时定义为默认方式:
/**
* @auther: lawt
* @date: 2018/12/1 17
* @Description: 默认启动方式
*/
@SpringBootApplication
public class MicroServicesSpringBootApplication {
public static void main(String[] args) {
//端口默认为8080
SpringApplication.run(MicroServicesSpringBootApplication.class, args);
}
}
第二种方式:
/**
* @auther: lawt
* @date: 2018/12/1 17
* @Description: 第二种启动方式
*/
@SpringBootApplication
public class MicroServicesSpringBootApplication1 {
public static void main(String[] args) {
//server.port=0 随机产生一个接口-随机向操作系统要一个可用的端口,
new SpringApplicationBuilder(MicroServicesSpringBootApplication.class)
.properties("server.port=0")
.run(args);
}
}
第三种启动方式:
/**
* @auther: lawt
* @date: 2018/12/1 17
* @Description: 第三种启动方式
*/
@SpringBootApplication
public class MicroServicesSpringBootApplication2 {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(MicroServicesSpringBootApplication2.class);
Map<String, Object> properties = new HashMap<>();
properties.put("server.port", 0);//不写端口,默认那是8080
springApplication.setDefaultProperties(properties);
springApplication.run(args);
}
}
神奇的注解
@SpringBootApplication
咱们看看其源码:
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),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {};
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
}
先看他上面的一个注解:
@ComponentScan
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
......
}
用于扫描,3.1版本来的,
再看它上面的另外一个注解:
@EnableAutoConfiguration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
...
}
激活自动装配 @Enable -> @Enable 开头的有:
- @EnableWebMvc
- @EnableTransactionManagement
- @EnableAspectJAutoProxy
- @EnableAsync
再看他上的另外一个注解:
@SpringBootConfiguration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
等价于 @Configuration -> Configuration Class 注解
标签:Springboot,启动,springframework,annotation,public,三种,import,ComponentScan,class From: https://blog.51cto.com/u_11702014/6186270