news 2026/4/15 13:42:13

Spring的配置各种依赖注入

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring的配置各种依赖注入

Spring配置

别名

alias标签

<!--别名,如果添加了别名,我们也可以使用别名获取到这个对象--><aliasname="user"alias="balbala"/>

实例化容器的时候调用

Useruser=(User)context.getBean("balbala");

实际上取别名不如用bean,不推荐使用这种方式起别名

Bean的配置

  • id:bean的唯一标识符,也就是相当于我们new对象的一个变量名,它也可以说是Spring容器的的id
  • class: bean 对象所对应的全限定名:包名+类名(new的那个对象的类名)
  • name:也是别名,而且name更高级,可以同时取多个别名(空格隔开、逗号、分号)
<bean id="user2"class="com.cike3.pojo.User2"name="userT u2,u3;u4"><property name="name"value="cike_y"/></bean>

import

这个import,一般用于团队开发使用,它可以将多个配置文件,导入合并为一个

假设,现在项目中有多个人开发,这三个人负责不同的类开发,不同的类需要注册的bean中,我们可以利用import,将所有人的beans.xml合并为一个总的!

  • 张三
  • 李四
  • 王五
  • applicationContext.xml(正规的命名)
<importresource="beans.xml"/><importresource="beans2.xml"/>

使用的时候,直接使用总的配置

官方文档:

https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-constructor-injection

依赖注入

构造器注入

前面 "IoC创建对象的方式“

set方式注入【重点】

  • 依赖注入:本质是Set注入
    • 依赖:bean对象的对象依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入

主要有以下注入:

  • 普通值注入,value
<beanid="student"class="com.cike4.pojo.Student"><!--第一种,普通值注入,直接使用value--><propertyname="name"value="cike_y"/></bean>
  • Bean注入,ref
<beanid="address"class="com.cike4.pojo.Address"><propertyname="address"value="广东"/></bean><beanid="student"class="com.cike4.pojo.Student"><!--第二种,Bean注入,ref引用address容器id--><propertyname="address"ref="address"/></bean>
  • 数组注入
<beanid="student"class="com.cike4.pojo.Student"><!--数组注入--><propertyname="books"><array><value>十日终焉</value><value>凡人修仙传</value><value>夏日重现</value></array></property></bean>
  • List注入
<beanid="student"class="com.cike4.pojo.Student"><propertyname="hobbys"><list><value>打游戏</value><value>看电影</value><value>爱躺平</value></list></property></bean>
  • Map注入
<beanid="student"class="com.cike4.pojo.Student"><!--Map 注入--><propertyname="card"><map><entrykey="身份证"value="444444444444444444"/></map></property></bean>
  • Set
<beanid="student"class="com.cike4.pojo.Student"><!--Set--><propertyname="games"><set><value>和平精英</value><value>原神</value></set></property></bean>
  • null
<beanid="student"class="com.cike4.pojo.Student"><!-- null value默认不写为null <property name="wife" value=""/> --><propertyname="wife"><null/></property></bean>
  • Properties
<beanid="student"class="com.cike4.pojo.Student"><!--Properties--><propertyname="info"><props><propkey="学号">20230302222</prop><propkey="姓名">cike_y</prop><propkey="性别"></prop><propkey="username">root</prop><propkey="password">123456</prop></props></property></bean>
【环境搭建】
  1. 复杂类型
publicclassAddress{privateStringaddress;publicStringgetAddress(){returnaddress;}publicvoidsetAddress(Stringaddress){this.address=address;}}
  1. 真实测试对象
