Spring boot 配置的注解之@Component ,@Configuration与@Bean,以及它们和传统xml的注入bean的区别

Spring boot 配置的注解之@Component ,@Configuration与@Bean,以及它们和传统xml的注入bean的区别

目录

1.传统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剩下文中。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值