1. 对象引用
设置一个类去引用另一个类
Class.java
public class Class {
private String className;
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void setClassName(String className) {
this.className = className;
}
public String getClassName() {
return className;
}
@Override
public String toString() {
return "Class{" +
"className='" + className + '\'' +
", user=" + user +
'}';
}
}
配置xml文件的配置有三种方式
- 使用ref属性
<bean id="class" class="spring.beans.Class">
<property name="className" value="初一"></property>
<property name="user" ref="user"></property>
</bean>
- 使用ref子标签
<bean id="class" class="spring.beans.Class">
<property name="className" value="初一"></property>
<property name="user" >
<ref bean="user"></ref>
</property>
</bean>
- 设置内部bean
<bean id="class" class="spring.beans.Class">
<property name="className" value="初一"></property>
<property name="user" >
<bean class="spring.beans.User">
<constructor-arg type="java.lang.String">
<value><![CDATA[《张三!@>]]></value>
</constructor-arg>
<constructor-arg value="11" index="1"></constructor-arg>
</bean>
</property>
</bean>
在主函数调用
public static void main(String[] args) {
// 1. 创建Spring的IOC容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从IOC容器中获取Bean实例
Class aclass = (Class) applicationContext.getBean("class");
// 3. 调用hello方法
System.out.println(aclass.toString());
}
运行结果
2. null属性和级联属性
2.1 null属性
配置文件xml配置
<bean id="class" class="spring.beans.Class">
<property name="className" value="初一"></property>
<property name="user" >
<null/>
</property>
</bean>
在次运行主函数
public static void main(String[] args) {
// 1. 创建Spring的IOC容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从IOC容器中获取Bean实例
Class aclass = (Class) applicationContext.getBean("class");
// 3. 调用hello方法
System.out.println(aclass.toString());
}
运行结果
注意:使用null属性后相应的对象会复制为null
2.2 级联属性
首先配置级联属性
<bean id="class" class="spring.beans.Class">
<property name="className" value="初一"></property>
<constructor-arg ref="user"></constructor-arg>
<!-- 为级联属性赋值。注意:属性需要先进行初始化才可以为级联属性赋值,否则会出现异常。-->
<property name="user.name" value="ok"></property>
</bean>
接着给要赋值的级联属性构造set函数
public void setName(String name) {
this.name = name;
}
设置bean类的对应的构造函数
public Class(User user) {
this.user = user;
}
最后运行主函数
public static void main(String[] args) {
// 1. 创建Spring的IOC容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从IOC容器中获取Bean实例
Class aclass = (Class) applicationContext.getBean("class");
// 3. 调用hello方法
System.out.println(aclass.toString());
}
运行结果
注意:在设置级联属性的时候要设置相应的set函数,与构造器
3. 集合属性
在配置文件xml中进行配置使用list标签用于指定集合中的多个元素。
3.1 list集合
xml配置
<bean id="user1" class="spring.collection.User">
<constructor-arg type="java.lang.String" value="张三"></constructor-arg>
<constructor-arg type="int" value="10"></constructor-arg>
</bean>
<bean id="user2" class="spring.collection.User">
<constructor-arg type="java.lang.String" value="李四"></constructor-arg>
<constructor-arg type="int" value="20"></constructor-arg>
</bean>
<bean id="classList" class="spring.collection.Class">
<property name="className" value="初一"></property>
<property name="users">
<list>
<ref bean="user1"></ref>
<ref bean="user2"/>
</list>
</property>
</bean>
给Class类添加list元素
private List<User> users;
public void setUsers(List<User> users) {
this.users = users;
}
public List<User> getUsers() {
return users;
}
运行主函数
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Class aclass = (Class) applicationContext.getBean("classList");
System.out.println(aclass.toString());
}
运行结果
3.2 map集合
配置文件xml配置
<bean id="user1" class="spring.collection.User">
<constructor-arg type="java.lang.String" value="张三"></constructor-arg>
<constructor-arg type="int" value="10"></constructor-arg>
</bean>
<bean id="user2" class="spring.collection.User">
<constructor-arg type="java.lang.String" value="李四"></constructor-arg>
<constructor-arg type="int" value="20"></constructor-arg>
</bean>
<bean id="classList" class="spring.collection.Class">
<property name="className" value="初一"></property>
<property name="users">
<map>
<entry key="one" value-ref="user1"></entry>
<entry key="two" value-ref="user2"></entry>
</map>
</property>
</bean>
改变Class函数
private Map users;
public void setUsers(Map users) {
this.users = users;
}
public Map getUsers() {
return users;
}
运行结果
3.3 Properties用于配置数据库连接
设置配置文件xml
bean id="dataSource" class="spring.collection.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
新建DataSource类
public class DataSource {
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
public Properties getProperties() {
return properties;
}
@Override
public String toString() {
return "DataSource{" +
"properties=" + properties +
'}';
}
}
运行主函数
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
System.out.println(dataSource.toString());
}
运行结果
4. utility scheme定义集合
配置相应的xml文件
<bean id="user1" class="spring.collection.User">
<constructor-arg type="java.lang.String" value="张三"></constructor-arg>
<constructor-arg type="int" value="10"></constructor-arg>
</bean>
<bean id="user2" class="spring.collection.User">
<constructor-arg type="java.lang.String" value="李四"></constructor-arg>
<constructor-arg type="int" value="20"></constructor-arg>
</bean>
<!-- 配置单例的集合bean,以供多个bean进行引用-->
<util:list id="users">
<ref bean="user1"></ref>
<ref bean="user2"></ref>
</util:list>
<bean id="classList" class="spring.collection.Class">
<property name="className" value="初一"></property>
<property name="users" ref="users"></property>
</bean>
运行结果
5. 使用p命名空间
配置相应的xml文件
<bean id="user1" class="spring.collection.User">
<constructor-arg type="java.lang.String" value="张三"></constructor-arg>
<constructor-arg type="int" value="10"></constructor-arg>
</bean>
<bean id="user2" class="spring.collection.User">
<constructor-arg type="java.lang.String" value="李四"></constructor-arg>
<constructor-arg type="int" value="20"></constructor-arg>
</bean>
<!-- 配置单例的集合bean,以供多个bean进行引用-->
<util:list id="users">
<ref bean="user1"></ref>
<ref bean="user2"></ref>
</util:list>
<bean id="classList" class="spring.collection.Class" p:className="初一" p:users-ref="users"></bean>
运行结果