spirng笔记01(组件类注解)

依赖注入

@Autowired、@Resource的区别

https://blog.csdn.net/java_lifeng/article/details/123339938

引申:Spring的循环依赖问题 SpringBoot2.6之后默认不支持循环依赖,需要手动加入配置开启,配置项为:
spring.main.allow-circular-references=true
但是最好不要有循环依赖

Bean定义

注解@Configration与@Bean以及@Component

@Configration声明当前类为配置类,其中内部组合了@Component注解,表示此类也是一个bean
@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法。
@Bean也用于创建Bean,区别于@Component,@Bean只能注解在方法上,但是其可以方便的实现Bean的定制化,而@Component只能依赖于类的无参构造来创建Bean

通过配置文件创建Bean与通过注解创建Bean时属性注入方式的不同(了解即可)
前者通过反射调用set方法注入,所以必须提供对应属性的set方法,后者使用@Autowird注解,通过反射获取属性并直接进行赋值,所以可以不用提供set方法。

属性获取及赋值

@Value注解的使用
https://juejin.cn/post/6998688624237625374
https://blog.csdn.net/mapeng765441650/article/details/105477160

使用此注解获取配置文件的属性时,首先需要加载配置文件,方式如下

<context:property-placeholder location="classpath:config/xxx.properties" system-properties-mode="OVERRIDE"></context:property-placeholder>

多个配置文件时。可以使用逗号分隔

<context:property-placeholder location="classpath:config/xxx.properties,classpath:config/yyy.properties" system-properties-mode="OVERRIDE"></context:property-placeholder>

或者

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="location">
        <value>dbconfig.properties</value>
        </property>
    </bean>

再或者

  <!-- 加载配置文件 -->
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="fileEncoding" value="UTF-8"/>
		<property name="locations">
			<list>
				<value>classpath:dbconfig.properties</value>
			</list>
		</property>
	</bean>

@PropertySource注解的使用

https://blog.csdn.net/qq_40837310/article/details/106587158

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值