Spring boot 配置的注解之@Component ,@Configuration与@Bean,以及它们和传统xml的注入bean的区别
目录
2.Spring boot 配置的注解之@Component
3.Spring boot 配置的注解之@Configuration与@Bean
4.Spring boot 配置的注解与xml的注入bean的区别
5.Spring boot 配置的注解之@Component和@Configuration,@Bean的区别
1.传统xml文件的注入bean
在xml文件中,创建一个bean组件,配置和bean组件各个属性,这样也就就创建好了一个类实体对象;
<!-- 多文上传,限制1G文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1073741824" />
</bean>
2.Spring boot 配置的注解之@Component
@Component 注解一个类会作为组件类,并告知Spring要为这个类创建bean,也就是自动装配到Spring容器中。
@Component
@ConfigurationProperties(prefix = "otwx")
public class OtWXConfig {
3.Spring boot 配置的注解之@Configuration与@Bean
@Configuration,相当于xml文件中beans组件,
@Bean要与@Configuration配合使用,@Bean告诉Spring这个方法会返回一个对象实例,这个对象实例也会注册Spring应用上下文中bean。并且实例名称默认是方法名,或者可以自己配置一个名称。自定义性比较好
@Configuration
public class WxMpConfiguration {
@Bean
public ExtWxGzhhService extWxGzhhService(){
4.Spring boot 配置的注解与xml的注入bean的区别
Spring boot 配置的注解省去编写xml文件步骤,也相当省去学习xml语法的功夫
xml文件的注入bean的的方式,更加直观查看有哪些配置
5.Spring boot 配置的注解之@Component和@Configuration,@Bean的区别
@Component自定义比较差,注入的是Spring容器中,一般自定义对象类中。
@Configuration,@Bean自定义比较好,注入的是Spirng剩下文中。