Spring IOC 技术 学习 控制翻转 Invension of Control ioc是一种通过描述来生成或获取对象的技术,ioc容器是spring的核心 ,通过注解来生成对象,不建议使用xml
1. @configuration 表明为java配置文件,spring 会更加他来生成ioc容器去装配bean @bean 表明将对应装配到ioc容器中 @component 表明那个类被装配进spring ioc容器中 @componentscan 表明采用何种策略去扫描装配bean @value 指定具体值注入到bean中对应属性对应值 @autowired 根据属性的类型(By Type) 找到对应的bean 进行注入 @primary 修改优先权的注解 @qualifier 配置项value需要一个字符串,与@autowired结合,通过类型和名称找到bean @override 表明了被标注的方法重载了父类的方法,起到了断言的作用
@value 也可以读取application.properties 中的属性,如@Value (”${ database . driverNa皿e }”} @configurationproperties 按照对应兑取application.properties 中的属性 ,如@ConfigurationProperties (” database ” ) @propertysource 定义对应属性文件 如@PropertySource(value={” classpath : jclbc. properties ”} ,丰gnoreResourceNotFound=true) @conditional 条件控制是否进行bean的初始化,需要配合实现接口condition接口,如@ Conditional(DatabaseC 口nditiona工 . class) 需要实现接口conditon中matchs函数,matchss 会读取上下文环境,然后判断是否对应的数据库信息
@scope 对象作用域 (prototype // singleton // session // applicaton // request // configurebeanfactory // webapplicationcontent ) @profile 配置环境 开发环境dev,测试环境test,准生产环境,生产环境,在启动java是 添加选项 application-dev.properties
2. bean 的声明周期 bean定义,bean初始化,bean生存期,bean销毁
3. xml配置bean <bean id= " squirrel ” class = ” com . springboot.other.pojo . Squirrel " /> @ImportResource (value = {'’ cl.asspath: spring-other . xml"})