@SpringBootApplication的使⽤
之前⽤户使⽤的是3个注解注解他们的main类。分别是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于这些注解⼀般都是⼀起使⽤,spring boot提供了⼀个统⼀的注解@SpringBootApplication。
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
@SpringBootApplication
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
分开解释@Configuration,@EnableAutoConfiguration,@ComponentScan。
1、@Configuration:提到@Configuration就要提到他的搭档@Bean。使⽤这两个注解就可以创建⼀个简单的spring配置类,可以⽤来替代相应的xml配置⽂件。
springboot实现aop<beans>
<bean id = "car" class="st.Car">
<property name="wheel" ref = "wheel"></property>
</bean>
<bean id = "wheel" class="st.Wheel"></bean>
</beans>
相当于:
@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。
2、@EnableAutoConfiguration:能够⾃动配置spring的上下⽂,试图猜测和配置你想要的bean类,通常会⾃动根据你的类路径和你的bean 定义⾃动配置。
3、@ComponentScan:会⾃动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的⼦注解
@Service,@Repository,@Controller。
如果有帮到您可以⽀持⼀下作者嘛

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。