大连市网站建设_网站建设公司_小程序网站_seo优化
2025/12/30 13:04:10 网站建设 项目流程

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

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询