news 2026/6/15 15:28:39

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/6/15 13:22:49

产品为王!用友HR SaaS斩获2025数字人力资源科技最佳产品奖

近期&#xff0c;备受行业瞩目的2025数字人力资源科技奖&#xff08;Digital HRTech Awards&#xff09;正式揭晓&#xff0c;用友HR SaaS成功斩获“2025数字人力资源科技最佳产品奖”。本奖项聚焦人力资源科技的创新价值与实践成效&#xff0c;是行业内极具权威性的专业评选。…

作者头像 李华
网站建设 2026/6/15 12:17:22

中国AI的致命短板:超越“追赶”,构建文明级战略操作系统

中国AI的致命短板&#xff1a;超越“追赶”&#xff0c;构建文明级战略操作系统 埃里克施密特的发言&#xff0c;一针见血地指出了中美AI竞赛中一个常被忽视的结构性差距&#xff1a;资本深度与创新生态的鸿沟。他揭示的并非单纯的技术落后&#xff0c;而是支撑技术爆炸的底层…

作者头像 李华
网站建设 2026/6/15 12:17:13

AI Agent 工程失效的确定性来源与 EDCA 控制解法(工程向)

本文默认读者具备 Agent 系统工程背景&#xff0c;默认读者有EDCA 体系概念 不讨论模型能力&#xff0c;不讨论 Prompt 技巧。1. Agent 失效不是“不聪明”&#xff0c;而是控制面缺失在真实工程中&#xff0c;以下问题高度重复出现&#xff1a;长任务中决策不可预测多 Agent 协…

作者头像 李华
网站建设 2026/6/15 13:26:01

如何在CSDN创作一篇98分高质量的技术博客?

如何在CSDN创作一篇98分高质量的技术博客&#xff1f; 摘要 在CSDN上创作一篇技术博客&#xff0c;如何才能达到98分的高质量&#xff1f;这不仅仅是关于内容本身的深度与广度&#xff0c;还包括文章的结构、排版、互动性等多个方面。本文将介绍如何从各个方面提升你的技术博…

作者头像 李华
网站建设 2026/6/15 12:19:18

非阻塞socket上getsockopt函数的使用要点及常见误解

在网络编程中&#xff0c;理解并正确使用非阻塞socket上的getsockopt函数&#xff0c;是确保程序行为可控、性能高效的关键。许多开发者误以为在非阻塞模式下&#xff0c;所有操作都变得简单&#xff0c;实际上&#xff0c;像getsockopt这样的“查询”操作也隐含着微妙的陷阱和…

作者头像 李华