publicclassStudent{// 引用类型privateStringname;privateAddressaddress;privateString[]books;privateList<String>hobbys;privateMap<String,Object>card;privateSet<String>games;privateStringwife;privatePropertiesinfo;}
  1. 完整的beans.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"><beanid="address"class="com.cike4.pojo.Address"><propertyname="address"value="广东"/></bean><beanid="student"class="com.cike4.pojo.Student"><!--第一种,普通值注入,直接使用value--><propertyname="name"value="cike_y"/><!--第二种,Bean注入,ref--><propertyname="address"ref="address"/><!--数组注入--><propertyname="books"><array><value>十日终焉</value><value>凡人修仙传</value><value>夏日重现</value></array></property><!--List注入--><propertyname="hobbys"><list><value>打游戏</value><value>看电影</value><value>爱躺平</value></list></property><!--Map 注入--><propertyname="card"><map><entrykey="身份证"value="444444444444444444"/></map></property><!--Set--><propertyname="games"><set><value>和平精英</value><value>原神</value></set></property><!-- null value默认不写为null <property name="wife" value=""/> --><propertyname="wife"><null/></property><!--Properties--><propertyname="info"><props><propkey="学号">20230302222</prop><propkey="姓名">cike_y</prop><propkey="性别"></prop><propkey="username">root</prop><propkey="password">123456</prop></props></property></bean></beans>
  1. 测试类
publicclasssprint_04_Test{@Testpublicvoidtest(){ApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml");Studentstudent=(Student)context.getBean("student");System.out.println(student.toString());/* * Student{name='cike_y', * address=Address{address='广东'}, * books=[十日终焉, 凡人修仙传, 夏日重现], * hobbys=[打游戏, 看电影, 爱躺平], * card={身份证=444444444444444444}, * games=[和平精英, 原神], * wife='null', * info={ * 学号=20230302222, * 性别=男, * password=123456, * 姓名=cike_y, * username=root * } * } */}}

官方文档:

https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-collection-elements https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-properties-detailed.html

扩展注入

注意:p命名空间和c命名空间的使用,需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p"xmlns:c="http://www.springframework.org/schema/c"

p命名空间 (相当于set)

要用它一定要先导入官方文档中的p命名空间

xmlns:p="http://www.springframework.org/schema/p"

它对应着set注入的一些方法

  • 普通值注入
  • 引用ref对象
<!--p命名空间注入,可以直接注入属性的值:property--><beanid="user"class="com.cike4.pojo.User"p:name="cike_y"p:age="20"/><!--相当于 <bean id="user" class="com.cike4.pojo.User"> <property name="name" value="cike_y"/> <property name="age" value="20"/> </bean> -->

也可以引用其他的Bean对象

userbeans.xml中

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"><!--p命名空间注入,可以直接注入属性的值:property--><beanid="user"class="com.cike4.pojo.User"p:name="cike_y"p:age="20"/><!--相当于 <bean id="user" class="com.cike4.pojo.User"> <property name="name" value="cike_y"/> <property name="age" value="20"/> </bean> --></beans>

JavaBean 的User类

publicclassUser{privateStringname;privateintage;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}@OverridepublicStringtoString(){return"User{"+"name='"+name+'\''+", age="+age+'}';}}

测试方法中实例化对象

@Testpublicvoidtest2(){ApplicationContextcontext=newClassPathXmlApplicationContext("userbeans.xml");// 这里申明了类型,就不需要强壮类型 User 类了Useruser=context.getBean("user",User.class);System.out.println(user.toString());}

c命名空间(相当于构造器注入)

要先导入c命名空间

xmlns:c="http://www.springframework.org/schema/c"

userbean.xml 中

<!--c命名空间注入,可以通过构造器注入:construct-args--><beanid="user2"class="com.cike4.pojo.User"c:name="user2"c:age="20"/><!--相当于 <bean id="user2" class="com.cike4.pojo.User"> <constructor-arg name="name" value="cike_y"/> <constructor-arg name="age" value="20"/> </bean> -->

User类添加构造器方法

publicUser(){}publicUser(Stringname,intage){this.name=name;this.age=age;}

测试方法

@Testpublicvoidtest2(){ApplicationContextcontext=newClassPathXmlApplicationContext("userbeans.xml");// 这里申明了类型,就不需要强壮类型 User 类了Useruser=context.getBean("user2",User.class);System.out.println(user.toString());}

官方解释:

官方文档:

https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-p-namespace https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-properties-detailed.html#beans-p-namespace
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/14 23:45:42

python基于Vue的电影院售票选座评价影评系统_5s356_django Flask pycharm项目

目录已开发项目效果实现截图关于博主开发技术路线相关技术介绍核心代码参考示例结论源码lw获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;已开发项目效果实现截图 同行可拿货,招校园代理 ,本人源头供货商 python基于Vue的电影院售票选座评价影…

作者头像 李华
网站建设 2026/4/14 21:12:54

华为 ModelEngine Nexent 零代码平台智能体能力深度实践评测

前言 在 AI 技术加速落地的当下&#xff0c;智能体已从概念走向产业实践&#xff0c;但传统开发模式依赖复杂编程与 Prompt 工程&#xff0c;高门槛成为制约其规模化普及的核心瓶颈。而华为 ModelEngine Nexent 作为零代码级智能体开发平台&#xff0c;精准切中这一行业痛点&am…

作者头像 李华
网站建设 2026/4/15 15:52:41

python基于Vue的北京市公交线路管理系统_vi06l_django Flask pycharm项目

目录已开发项目效果实现截图关于博主开发技术路线相关技术介绍核心代码参考示例结论源码lw获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;已开发项目效果实现截图 同行可拿货,招校园代理 ,本人源头供货商 python基于Vue的北京市公交线路管理系…

作者头像 李华
网站建设 2026/4/14 13:34:43

目录结构保留的JAVA文件夹上传实现思路

大文件传输系统解决方案 项目背景与需求分析 作为北京某软件公司项目负责人&#xff0c;我们近期面临一个技术挑战&#xff1a;在产品中集成一个高性能、高稳定性的超大文件传输系统。经过详细需求分析&#xff0c;我们确认以下核心需求&#xff1a; 大文件处理能力&#xf…

作者头像 李华
网站建设 2026/4/14 21:10:57

毕设项目 基于机器视觉的驾驶疲劳检测系统(源码+论文)

文章目录 0 前言1 项目运行效果2 课题背景3 Dlib人脸检测与特征提取3.1 简介3.2 Dlib优点 4 疲劳检测算法4.1 眼睛检测算法4.2 打哈欠检测算法4.3 点头检测算法 5 PyQt55.1 简介5.2相关界面代码 6 最后 0 前言 &#x1f525;这两年开始毕业设计和毕业答辩的要求和难度不断提升…

作者头像 李华
网站建设 2026/4/4 7:37:00

Web编辑器复制PPT图片并自动上传服务器组件

企业网站后台管理系统富文本编辑器功能扩展项目记录 一、项目背景与需求分析 作为北京某集团企业项目负责人&#xff0c;近期负责的项目中遇到新需求&#xff1a;在企业网站后台管理系统文章发布模块增加 Word 粘贴和导入功能&#xff0c;支持微信公众号内容粘贴&#xff08;…

作者头像 李华