目录
Ioc控制反转
-
依赖注入
概念:
依赖:Bean对象的创建依赖于容器
注入:通过配置文件利用容器来注入属性
注入方式
1.set注入
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .
<bean id="student" class="com.somls.pojo.Student"> <property name="name" value="小明"/> 常量注入 </bean>
2.bean注入
<bean id="addr" class="com.kuang.pojo.Address"> <property name="address" value="重庆"/> </bean> <bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="小明"/> <property name="address" ref="addr"/> </bean>
3.数组注入
<bean id="addr" class="com.kuang.pojo.Address"> <property name="address" value="重庆"/> </bean> <bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="小明"/> <property name="address" ref="addr"/> </bean>
4.List注入
<property name="hobbys"> <list> <value>听歌</value> <value>看电影</value> <value>爬山</value> </list> </property>
5.Map注入
<property name="card"> <map> <entry key="中国邮政" value="456456456465456"/> <entry key="建设" value="1456682255511"/> </map> </property>
6.Set注入
<property name="games"> <set> <value>LOL</value> <value>BOB</value> <value>COC</value> </set> </property>
作用域:
类别 说明 singleton 单例模式 prototype 多例 request 每一次请求都会生产一个新Bean session 每一个 session 使用同一个 -
自动装配:没有注解好用,是其原型
autowire属性可选类型 : byName / byType
有了注解后不推荐此方法
byName 和 byType是通过 id 、class 类型来区分的 Bean
-
注解开发
@autowire :byType 形式注入,要求 bean 唯一
@qualifire :通过 Bean 的 id 属性,配合 autowire 一起注入
@resource :简化版,合并了 autowire 和 qualifire
-
静态/动态代理模式
Aop面向切面编程
暂时还没怎么用上,日后再补充(2022/4/24)
参考文章: