当前位置: 首页 > news >正文

Spring注入和注解实现IOC

标题

  • 注入
    • 依赖注入的方式
      • 通过Set方法注入
      • 通过构造方法注入
      • 自动注入
    • 依赖注入的数据类型
      • 注入Bean对象
      • 注入基本数据类型和字符串
      • 注入List
      • 注入Set
      • 注入Map
      • 注入Properties
  • 注解实现IOC
    • Component
    • @Repository、@Service、@Controller

注入

依赖注入的方式

在使用依赖注入时,如果注入的是 Bean 对象,那么要求注入的 Bean 对象与被注入的
Bean 对象都需要 Spring

通过Set方法注入

需要为注入的成员变量提供 Set 方法。

  1. 配置文件
<bean id="usersDao" class="com.bjsxt.dao.impl.UsersDaoImpl"/>
<bean id="usersDaoMybatis" class="com.bjsxt.dao.impl.UsersDaoImpl"/>
<bean id="usersService" name="name1,name2,name3" class="com.bjsxt.service.impl.UsersServiceImpl"><!--<name要和UsersServiceImpl中属性名相同--><property name="usersDao"><ref bean="usersDaoMybatis"/></property>
<!--<property name="usersDao" ref="usersDaoMybatis"/>-->
</bean>
  1. Bean对象
private UsersDao usersDao;
public UsersDao getUsersDao() {return usersDao;
}
public void setUsersDao(UsersDao usersDao) {this.usersDao = usersDao;
}

通过构造方法注入

Bean 对象中需要提供有参的构造方法

  1. 配置文件
<bean id="usersDaoMybatis" class="com.bjsxt.dao.impl.UsersDaoMybatisImpl"/>
<bean id="usersService" name="name1, name2, name3" class="com.bjsxt.service.impl.UsersServiceImpl"><!--<property name="usersDao" ref="usersDaoMybatis"/>--><!--一个constructor-arg标签表示构造方法中的一个参数name:根据参数名称识别参数index:根据参数的位置识别参数type:根据参数类型识别参数--><constructor-arg type="com.bjsxt.dao.UsersDao"><ref bean="usersDaoMybatis"/></constructor-arg>
</bean>

2.Bean对象

private UsersDao usersDao;
public UsersServiceImpl(UsersDao usersDao){this.usersDao = usersDao;
}

自动注入

自动注入的方式有两种,一种是全局配置自动注入,另一种是局部配置自动注入。
无论全局配置或局部单独配置,都有 5 个值可以选择:

  • no:当 autowire 设置为 no 的时候,Spring 就不会进行自动注入。
  • byName:在 Spring 容器中查找 id 与属性名相同的 bean,并进行注入。需要提供 set 方
    法。
  • byType:在 Spring 容器中查找类型与属性名的类型相同的 bean,并进行注入。需要提
    供 set 方法。
  • constructor:仍旧是使用 byName 方式,只不过注入的时候,使用构造方式进行注入。
  • default:全局配置的 default 相当于 no,局部的 default 表示使用全局配置设置

局部自动注入:通过 bean 标签中的 autowier 属性配置自动注入,有效范围:仅针对当前 bean 标签生效
全局自动注入:通过 beans 标签中的 default-autowire , 有效范围:配置文件中的所有 bean

依赖注入的数据类型

注入Bean对象

方式一:

<property name="FieldName"><ref bean="BeanID"/>
</property>

方式二:

<property name="FieldName" ref="BeanID"/>

注入基本数据类型和字符串

方式一:

<property name="FieldName"><value>content</value>
</property>
<!--content是要传入内容的值-->

方式二

<property name="FieldName" value="Content"/>

注入List

<property name="users"><list><bean class="com.bjsxt.pojo.Users"><property name="username" value="Oldlu"/><property name="userage" value="30"/></bean><bean class="com.bjsxt.pojo.Users"><property name="username" value="admin"/><property name="userage" value="20"/></bean></list></property>

注入Set

<property name="usersSet"><set><bean class="com.bjsxt.pojo.Users"><property name="username" value="Oldlu-set"/><property name="userage" value="30"/></bean><bean class="com.bjsxt.pojo.Users"><property name="username" value="admin-set"/><property name="userage" value="20"/></bean></set></property>

注入Map

方式一:

 <property name="map"><map><entry key="key1" value="value1"/><entry key="key2" value="value2"/></map></property>

方式二:

<bean id="users1" class="......">
<bean id="users1" class="......">
<property name="FieldName"><map><entry key="key1" value-ref="users1"/><entry key="key2" value-ref="users2"/></map>
</property>

注入Properties

<property name="FieldName" ><props><prop key="KeyName">Content</prop></props>
</property>

注解实现IOC

E:\java\Spring\spring_ioc2

Component

作用:用于创建对象,放入Spring容器,相当于 <bean id="" class="">
注意:

  1. 要在配置文件中配置扫描的包,扫描到该注解才能生效。
    context:component-scan base-package="com.itbaizhan"> </context:component-scan>
  2. @Component 注解配置bean的默认id是首字母小写的类名。也
    可以手动设置bean的id值。
/ 此时bean的id为studentDaoImpl
@Component
public class StudentDaoImpl implements
StudentDao{public Student findById(int id) {// 模拟根据id查询学生return new Student(1,"百战程序
员","北京");}}// 此时bean的id为studentDao
@Component("studentDao")
public class StudentDaoImpl implements
StudentDao{public Student findById(int id) {// 模拟根据id查询学生return new Student(1,"百战程序
员","北京");}
}

@Repository、@Service、@Controller

作用:这三个注解和@Component的作用一样,使用它们是为了区分该类属于什么层。

  • @Repository用于Dao层
  • @Service用于Service层
  • @Controller用于Controller层

@Scope指定bean的创建策略:singleton prototype request session globalsession

http://www.lryc.cn/news/25780.html

相关文章:

  • Python统计Labelme标注文件信息并绘制散点图
  • 远程接入方案 OpenText Exceed TurboX(ETX) 客户案例——ET Innovations
  • Django4.1.7通过djongo1.3.6链接mongoDB6.0.4
  • 如何使用FindFunc在IDA Pro中寻找包含指定代码模式的函数代码
  • 【C++】讲的最通透最易懂的关于结构体内存对齐的问题
  • Stochastic Approximation 随机近似方法的详解之(一)
  • 软件自动化测试工程师面试题集锦
  • 智合同丨教你做一个懂AI的法律人
  • 如何判断自己使用的IP是独享还是共享?
  • 跳石头
  • 上传gitee教程,Gitee怎么上传代码到仓库
  • netstat命令详解
  • 数据库三范式
  • K8S 1.20 弃用 Docker 评估之 Docker 和 OCI 镜像格式的差别
  • Vue2和Vue3响应式的区别
  • 模型实战(6)之Alex实现图像分类:模型原理+训练+预测(详细教程!)
  • 【大数据】最全的大数据Hadoop|Yarn|Spark|Flink|Hive技术书籍分享/下载链接,持续更新中...
  • RIG Exploit Kit 仍然通过 IE 感染企业用户
  • GIS在地质灾害危险性评估与灾后重建中的实践技术应用及python机器学习灾害易发性评价模型建立与优化进阶
  • SQL SERVER中SCHEMA的詳解
  • 【LeetCode】剑指 Offer(13)
  • 帮助小型企业实现业务增长的7种数字营销策略
  • 互联网行业的高级产品经理和普通产品经理有哪些区别?
  • aardio - 【库】简单信息框
  • 程序员必备!最值得收藏的宝藏网站大盘点
  • Android 10.0 Settings 关掉开发者模式
  • 软件测试面试必杀篇:【2023软件测试面试八股文宝典】
  • 原子级操作快速自制modbus协议
  • 大数据之Apache Doris_亚秒级响应_大数据处理分析_介绍_概述---大数据之Apache Doris工作笔记0001
  • SpringCloud学习笔记 - 分布式系统全局事务管理 - Seata1.5.2+Nacos+OpenFeign