以前用户使用的是3个注解注解他们的main类。分别是@Configuration,@EnableAutoConfiguration,@ComponentScan。因为这些注解通常都是一块儿使用,spring boot提供了一个统一的注解@SpringBootApplication。java
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。spring
@SpringBootApplication public class ApplicationMain { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
分开解释@Configuration,@EnableAutoConfiguration,@ComponentScan。xml
一、@Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就能够建立一个简单的spring配置类,能够用来替代相应的xml配置文件。对象
<beans> <bean id = "car" class="com.test.Car"> <property name="wheel" ref = "wheel"></property> </bean> <bean id = "wheel" class="com.test.Wheel"></bean> </beans>
至关于:blog
@Configuration public class Conf { @Bean public Car car() { Car car = new Car(); car.setWheel(wheel()); return car; } @Bean public Wheel wheel() { return new Wheel(); } }
@Configuration的注解类标识这个类能够使用Spring IoC容器做为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。it
二、@EnableAutoConfiguration:可以自动配置spring的上下文,试图猜想和配置你想要的bean类,一般会自动根据你的类路径和你的bean定义自动配置。io
三、@ComponentScan:会自动扫描指定包下的所有标有@Component的类,并注册成bean,固然包括@Component下的子注解@Service,@Repository,@Controller。class