Bean管理环境类外部bean级联赋值
内部bean集合类型注入环境类xml文件
集合里边设置对象的注入
Bean管理
环境类
public class User {
public String name
;
public String password
;
public Tool tool
;
public void setName(String name
) {
this.name
= name
;
}
public void setPassword(String password
) {
this.password
= password
;
}
public void setTool(Tool tool
) {
this.tool
= tool
;
}
@Override
public String
toString() {
return "User{" +
"name='" + name
+ '\'' +
", password='" + password
+ '\'' +
'}';
}
}
public class Tool {
public String pencil
;
public void setPencil(String pencil
) {
this.pencil
= pencil
;
}
@Override
public String
toString() {
return "Tool{" +
"pencil='" + pencil
+ '\'' +
'}';
}
}
外部bean
<!--xml配置文件
-->
<!--User类
-->
<bean id
="user" class="com.d.Spring.IOC.User">
<property name
="name" value
="sudi"></property
>
<property name
="password" value
="123456"></property
>
<!--外部bean
:ref属性用来引入外部bean 与使用bean标签创建出的Tool类的id属性一致
->
<property name
="tool" ref
="tool"></property
>
</bean
>
<!--Tool类
-->
<bean id
="tool" class="com.d.Spring.IOC.Tool">
<property name
="pencil" value
="铅笔"></property
>
</bean
>
级联赋值
<bean id
="user" class="com.d.Spring.IOC.User">
<property name
="name" value
="sudi"></property
>
<property name
="password" value
="123456"></property
>
<!--级联赋值
1.先引入bean
2.设置属性值
->
<!--特别注意
:User中有Tool类型的成员变量
,因为Tool是一个对象
,所以
要想设置其中的tool中的成员变量
,就要先得到这个tool对象
,因此
User类中必须有tool的getter方法
,否则则会报错
-->
<property name
="tool" ref
="tool"></property
>
<property name
="tool.pencil" value
="铅笔"></property
>
</bean
>
<bean id
="tool" class="com.d.Spring.IOC.Tool"></bean
>
内部bean
<!--User类
-->
<bean id
="user" class="com.d.Spring.IOC.User">
<property name
="name" value
="sudi"></property
>
<property name
="password" value
="123456"></property
>
<!--内部bean
:将使用bean标签创建类的动作放在property内部
-->
<property name
="tool">
<bean id
="tool" class="com.d.Spring.IOC.Tool">
<property name
="pencil" value
="铅笔"></property
>
</bean
>
</property
>
</bean
>
集合类型注入
环境类
package com
.d
.Spring
.IOC
;
import java
.util
.Arrays
;
import java
.util
.List
;
import java
.util
.Map
;
import java
.util
.Set
;
public class Students {
public int[] name
;
public List
<Integer> id
;
public Map
<String,String> course
;
public Set
<String> teacher
;
public void setName(int[] name
) {
this.name
= name
;
}
public void setId(List
<Integer> id
) {
this.id
= id
;
}
public void setCourse(Map
<String, String> course
) {
this.course
= course
;
}
public void setTeacher(Set
<String> teacher
) {
this.teacher
= teacher
;
}
@Override
public String
toString() {
return "Students{" +
"name=" + Arrays
.toString(name
) +
", id=" + id
+
", course=" + course
+
", teacher=" + teacher
+
'}';
}
}
xml文件
<?xml version
="1.0" encoding
="UTF-8"?>
<beans xmlns
="http://www.springframework.org/schema/beans"
xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns
:context
="http://www.springframework.org/schema/context"
xmlns
:aop
="http://www.springframework.org/schema/aop"
xsi
:schemaLocation
="http
://www
.springframework
.org
/schema
/beans
http
://www
.springframework
.org
/schema
/beans
/spring
-beans
.xsd
http
://www
.springframework
.org
/schema
/context
http
://www
.springframework
.org
/schema
/context
/spring
-context
.xsd
http
://www
.springframework
.org
/schema
/aop
http
://www
.springframework
.org
/schema
/aop
/spring
-aop
.xsd"
>
<bean name
="students" class="com.d.Spring.IOC.Students">
<property name
="name">
<array>
<value>2020</value
>
<value>2020</value
>
<value>2020</value
>
</array
>
</property
>
<property name
="id">
<list>
<value>1</value
>
<value>2</value
>
<value>3</value
>
</list
>
</property
>
<property name
="course">
<map>
<entry key
="Java" value
="java"></entry
>
<entry key
="C" value
="c"></entry
>
<entry key
="C++" value
="c++"></entry
>
</map
>
</property
>
<property name
="teacher">
<set>
<value>张三老师
</value
>
<value>王五老师
</value
>
<value>李四老师
</value
>
</set
>
</property
>
</bean
>
</beans
>
集合里边设置对象的注入
package com
.d
.Spring
.IOC
;
import java
.util
.List
;
public class Me {
public List
<Friends> friends
;
public List
<Friends> getFriends() {
return friends
;
}
public void setFriends(List
<Friends> friends
) {
this.friends
= friends
;
}
@Override
public String
toString() {
return "Me{" +
"friends=" + friends
+
'}';
}
}
package com
.d
.Spring
.IOC
;
public class Friends {
public String name
;
public void setName(String name
) {
this.name
= name
;
}
}
<?xml version
="1.0" encoding
="UTF-8"?>
<beans xmlns
="http://www.springframework.org/schema/beans"
xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns
:context
="http://www.springframework.org/schema/context"
xmlns
:aop
="http://www.springframework.org/schema/aop"
xsi
:schemaLocation
="http
://www
.springframework
.org
/schema
/beans
http
://www
.springframework
.org
/schema
/beans
/spring
-beans
.xsd
http
://www
.springframework
.org
/schema
/context
http
://www
.springframework
.org
/schema
/context
/spring
-context
.xsd
http
://www
.springframework
.org
/schema
/aop
http
://www
.springframework
.org
/schema
/aop
/spring
-aop
.xsd"
>
<bean name
="me" class="com.d.Spring.IOC.Me">
<property name
="friends">
<list>
<ref bean
="friends"/>
</list
>
</property
>
</bean
>
<!--先在外部用bean标签创建Friends的一个名为friends实例对象
,之后用ref标签引入到Me创建的实例对象me中完成赋值
-->
<bean name
="friends" class="com.d.Spring.IOC.Friends">
<property name
="name" value
="小红"></property
>
</bean
>
</beans
>