首先在项目中namespace导入p命名空间 可以方便定义对象属性
<bean id="s4" class="com.atguigu.spring.di.Student" p:id="10033" p:name="赵六" p:age="26" p:sex="女" p:teacher-ref="teacher">
</bean>
<bean id="s1" class="com.atguigu.spring.di.Student">
<property name="id">
<value>10010</value> //通过value标签进行赋值
</property>
<property name="name" value="张三"></property>
<property name="age" value="23"></property>
<property name="sex" value="男"></property>
</bean>
ref关键字!
<bean id="s5" class="com.atguigu.spring.di.Student">
<property name="id" value="10055"></property>
<property name="name" value="张三三"></property>
<property name="age" value="23"></property>
<property name="sex" value="男"></property>
<property name="teacher" ref="teacher"></property>//通过ref使用其他类的值
<property name="teacher.tname" value="徐老师"></property>//通过修改标识 来进行值的修改
</bean>
<bean id="teacher" class="com.atguigu.spring.di.Teacher">
<property name="tid" value="10000"></property>
<property name="tname" value="于老师"></property>
</bean>
<bean id="s6" class="com.atguigu.spring.di.Student">
<property name="id" value="10066"></property>
<property name="name" value="崔八"></property>
<property name="age" value="18"></property>
<property name="sex" value="男"></property>
<property name="teacher">
<bean id="tt" class="com.atguigu.spring.di.Teacher">
<property name="tid" value="222"></property>
<property name="tname" value="黄老师"></property>
</bean>
</property>
</bean>
<!-- 定义在某个bean内部的bean,只能在当前bean中使用 -->
<!-- <bean id="s7" class="com.atguigu.spring.di.Student">
<property name="id" value="10066"></property>
<property name="name" value="崔八"></property>
<property name="age" value="18"></property>
<property name="sex" value="男"></property>
<property name="teacher" ref="tt"></property>
</bean> -->
private List<String> cls;
<bean id="t1" class="com.atguigu.spring.di.Teacher">
<property name="tid" value="10001"></property>
<property name="tname" value="何老师"></property>
<property name="cls">
<list>
<value>A</value>
<value>B</value>
<value>C</value>
</list>
</property>
</bean>
private List<Student> students; //注意命名格式 格式错误时无法赋值
<bean id="t2" class="com.atguigu.spring.di.Teacher">
<property name="tid" value="10002"></property>
<property name="tname" value="李老师"></property>
<property name="students">
<list>
<ref bean="s1"/>
<ref bean="s2"/>
<ref bean="s3"/>
</list>
</property>
</bean>
private Map<String,String> bossMap; //map数组中一个entry代表一个"键 值"对
<bean id="t3" class="com.atguigu.spring.di.Teacher">
<property name="tid" value="10003"></property>
<property name="tname" value="admin"></property>
<property name="bossMap">
<map>
<entry>
<key>
<value>10001</value>
</key>
<value>佟老师</value>
</entry>
<entry>
<key>
<value>10002</value>
</key>
<value>陈老师</value>
</entry>
</map>
</property>
</bean>
private List<Student> students;
<bean id="t4" class="com.atguigu.spring.di.Teacher">
<property name="tid" value="10004"></property>
<property name="tname" value="root"></property>
<property name="students" ref="s"></property>
</bean>
<util:list id="s"> //此时的students指的是Student类的集合
<ref bean="s4"/>
<ref bean="s5"/>
<ref bean="s6"/>
</util:list>
//对于map集合也适用
<util:map id="map">
<entry>
<key>
<value>1</value>
</key>
<value>张三</value>
</entry>
</util:map>
注意:
出错原因:
<util:list id="s">
<list>
<ref bean="s4"/>
<ref bean="s5"/>
<ref bean="s6"/>
</list>
</util:list>
//list中包含的是student对象的类 而不应该是一个集合
工厂bean FactoryBean
public class Car {
private String brand;
private Double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
//实现工厂设计模式FactoryBean
public class MyFacory implements FactoryBean<Car> {
//获取对象
@Override
public Car getObject() throws Exception {
Car car = new Car();//工厂式创建对象
car.setBrand("奥迪");
car.setPrice(200000.0);
return car;
}
//获取对象类型 所属类型
@Override
public Class<?> getObjectType() {
return Car.class;
}
//是否为单例模式
@Override
public boolean isSingleton() {
return false;
}
}
<bean id="factory" class="com.atguigu.spring.factorybean.MyFacory"> </bean>
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("factory-bean.xml");
Object object = ac.getBean("factory");
System.out.println(object);//获取的是MyFactory下创建的car对象
}
}