Spring中IoC操作Bean管理(基于xml方法)
【摘要】
文章目录
一.基于 xml 方式创建对象二.基于 xml 方式注入属性1.使用 set 方法进行注入2.使用有参数构造进行注入3.p名称空间注入(只做了解)
三.xml注入其他类型属性1....
一.基于 xml 方式创建对象
首先创建一个Person类,如下:
public class Person {
private String name;
private int age;
private String gender;
public void eat(){
System.out.println("吃饭!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
- 在 bean 标签有很多属性,介绍常用的属性
- id 属性:唯一标识
- class 属性:类全路径(包类路径)
- 创建对象时候,默认也是执行无参数构造方法完成对象创建
xml配置中添加如下代码,完成对象创建:
<bean id="person" class="iocbean.byxml.Person"></bean>
- 1
二.基于 xml 方式注入属性
DI:依赖注入,就是注入属性
分为两种注入方式:
- set 方法进行注入
- 有参数构造进行注入
1.使用 set 方法进行注入
<bean id="person" class="iocbean.byxml.Person">
<!--set 方法注入属性-->
<!--使用 property 完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="name" value="小明"></property>
<property name="age" value="21"></property>
<property name="gender" value="男"></property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2.使用有参数构造进行注入
首先在刚才创建的Person类中添加有参构造器
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
- 1
- 2
- 3
- 4
- 5
然后在 spring 配置文件配置对象创建,配置属性注入
<bean id="person1" class="iocbean.byxml.Person">
<!--有参数构造注入属性-->
<!--使用 constructor-arg 完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<constructor-arg name="name" value="小红"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="gender" value="女"></constructor-arg>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.p名称空间注入(只做了解)
使用p名称空间注入,可以简化基于xml的配置方式
首先我们先添加p名称空间在配置文件中
然后进行属性注入,在bean标签里面进行操作
<!-- set方法注入属性
P:xxx 其中xxx为类里面的属性名称
例如:p:name="小华" 其中name为Person类的属性
-->
<bean id="person2" class="iocbean.byxml.Person" p:name="小华" p:age="20" p:gender="男"></bean>
- 1
- 2
- 3
- 4
- 5
三.xml注入其他类型属性
1.字面量
- null值
<property name="name">
<null/>
</property>
- 1
- 2
- 3
- 属性值包含特殊符号
把带特殊符号内容写到 CDATA内
<property name="gender">
<value><![CDATA[<男>]]></value>
</property>
- 1
- 2
- 3
2.注入属性(外部bean)
在已有上述Person类的情况下,再创建一个Student类,在Student里有类型为Person的属性,Student类如下:
public class Student {
private Person person;
private String university;
public Student() {
}
public Student(Person person, String university) {
this.person = person;
this.university = university;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getUniversity() {
return university;
}
public void setUniversity(String university) {
this.university = university;
}
@Override
public String toString() {
return "Student{" +
"person=" + person +
", university='" + university + '\'' +
'}';
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
在配置文件中进行配置:
<bean id="student0" class="iocbean.byxml.Student">
<!--注入 Person 对象
name 属性:类里面属性名称
ref 属性:创建 Person 对象 bean 标签 id 值 -->
<property name="person" ref="person1"></property>
<property name="university" value="Yangtze University"></property>
</bean>
<bean id="person1" class="iocbean.byxml.Person"></bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.注入属性(内部bean)
<bean id="student1" class="iocbean.byxml.Student">
<!--设置一个普通属性-->
<property name="university" value="Yangtze University"></property>
<!--设置对象类型属性-->
<property name="person">
<bean id="person1" class="iocbean.byxml.Person">
<property name="name">
<value><![CDATA[<小>]]>蓝</value>
</property>
</bean>
</property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
4.注入属性(级联赋值)
<!--级联赋值-->
<bean id="student2" class="iocbean.byxml.Student">
<property name="university" value="Yangtze University"></property>
<property name="person" ref="person0"></property>
<property name="person.name" value="改名了"></property>
</bean>
<bean id="person0" class="iocbean.byxml.Person">
<!--set 方法注入属性-->
<!--使用 property 完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="name" value="小明"></property>
<property name="age" value="21"></property>
<property name="gender" value="男"></property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
四.xml 注入集合属性
1.注入数组、list、map、set 类型属性
创建CollectionDemo类,如下:
public class CollectionDemo {
private String[] name;
private List<String> list;
private Map<String,String> maps;
private Set<String> sets;
public void setName(String[] name) {
this.name = name;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
@Override
public String toString() {
return "CollectionDemo{" +
"name=" + Arrays.toString(name) +
", list=" + list +
", maps=" + maps +
", sets=" + sets +
'}';
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="collectionDemo" class="iocbean.byxml.collection.CollectionDemo">
<!--数组类型属性注入-->
<property name="name">
<array>
<value>小明</value>
<value>小华</value>
<value>小红</value>
</array>
</property>
<!--list 类型属性注入-->
<property name="list">
<list>
<value>123</value>
<value>456</value>
<value>789</value>
</list>
</property>
<!--map 类型属性注入-->
<property name="maps">
<map>
<entry key="数学" value="mathematics"></entry>
<entry key="物理" value="physics"></entry>
<entry key="生物" value="biology"></entry>
</map>
</property>
<!--set 类型属性注入-->
<property name="sets">
<set>
<value>150</value>
<value>110</value>
<value>90</value>
</set>
</property>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
2.在集合里面设置对象类型值
<!--创建多个 course 对象-->
<bean id="student0" class="iocbean.byxml.collection.Student">
<property name="name" value="小红"></property>
</bean>
<bean id="student1" class="iocbean.byxml.collection.Student">
<property name="name" value="小华"></property>
</bean>
<bean id="student2" class="iocbean.byxml.collection.Student">
<property name="name" value="小明"></property>
</bean>
<!--注入 list 集合类型,值是对象-->
<bean id="collectionDemo1" class="iocbean.byxml.collection.CollectionDemo1">
<property name="studnets">
<list>
<ref bean="student0"></ref>
<ref bean="student1"></ref>
<ref bean="student2"></ref>
</list>
</property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
3.把集合注入部分提取出来
- 在Spring配置文件中引入名称空间util
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
- 1
- 2
- 3
- 4
- 5
- 使用 util 标签完成 list 集合注入提取
<!--1 提取 list 集合类型属性注入-->
<util:list id="studentsList">
<ref bean="student0"></ref>
<ref bean="student1"></ref>
<ref bean="student2"></ref>
</util:list>
<!--2 提取 list 集合类型属性注入使用-->
<bean id="collectionDemo2" class="iocbean.byxml.collection.CollectionDemo1">
<property name="studnets" ref="studentsList"></property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
文章来源: blog.csdn.net,作者:Mr.Yushiwen,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/MrYushiwen/article/details/110674649
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)