1.@SpringBootApplication包括:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponontScan
2.@SpringBootConfiguration
标注这个类是一个配置类;
它只是@Configuration注解的派生注解;
它与@Configuration注解的功能一致;
只不过@SpringBootConfiguration是springboot的注解,而@Configuration是spring的注解。
@Configuration 可以理解为一个Configuration就是对应的一个Spring的xml版的容器;(beans)
@Component
@Bean
是两种使用注解来定义bean的方式。
@Component(和@Service和@Repository)用于自动检测和使用类路径扫描自动配置bean。注释类和bean之间存在隐式的一对一映射(即每个类一个bean)。
@Bean用于显式声明单个bean,而不是让Spring像上面那样自动执行它。它将bean的声明与类定义分离,并允许您精确地创建和配置bean。
@Bean则常和@Configuration注解搭配使用:
@Configuration
public class WebSocketConfig {
@Bean
public Student student(){
return new Student();
}
}
都可以使用@Autowired或者@Resource注解注入;
@Bean注解告诉Spring这个方法将会返回一个对象,这个对象要注册为Spring应用上下文中的bean。通常方法体中包含了最终产生bean实例的逻辑。
两者的区别:
如果想将第三方的类变成组件,你又没有没有源代码,也就没办法使用@Component进行自动配置,这种时候使用@Bean就比较合适了。
另外@Bean注解的方法返回值是对象,可以在方法中为对象设置属性。
@SpringBootConfiguration 注解的作用
猜你喜欢
转载自blog.csdn.net/ScorpC/article/details/104752740
今日推荐
周排行